ntc-jnats

ntc-jnats is a module NAST java client

License

License

Categories

Categories

JNA Development Tools Native
GroupId

GroupId

com.streetcodevn
ArtifactId

ArtifactId

ntc-jnats
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

ntc-jnats
ntc-jnats is a module NAST java client
Project URL

Project URL

https://github.com/congnghia0609/ntc-jnats
Source Code Management

Source Code Management

https://github.com/congnghia0609/ntc-jnats/tree/master

Download ntc-jnats

How to add to project

<!-- https://jarcasting.com/artifacts/com.streetcodevn/ntc-jnats/ -->
<dependency>
    <groupId>com.streetcodevn</groupId>
    <artifactId>ntc-jnats</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.streetcodevn/ntc-jnats/
implementation 'com.streetcodevn:ntc-jnats:1.0.0'
// https://jarcasting.com/artifacts/com.streetcodevn/ntc-jnats/
implementation ("com.streetcodevn:ntc-jnats:1.0.0")
'com.streetcodevn:ntc-jnats:jar:1.0.0'
<dependency org="com.streetcodevn" name="ntc-jnats" rev="1.0.0">
  <artifact name="ntc-jnats" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.streetcodevn', module='ntc-jnats', version='1.0.0')
)
libraryDependencies += "com.streetcodevn" % "ntc-jnats" % "1.0.0"
[com.streetcodevn/ntc-jnats "1.0.0"]

Dependencies

compile (5)

Group / Artifact Type Version
com.streetcodevn : ntc-configuration jar 1.0.0
ch.qos.logback : logback-classic jar 1.2.3
ch.qos.logback : logback-core jar 1.2.3
org.slf4j : slf4j-api jar 1.7.25
io.nats : jnats jar 2.6.6

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.0.0