Giter VIP home page Giter VIP logo

ntc-jnats's Introduction

ntc-jnats

ntc-jnats is a module NATS java client.

Maven

<dependency>
    <groupId>com.streetcodevn</groupId>
    <artifactId>ntc-jnats</artifactId>
    <version>1.0.0</version>
</dependency>

1. Publish-Subscribe

Publish-Subscribe

Publisher

String subj = "msg.test";
for (int i=0; i<10; i++) {
    String msg = "hello " + i;
    NPub.getInstance("pub-notify").publish(subj, msg);
    log.info("Published PubSub["+subj+"] : '"+msg+"'");
}

Subscriber

public static class NSubscriber extends NSub {
    private final Logger log = LoggerFactory.getLogger(NSubscriber.class);

    public NSubscriber(String name) throws IOException, InterruptedException {
        super(name);
    }

    @Override
    public void execute(Message msg) {
        try {
            String data = new String(msg.getData(), StandardCharsets.UTF_8);
            log.info("NSubscriber received on PubSub ["+getSubject()+"]: '"+data+"'");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

public static void main(String[] args) {
    try {
        NSubGroup nsubGroup = new NSubGroup();
        for (int i=0; i< 2; i++) {
            NSubscriber ns = new NSubscriber("sub-notify");
            nsubGroup.add(ns);
        }
        nsubGroup.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2. Queue Groups

Queue Groups

Queue Worker

public static class NWorkerEmail extends NWorker {
    private final Logger log = LoggerFactory.getLogger(NWorkerEmail.class);

    public NWorkerEmail(String name) throws IOException, InterruptedException {
        super(name);
    }

    @Override
    public void execute(Message msg) {
        try {
            String data = new String(msg.getData(), StandardCharsets.UTF_8);
            log.info("NWorkerEmail["+getGroup()+"] received on QueueWorker["+getSubject()+"]: '"+data+"'");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

public static void main(String[] args) {
    try {
        NWorkerGroup workerGroup = new NWorkerGroup();
        for (int i=0; i<2; i++) {
            NWorkerEmail nw = new NWorkerEmail("worker-email");
            workerGroup.add(nw);
        }
        workerGroup.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Publisher

String subj = "worker.email";
for (int i=0; i<10; i++) {
    String msg = "hello " + i;
    NPub.getInstance("pub-notify").publish(subj, msg);
    log.info("Published QueueWorker["+subj+"] : '"+msg+"'");
}

3. Request-Reply

Request-Reply

Request

String subj = "reqres";
for (int i=0; i<10; i++) {
    String msg = "this is request " + i;
    Message resp = NReq.getInstance("req-db").publish(subj, msg);
    log.info("NReq Requested ["+subj+"] : '"+msg+"'");
    log.info("NReq Received  ["+resp.getSubject()+"] : '"+new String(resp.getData(), StandardCharsets.UTF_8)+"'");
}

Reply

public static class NResQueryDB extends NRes {
    private final Logger log = LoggerFactory.getLogger(NResQueryDB.class);
    private String reply = "this is response ==> ";

    public NResQueryDB(String name) throws IOException, InterruptedException {
        super(name);
    }

    @Override
    public void execute(Message msg) {
        try {
            String data = new String(msg.getData(), StandardCharsets.UTF_8);
            log.info("NRes["+getGroup()+"] Received on QueueNRes["+getSubject()+"]: '"+data+"'");
            String datares = reply + data;
            reply(msg, datares);
            log.info("NRes["+getGroup()+"] Reply on QueueNRes["+getSubject()+"]: '"+datares+"'");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

public static void main(String[] args) {
    try {
        NResGroup resGroup = new NResGroup();
        for (int i=0; i<2; i++) {
            NResQueryDB res = new NResQueryDB("res-db");
            resGroup.add(res);
        }
        resGroup.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

License

This code is under the Apache License v2.

ntc-jnats's People

Contributors

congnghia0609 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.