Skip to content

Commit

Permalink
Showing 53 changed files with 1,248 additions and 174 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

# Change Log

## Version 2.4.0

* [ADDED] - support for JWT-based authentication and NGS
* [FIXED] - issue with norandomize server connect order, it was broken at some point
* [FIXED] ##199 - import of a private package
* [FIXED] #195 - issue with "discovered" servers not having a protocol but we tried to parse as uri
* [FIXED] #186, #191 - sneaky issue with connection reader not reseting state on reconnect
* [ADDED] #192 - option to set inbox prefix, default remains the same
* [FIXED] #196 - updated all the versions i could find, and added note in gradle file about them

## Version 2.3.0

* [BREAKING CHANGE] Replaced use of strings for seeds and public keys in NKey code to use char[] to allow better memory security.
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -11,11 +11,12 @@ plugins {
}

// Update version here, repeated check-ins not into master will have snapshot on them
// Be sure to update Nats.java with the latest version, the change log and the package-info.java
def versionMajor = 2
def versionMinor = 3
def versionMinor = 4
def versionPatch = 0
def versionModifier = ""
def jarVersion = "2.3.0"
def jarVersion = "2.4.0"
def branch = System.getenv("TRAVIS_BRANCH");

def getVersionName = { ->
@@ -136,6 +137,17 @@ task sourcesJar(type: Jar) {
from sourceSets.main.allSource
}

task fatJar(type: Jar) {
classifier = 'fat'
manifest {
attributes('Implementation-Title': 'Java Nats With Dependencies',
'Implementation-Version': jarVersion,
'Implementation-Vendor': 'nats.io')
}
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

jacocoTestReport {
reports {
xml.enabled = true // coveralls plugin depends on xml format report
4 changes: 4 additions & 0 deletions src/examples/java/io/nats/examples/ExampleAuthHandler.java
Original file line number Diff line number Diff line change
@@ -68,4 +68,8 @@ public byte[] sign(byte[] nonce) {
return null;
}
}

public char[] getJWT() {
return null;
}
}
52 changes: 52 additions & 0 deletions src/examples/java/io/nats/examples/ExampleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package io.nats.examples;

import java.time.Duration;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.ConnectionListener;
import io.nats.client.Nats;
import io.nats.client.Options;

public class ExampleUtils {
public static Options createExampleOptions(String server, boolean allowReconnect) throws Exception {
Options.Builder builder = new Options.Builder().
server(server).
connectionTimeout(Duration.ofSeconds(5)).
pingInterval(Duration.ofSeconds(10)).
reconnectWait(Duration.ofSeconds(1)).
connectionListener(new ConnectionListener(){
public void connectionEvent(Connection conn, Events type) {
System.out.println("Status change "+type);
}
});

if (!allowReconnect) {
builder = builder.noReconnect();
} else {
builder = builder.maxReconnects(-1);
}

if (System.getenv("NATS_NKEY") != null && System.getenv("NATS_NKEY") != "") {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
} else if (System.getenv("NATS_CREDS") != null && System.getenv("NATS_CREDS") != "") {
builder.authHandler(Nats.credentials(System.getenv("NATS_CREDS")));
}

return builder.build();
}
}
21 changes: 8 additions & 13 deletions src/examples/java/io/nats/examples/NatsDispatch.java
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@
import java.time.Duration;
import java.util.concurrent.CountDownLatch;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
public class NatsDispatch {

static final String usageString =
"\nUsage: java NatsDispatch [server] <subject> <msgCount>"
"\nUsage: java NatsDispatch [server] <subject> <msgCount>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -50,16 +50,11 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

System.out.println();
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
Connection nc = Nats.connect(builder.build());
System.out.println();

Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread

Dispatcher d = nc.createDispatcher((msg) -> {
22 changes: 9 additions & 13 deletions src/examples/java/io/nats/examples/NatsPub.java
Original file line number Diff line number Diff line change
@@ -16,18 +16,18 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Nats;
import io.nats.client.Options;

public class NatsPub {

static final String usageString =
"\nUsage: java NatsPub [server] <subject> <text message>"
"\nUsage: java NatsPub [server] <subject> <text message>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -48,17 +48,13 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

Connection nc = Nats.connect(builder.build());
System.out.println();
System.out.printf("Sending %s on %s, server is %s\n", message, subject, server);
System.out.println();
nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));
nc.flush(Duration.ZERO);
nc.flush(Duration.ofSeconds(5));
nc.close();

} catch (Exception exp) {
21 changes: 7 additions & 14 deletions src/examples/java/io/nats/examples/NatsQSub.java
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Message;
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
public class NatsQSub {

static final String usageString =
"\nUsage: java NatsQSub [server] <subject> <queue> <msgCount>"
"\nUsage: java NatsQSub [server] <subject> <queue> <msgCount>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -53,18 +53,11 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

Connection nc = Nats.connect(builder.build());
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
Subscription sub = nc.subscribe(subject, queue);
nc.flush(Duration.ZERO);
nc.flush(Duration.ofSeconds(5));

System.out.println();
for(int i=0;i<msgCount;i++) {
Message msg = sub.nextMessage(Duration.ofHours(1));

21 changes: 7 additions & 14 deletions src/examples/java/io/nats/examples/NatsReply.java
Original file line number Diff line number Diff line change
@@ -17,7 +17,6 @@
import java.time.Duration;
import java.util.concurrent.CountDownLatch;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
public class NatsReply {

static final String usageString =
"\nUsage: java NatsReply [server] <subject> <msgCount>"
"\nUsage: java NatsReply [server] <subject> <msgCount>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -50,17 +50,10 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

Connection nc = Nats.connect(builder.build());
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread

System.out.println();
Dispatcher d = nc.createDispatcher((msg) -> {
System.out.printf("Received message \"%s\" on subject \"%s\", replying to %s\n",
new String(msg.getData(), StandardCharsets.UTF_8),
@@ -70,7 +63,7 @@ public static void main(String args[]) {
});
d.subscribe(subject);

nc.flush(Duration.ZERO);
nc.flush(Duration.ofSeconds(5));

latch.await();

23 changes: 9 additions & 14 deletions src/examples/java/io/nats/examples/NatsReq.java
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@

import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Message;
import io.nats.client.Nats;
@@ -25,10 +25,11 @@
public class NatsReq {

static final String usageString =
"\nUsage: java NatsReq [server] <subject> <text message>"
"\nUsage: java NatsReq [server] <subject> <text message>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -49,21 +50,15 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

Connection nc = Nats.connect(builder.build());
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));
Future<Message> replyFuture = nc.request(subject, message.getBytes(StandardCharsets.UTF_8));
Message reply = replyFuture.get();
Message reply = replyFuture.get(5, TimeUnit.SECONDS);

System.out.println();
System.out.printf("Received reply \"%s\" on subject \"%s\"\n",
new String(reply.getData(), StandardCharsets.UTF_8),
reply.getSubject());
System.out.println();

nc.close();

23 changes: 9 additions & 14 deletions src/examples/java/io/nats/examples/NatsSub.java
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;

import io.nats.client.AuthHandler;
import io.nats.client.Connection;
import io.nats.client.Message;
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
public class NatsSub {

static final String usageString =
"\nUsage: java NatsSub [server] <subject> <msgCount>"
"\nUsage: java NatsSub [server] <subject> <msgCount>\n"
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
+ "Use the URL for user/pass/token authentication.\n";
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
+ "\nUse the URL for user/pass/token authentication.\n";

public static void main(String args[]) {
String subject;
@@ -50,18 +50,13 @@ public static void main(String args[]) {
}

try {

Options.Builder builder = new Options.Builder().server(server).noReconnect();

if (System.getenv("NATS_NKEY") != null) {
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
builder.authHandler(handler);
}

System.out.println();
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
Connection nc = Nats.connect(builder.build());
System.out.println();

Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
Subscription sub = nc.subscribe(subject);
nc.flush(Duration.ZERO);
nc.flush(Duration.ofSeconds(5));

for(int i=0;i<msgCount;i++) {
Message msg = sub.nextMessage(Duration.ofHours(1));
Loading

0 comments on commit 952ba97

Please sign in to comment.