Skip to content

Commit df42f24

Browse files
authored
NatsClient refactor (eugenp#3914)
1 parent 5da2ea9 commit df42f24

1 file changed

Lines changed: 26 additions & 54 deletions

File tree

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
package com.baeldung.jnats;
22

3-
import io.nats.client.*;
4-
5-
import java.io.IOException;
6-
import java.util.*;
7-
3+
import io.nats.client.AsyncSubscription;
4+
import io.nats.client.Connection;
5+
import io.nats.client.Message;
6+
import io.nats.client.Nats;
7+
import io.nats.client.Options;
8+
import io.nats.client.Subscription;
9+
import io.nats.client.SyncSubscription;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
1012

11-
public class NatsClient {
13+
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
public final class NatsClient {
1218

13-
private String serverURI;
19+
private final String serverURI;
1420

15-
private Connection natsConnection;
21+
private final Connection natsConnection;
1622

17-
private Map<String, Subscription> subscriptions = new HashMap<>();
23+
private final Map<String, Subscription> subscriptions = new HashMap<>();
1824

1925
private final static Logger log = LoggerFactory.getLogger(NatsClient.class);
2026

21-
public NatsClient() {
27+
NatsClient() {
2228
this.serverURI = "jnats://localhost:4222";
2329
natsConnection = initConnection(serverURI);
2430
}
2531

26-
2732
public NatsClient(String serverURI) {
28-
2933
if ((serverURI != null) && (!serverURI.isEmpty())) {
3034
this.serverURI = serverURI;
3135
} else {
@@ -40,59 +44,33 @@ public void closeConnection() {
4044
natsConnection.close();
4145
}
4246

43-
4447
private Connection initConnection(String uri) {
4548
try {
46-
4749
Options options = new Options.Builder()
48-
.errorCb(new ExceptionHandler() {
49-
@Override
50-
public void onException(NATSException ex) {
51-
log.error("Connection Exception: ", ex);
52-
}
53-
})
54-
.disconnectedCb(new DisconnectedCallback() {
55-
@Override
56-
public void onDisconnect(ConnectionEvent event) {
57-
log.error("Channel disconnected: {}", event.getConnection());
58-
}
59-
})
60-
.reconnectedCb(new ReconnectedCallback() {
61-
@Override
62-
public void onReconnect(ConnectionEvent event) {
63-
log.error("Reconnected to server: {}", event.getConnection());
64-
}
65-
})
50+
.errorCb(ex -> log.error("Connection Exception: ", ex))
51+
.disconnectedCb(event -> log.error("Channel disconnected: {}", event.getConnection()))
52+
.reconnectedCb(event -> log.error("Reconnected to server: {}", event.getConnection()))
6653
.build();
6754

6855
return Nats.connect(uri, options);
69-
7056
} catch (IOException ioe) {
7157
log.error("Error connecting to NATs! ", ioe);
7258
return null;
7359
}
7460
}
7561

76-
77-
public void publishMessage(String topic, String replyTo, String message) {
62+
void publishMessage(String topic, String replyTo, String message) {
7863
try {
79-
// Simple Publisher
8064
natsConnection.publish(topic, replyTo, message.getBytes());
8165
} catch (IOException ioe) {
8266
log.error("Error publishing message: {} to {} ", message, topic, ioe);
8367
}
8468
}
8569

86-
8770
public void subscribeAsync(String topic) {
8871

89-
// Simple Async Subscriber
90-
AsyncSubscription subscription = natsConnection.subscribe(topic, new MessageHandler() {
91-
@Override
92-
public void onMessage(Message msg) {
93-
log.info("Received message on {}", msg.getSubject());
94-
}
95-
});
72+
AsyncSubscription subscription = natsConnection.subscribe(
73+
topic, msg -> log.info("Received message on {}", msg.getSubject()));
9674

9775
if (subscription == null) {
9876
log.error("Error subscribing to {}", topic);
@@ -101,13 +79,11 @@ public void onMessage(Message msg) {
10179
}
10280
}
10381

104-
public SyncSubscription subscribeSync(String topic) {
105-
// Simple Sync Subscriber
82+
SyncSubscription subscribeSync(String topic) {
10683
return natsConnection.subscribe(topic);
10784
}
10885

10986
public void unsubscribe(String topic) {
110-
11187
try {
11288
Subscription subscription = subscriptions.get(topic);
11389

@@ -116,15 +92,12 @@ public void unsubscribe(String topic) {
11692
} else {
11793
log.error("{} not found. Unable to unsubscribe.", topic);
11894
}
119-
12095
} catch (IOException ioe) {
12196
log.error("Error unsubscribing from {} ", topic, ioe);
12297
}
12398
}
12499

125-
126-
public Message makeRequest(String topic, String request) {
127-
100+
Message makeRequest(String topic, String request) {
128101
try {
129102
return natsConnection.request(topic, request.getBytes(), 100);
130103
} catch (IOException | InterruptedException ioe) {
@@ -133,7 +106,7 @@ public Message makeRequest(String topic, String request) {
133106
}
134107
}
135108

136-
public void installReply(String topic, String reply) {
109+
void installReply(String topic, String reply) {
137110
natsConnection.subscribe(topic, message -> {
138111
try {
139112
natsConnection.publish(message.getReplyTo(), reply.getBytes());
@@ -143,8 +116,7 @@ public void installReply(String topic, String reply) {
143116
});
144117
}
145118

146-
public SyncSubscription joinQueueGroup(String topic, String queue) {
119+
SyncSubscription joinQueueGroup(String topic, String queue) {
147120
return natsConnection.subscribe(topic, queue);
148121
}
149-
150122
}

0 commit comments

Comments
 (0)