Skip to content

Commit 7cbf32c

Browse files
authored
Merge pull request #256 from nats-io/2.6.0
2.6.0
2 parents 593037e + ad674a6 commit 7cbf32c

31 files changed

+927
-244
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ jdk:
77
- openjdk8
88
- openjdk9
99
- openjdk10
10+
- openjdk11
1011
before_script:
1112
- wget "https://github.com/nats-io/nats-server/releases/download/$nats_server_version/gnatsd-$nats_server_version-linux-amd64.zip"
1213
-O tmp.zip

CHANGELOG.md

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

22
# Change Log
33

4+
## Version 2.6.0
5+
6+
* [FIXED] - cleaned up use of "chain" instead of "creds"
7+
* [FIXED] - #255 - added special ioexception when possible to indicate an authentication problem on connect
8+
* [FIXED] - #252 - deprecated strings for username/pass/token and use char arrays instead, required changing some other code to CharBuffer
9+
* [ADDED] - Openjdk11 to travis build and updated gradle wrapper to 5.5
10+
* [ADDED] - an option to trace connect timing, including a test and example
11+
* [FIXED/ADDED] - #197 - the ability to use a single dispatcher for multiple message handlers, including multiple subscriptions on a single subject
12+
413
## Version 2.5.2
514

615
* [FIXED] - #244 - fixed an issue with parsing ipv6 addresses in the info JSON, added unit test for parser

README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ The java-nats client is provided in a single jar file, with a single external de
5252

5353
### Downloading the Jar
5454

55-
You can download the latest jar at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.5.2/jnats-2.5.2.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.5.2/jnats-2.5.2.jar).
55+
You can download the latest jar at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.6.0/jnats-2.6.0.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.6.0/jnats-2.6.0.jar).
5656

57-
The examples are available at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.5.2/jnats-2.5.2-examples.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.5.2/jnats-2.5.2-examples.jar).
57+
The examples are available at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.6.0/jnats-2.6.0-examples.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.6.0/jnats-2.6.0-examples.jar).
5858

