nsq-j

Java client for the NSQ realtime distributed messaging platform

License

License

GroupId

GroupId

com.sproutsocial
ArtifactId

ArtifactId

nsq-j
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

nsq-j
Java client for the NSQ realtime distributed messaging platform
Project URL

Project URL

https://github.com/sproutsocial/nsq-j
Source Code Management

Source Code Management

http://github.com/sproutsocial/nsq-j/tree/master

Download nsq-j

How to add to project

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

Dependencies

compile (6)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25
com.google.code.gson : gson jar 2.8.2
net.jcip : jcip-annotations jar 1.0
org.xerial.snappy : snappy-java Optional jar 1.1.2.6
ch.qos.logback : logback-core Optional jar 1.2.3
ch.qos.logback : logback-classic Optional jar 1.2.3

test (4)

Group / Artifact Type Version
junit : junit jar 4.13.1
org.powermock : powermock-module-junit4 jar 1.6.2
org.powermock : powermock-api-mockito jar 1.6.2
com.google.guava : guava jar 24.1.1-jre

Project Modules

There are no modules declared in this project.

Maven Central

nsq-j

Java client for the NSQ realtime distributed messaging platform

Install

Add a dependency using Maven

<dependency>
  <groupId>com.sproutsocial</groupId>
  <artifactId>nsq-j</artifactId>
  <version>1.2</version>
</dependency>

or Gradle

dependencies {
  compile 'com.sproutsocial:nsq-j:1.2'
}

Publish

Publisher publisher = new Publisher("nsqd-host");

byte[] data = "Hello nsq".getBytes();
publisher.publishBuffered("example_topic", data);

Buffers messages to improve performance (to 16k or 300 milliseconds by default),

publisher.publish("example_topic", data) publishes synchronously and returns after nsqd responds OK

You can batch messages manually and publish them all at once with publish(String topic, List<byte[]> messages)

Subscribe

public class PubExample {

    public static void handleData(byte[] data) {
        System.out.println("Received:" + new String(data));
    }

    public static void main(String[] args) {
        Subscriber subscriber = new Subscriber("lookup-host-1", "lookup-host-2");
        subscriber.subscribe("example_topic", "test_channel", PubExample::handleData);
    }
}

Uses the lookup-hosts to discover any servers publishing example_topic.

If handleData returns normally FIN is sent to nsqd to finish the message, if the handler throws any exception then the message is requeued and processing is delayed using exponential backoff. Delay times and max attempts are configurable.

For complete control handle the Message directly:

    public static void handleMessage(Message msg) {
        try {
            byte[] data = msg.getData();
            // ... complicated work ...
            msg.finish();
        }
        catch (Exception e) {
            msg.requeue();
        }
    }

Publishers and Subscribers are thread safe and should be reused. Your handler methods should be thread safe, make them synchronized if you are unsure.

Client.getDefaultClient().stop() waits for in-flight messages, closes all connections and allows all threads to exit.

Javadocs

com.sproutsocial

Sprout Social

Versions

Version
1.2
1.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9