Skip to content

Commit 952ba97

Browse files
authored
Merge pull request #200 from nats-io/v2.4.0
V2.4.0
2 parents f49af69 + 3426367 commit 952ba97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1248
-174
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11

22
# Change Log
33

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

616
* [BREAKING CHANGE] Replaced use of strings for seeds and public keys in NKey code to use char[] to allow better memory security.

build.gradle

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ plugins {
1111
}
1212

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

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

140+
task fatJar(type: Jar) {
141+
classifier = 'fat'
142+
manifest {
143+
attributes('Implementation-Title': 'Java Nats With Dependencies',
144+
'Implementation-Version': jarVersion,
145+
'Implementation-Vendor': 'nats.io')
146+
}
147+
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
148+
with jar
149+
}
150+
139151
jacocoTestReport {
140152
reports {
141153
xml.enabled = true // coveralls plugin depends on xml format report

src/examples/java/io/nats/examples/ExampleAuthHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ public byte[] sign(byte[] nonce) {
6868
return null;
6969
}
7070
}
71+
72+
public char[] getJWT() {
73+
return null;
74+
}
7175
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.examples;
15+
16+
import java.time.Duration;
17+
18+
import io.nats.client.AuthHandler;
19+
import io.nats.client.Connection;
20+
import io.nats.client.ConnectionListener;
21+
import io.nats.client.Nats;
22+
import io.nats.client.Options;
23+
24+
public class ExampleUtils {
25+
public static Options createExampleOptions(String server, boolean allowReconnect) throws Exception {
26+
Options.Builder builder = new Options.Builder().
27+
server(server).
28+
connectionTimeout(Duration.ofSeconds(5)).
29+
pingInterval(Duration.ofSeconds(10)).
30+
reconnectWait(Duration.ofSeconds(1)).
31+
connectionListener(new ConnectionListener(){
32+
public void connectionEvent(Connection conn, Events type) {
33+
System.out.println("Status change "+type);
34+
}
35+
});
36+
37+
if (!allowReconnect) {
38+
builder = builder.noReconnect();
39+
} else {
40+
builder = builder.maxReconnects(-1);
41+
}
42+
43+
if (System.getenv("NATS_NKEY") != null && System.getenv("NATS_NKEY") != "") {
44+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
45+
builder.authHandler(handler);
46+
} else if (System.getenv("NATS_CREDS") != null && System.getenv("NATS_CREDS") != "") {
47+
builder.authHandler(Nats.credentials(System.getenv("NATS_CREDS")));
48+
}
49+
50+
return builder.build();
51+
}
52+
}

src/examples/java/io/nats/examples/NatsDispatch.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.time.Duration;
1818
import java.util.concurrent.CountDownLatch;
1919

20-
import io.nats.client.AuthHandler;
2120
import io.nats.client.Connection;
2221
import io.nats.client.Dispatcher;
2322
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
2625
public class NatsDispatch {
2726

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

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

5252
try {
53-
54-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55-
56-
if (System.getenv("NATS_NKEY") != null) {
57-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58-
builder.authHandler(handler);
59-
}
60-
53+
System.out.println();
6154
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
62-
Connection nc = Nats.connect(builder.build());
55+
System.out.println();
56+
57+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
6358
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
6459

6560
Dispatcher d = nc.createDispatcher((msg) -> {

src/examples/java/io/nats/examples/NatsPub.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19-
import io.nats.client.AuthHandler;
2019
import io.nats.client.Connection;
2120
import io.nats.client.Nats;
2221
import io.nats.client.Options;
2322

2423
public class NatsPub {
2524

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

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

5050
try {
51-
52-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
51+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));
5352

54-
if (System.getenv("NATS_NKEY") != null) {
55-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
56-
builder.authHandler(handler);
57-
}
58-
59-
Connection nc = Nats.connect(builder.build());
53+
System.out.println();
54+
System.out.printf("Sending %s on %s, server is %s\n", message, subject, server);
55+
System.out.println();
6056
nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));
61-
nc.flush(Duration.ZERO);
57+
nc.flush(Duration.ofSeconds(5));
6258
nc.close();
6359

6460
} catch (Exception exp) {

src/examples/java/io/nats/examples/NatsQSub.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19-
import io.nats.client.AuthHandler;
2019
import io.nats.client.Connection;
2120
import io.nats.client.Message;
2221
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
2625
public class NatsQSub {
2726

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

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

5555
try {
56-
57-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
58-
59-
if (System.getenv("NATS_NKEY") != null) {
60-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
61-
builder.authHandler(handler);
62-
}
63-
64-
Connection nc = Nats.connect(builder.build());
56+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
6557
Subscription sub = nc.subscribe(subject, queue);
66-
nc.flush(Duration.ZERO);
58+
nc.flush(Duration.ofSeconds(5));
6759

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

src/examples/java/io/nats/examples/NatsReply.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.time.Duration;
1818
import java.util.concurrent.CountDownLatch;
1919

20-
import io.nats.client.AuthHandler;
2120
import io.nats.client.Connection;
2221
import io.nats.client.Dispatcher;
2322
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
2625
public class NatsReply {
2726

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

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

5252
try {
53-
54-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55-
56-
if (System.getenv("NATS_NKEY") != null) {
57-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58-
builder.authHandler(handler);
59-
}
60-
61-
Connection nc = Nats.connect(builder.build());
53+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
6254
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
6355

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

73-
nc.flush(Duration.ZERO);
66+
nc.flush(Duration.ofSeconds(5));
7467

7568
latch.await();
7669

src/examples/java/io/nats/examples/NatsReq.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import java.nio.charset.StandardCharsets;
1717
import java.util.concurrent.Future;
18+
import java.util.concurrent.TimeUnit;
1819

19-
import io.nats.client.AuthHandler;
2020
import io.nats.client.Connection;
2121
import io.nats.client.Message;
2222
import io.nats.client.Nats;
@@ -25,10 +25,11 @@
2525
public class NatsReq {
2626

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

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

5152
try {
52-
53-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
54-
55-
if (System.getenv("NATS_NKEY") != null) {
56-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
57-
builder.authHandler(handler);
58-
}
59-
60-
Connection nc = Nats.connect(builder.build());
53+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, false));
6154
Future<Message> replyFuture = nc.request(subject, message.getBytes(StandardCharsets.UTF_8));
62-
Message reply = replyFuture.get();
55+
Message reply = replyFuture.get(5, TimeUnit.SECONDS);
6356

57+
System.out.println();
6458
System.out.printf("Received reply \"%s\" on subject \"%s\"\n",
6559
new String(reply.getData(), StandardCharsets.UTF_8),
6660
reply.getSubject());
61+
System.out.println();
6762

6863
nc.close();
6964

src/examples/java/io/nats/examples/NatsSub.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19-
import io.nats.client.AuthHandler;
2019
import io.nats.client.Connection;
2120
import io.nats.client.Message;
2221
import io.nats.client.Nats;
@@ -26,10 +25,11 @@
2625
public class NatsSub {
2726

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

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

5252
try {
53-
54-
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55-
56-
if (System.getenv("NATS_NKEY") != null) {
57-
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58-
builder.authHandler(handler);
59-
}
60-
53+
System.out.println();
6154
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
62-
Connection nc = Nats.connect(builder.build());
55+
System.out.println();
56+
57+
Connection nc = Nats.connect(ExampleUtils.createExampleOptions(server, true));
6358
Subscription sub = nc.subscribe(subject);
64-
nc.flush(Duration.ZERO);
59+
nc.flush(Duration.ofSeconds(5));
6560

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

0 commit comments

Comments
 (0)