5959
To use NKeys, you will need the ed25519 library, which can be downloaded at [https://repo1.maven.org/maven2/net/i2p/crypto/eddsa/0.3.0/eddsa-0.3.0.jar](https://repo1.maven.org/maven2/net/i2p/crypto/eddsa/0.3.0/eddsa-0.3.0.jar).
6060

@@ -64,7 +64,7 @@ The NATS client is available in the Maven central repository, and can be importe
6464

6565
```groovy
6666
dependencies {
67-
implementation 'io.nats:jnats:2.5.2'
67+
implementation 'io.nats:jnats:2.6.0'
6868
}
6969
```
7070

@@ -90,7 +90,7 @@ The NATS client is available on the Maven central repository, and can be importe
9090
<dependency>
9191
<groupId>io.nats</groupId>
9292
<artifactId>jnats</artifactId>
93-
<version>2.5.2</version>
93+
<version>2.6.0</version>
9494
</dependency>
9595
```
9696

@@ -194,7 +194,7 @@ The Java NATS library provides two mechanisms to listen for messages, three if y
194194
String response = new String(msg.getData(), StandardCharsets.UTF_8);
195195
```
196196

197-
2. A Dispatcher that will call application code in a background thread. Dispatchers can manage multiple subjects with a single thread and single callback.
197+
2. A Dispatcher that will call application code in a background thread. Dispatchers can manage multiple subjects with a single thread and shared callback.
198198

199199
```java
200200
Dispatcher d = nc.createDispatcher((msg) -> {
@@ -205,6 +205,18 @@ The Java NATS library provides two mechanisms to listen for messages, three if y
205205
d.subscribe("subject");
206206
```
207207

208+
A dispatcher can also accept individual callbacks for any given subscription.
209+
210+
```java
211+
Dispatcher d = nc.createDispatcher((msg) -> {});
212+
213+
Subscription s = d.subscribe("some.subject", (msg) -> {
214+
String response = new String(msg.getData(), StandardCharsets.UTF_8);
215+
System.out.println("Message received (up to 100 times): " + response);
216+
});
217+
d.unsubscribe(s, 100);
218+
```
219+
208220
## Advanced Usage
209221

210222
### TLS

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ plugins {
1313
// Update version here, repeated check-ins not into master will have snapshot on them
1414
// Be sure to update Nats.java with the latest version, the change log and the package-info.java
1515
def versionMajor = 2
16-
def versionMinor = 5
17-
def versionPatch = 2
16+
def versionMinor = 6
17+
def versionPatch = 0
1818
def versionModifier = ""
19-
def jarVersion = "2.5.2"
19+
def jarVersion = "2.6.0"
2020
def branch = System.getenv("TRAVIS_BRANCH");
2121

2222
def getVersionName = { ->
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2015-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.Consumer;
21+
import io.nats.client.ErrorListener;
22+
import io.nats.client.Nats;
23+
import io.nats.client.Options;
24+
25+
public class ConnectTime {
26+
static final String usageString =
27+
"\nUsage: java ConnectTime server\n"
28+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
29+
+ "\nSet the environment variable NATS_NKEY to use challenge response authentication by setting a file containing your private key.\n"
30+
+ "\nSet the environment variable NATS_CREDS to use JWT/NKey authentication by setting a file containing your user creds.\n"
31+
+ "\nUse the URL for user/pass/token authentication.\n";
32+
33+
public static Options createOptions(String server) throws Exception {
34+
Options.Builder builder = new Options.Builder().
35+
server(server).
36+
connectionTimeout(Duration.ofSeconds(5)).
37+
pingInterval(Duration.ofSeconds(10)).
38+
reconnectWait(Duration.ofSeconds(1)).
39+
maxReconnects(-1).
40+
traceConnection();
41+
42+
43+
builder = builder.connectionListener((conn, type) -> System.out.println("Status change "+type));
44+
45+
builder = builder.errorListener(new ErrorListener() {
46+
@Override
47+
public void slowConsumerDetected(Connection conn, Consumer consumer) {
48+
System.out.println("NATS connection slow consumer detected");
49+
}
50+
51+
@Override
52+
public void exceptionOccurred(Connection conn, Exception exp) {
53+
System.out.println("NATS connection exception occurred");
54+
exp.printStackTrace();
55+
}
56+
57+
@Override
58+
public void errorOccurred(Connection conn, String error) {
59+
System.out.println("NATS connection error occurred " + error);
60+
}
61+
});
62+
63+
if (System.getenv("NATS_NKEY") != null && System.getenv("NATS_NKEY") != "") {
64+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
65+
builder.authHandler(handler);
66+
} else if (System.getenv("NATS_CREDS") != null && System.getenv("NATS_CREDS") != "") {
67+
builder.authHandler(Nats.credentials(System.getenv("NATS_CREDS")));
68+
}
69+
70+
return builder.build();
71+
}
72+
73+
public static void main(String args[]) {
74+
String server;
75+
76+
if (args.length == 1) {
77+
server = args[0];
78+
} else {
79+
usage();
80+
return;
81+
}
82+
83+
try {
84+
System.out.println();
85+
System.out.printf("Timing connect time to %s.\n", server);
86+
System.out.println();
87+
88+
Options options = createOptions(server);
89+
90+
long start = System.nanoTime();
91+
Connection nc = Nats.connect(options);
92+
long end = System.nanoTime();
93+
double seconds = ((double)(end - start)) / 1_000_000_000.0;
94+
System.out.printf("Connect time to %s was %.3f seconds\n", server, seconds);
95+
96+
nc.close();
97+
98+
} catch (Exception exp) {
99+
exp.printStackTrace();
100+
}
101+
}
102+
103+
static void usage() {
104+
System.err.println(usageString);
105+
System.exit(-1);
106+
}
107+
}

src/examples/java/io/nats/examples/ExampleUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.nats.client.AuthHandler;
1919
import io.nats.client.Connection;
2020
import io.nats.client.ConnectionListener;
21+
import io.nats.client.Consumer;
22+
import io.nats.client.ErrorListener;
2123
import io.nats.client.Nats;
2224
import io.nats.client.Options;
2325

@@ -28,6 +30,19 @@ public static Options createExampleOptions(String server, boolean allowReconnect
2830
connectionTimeout(Duration.ofSeconds(5)).
2931
pingInterval(Duration.ofSeconds(10)).
3032
reconnectWait(Duration.ofSeconds(1)).
33+
errorListener(new ErrorListener(){
34+
public void exceptionOccurred(Connection conn, Exception exp) {
35+
System.out.println("Exception " + exp.getMessage());
36+
}
37+
38+
public void errorOccurred(Connection conn, String type) {
39+
System.out.println("Error " + type);
40+
}
41+
42+
public void slowConsumerDetected(Connection conn, Consumer consumer) {
43+
System.out.println("Slow consumer");
44+
}
45+
}).
3146
connectionListener(new ConnectionListener(){
3247
public void connectionEvent(Connection conn, Events type) {
3348
System.out.println("Status change "+type);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.client;
15+
16+
import java.io.IOException;
17+
18+
/**
19+
* AuthenticationException is used when the connect process fails due to an authentication
20+
* problem.
21+
*
22+
* The exception will not include the authentication tokens, but as a subclass
23+
* of IOException allows the client to distinguish an IO problem from an
24+
* authentication problem.
25+
*/
26+
public class AuthenticationException extends IOException {
27+
28+
private static final long serialVersionUID = 1L;
29+
30+
/**
31+
* Create a new AuthenticationException.
32+
*
33+
* @param errorMessage the error message, see Exception for details
34+
*/
35+
public AuthenticationException(String errorMessage) {
36+
super(errorMessage);
37+
}
38+
}

0 commit comments

Comments
 (0)