NatsClient refactor (#3914)
This commit is contained in:
parent
5da2ea995c
commit
df42f243f2
@ -1,31 +1,35 @@
|
|||||||
package com.baeldung.jnats;
|
package com.baeldung.jnats;
|
||||||
|
|
||||||
import io.nats.client.*;
|
import io.nats.client.AsyncSubscription;
|
||||||
|
import io.nats.client.Connection;
|
||||||
import java.io.IOException;
|
import io.nats.client.Message;
|
||||||
import java.util.*;
|
import io.nats.client.Nats;
|
||||||
|
import io.nats.client.Options;
|
||||||
|
import io.nats.client.Subscription;
|
||||||
|
import io.nats.client.SyncSubscription;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class NatsClient {
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
private String serverURI;
|
public final class NatsClient {
|
||||||
|
|
||||||
private Connection natsConnection;
|
private final String serverURI;
|
||||||
|
|
||||||
private Map<String, Subscription> subscriptions = new HashMap<>();
|
private final Connection natsConnection;
|
||||||
|
|
||||||
|
private final Map<String, Subscription> subscriptions = new HashMap<>();
|
||||||
|
|
||||||
private final static Logger log = LoggerFactory.getLogger(NatsClient.class);
|
private final static Logger log = LoggerFactory.getLogger(NatsClient.class);
|
||||||
|
|
||||||
public NatsClient() {
|
NatsClient() {
|
||||||
this.serverURI = "jnats://localhost:4222";
|
this.serverURI = "jnats://localhost:4222";
|
||||||
natsConnection = initConnection(serverURI);
|
natsConnection = initConnection(serverURI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public NatsClient(String serverURI) {
|
public NatsClient(String serverURI) {
|
||||||
|
|
||||||
if ((serverURI != null) && (!serverURI.isEmpty())) {
|
if ((serverURI != null) && (!serverURI.isEmpty())) {
|
||||||
this.serverURI = serverURI;
|
this.serverURI = serverURI;
|
||||||
} else {
|
} else {
|
||||||
@ -40,59 +44,33 @@ public class NatsClient {
|
|||||||
natsConnection.close();
|
natsConnection.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Connection initConnection(String uri) {
|
private Connection initConnection(String uri) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
Options options = new Options.Builder()
|
Options options = new Options.Builder()
|
||||||
.errorCb(new ExceptionHandler() {
|
.errorCb(ex -> log.error("Connection Exception: ", ex))
|
||||||
@Override
|
.disconnectedCb(event -> log.error("Channel disconnected: {}", event.getConnection()))
|
||||||
public void onException(NATSException ex) {
|
.reconnectedCb(event -> log.error("Reconnected to server: {}", event.getConnection()))
|
||||||
log.error("Connection Exception: ", ex);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.disconnectedCb(new DisconnectedCallback() {
|
|
||||||
@Override
|
|
||||||
public void onDisconnect(ConnectionEvent event) {
|
|
||||||
log.error("Channel disconnected: {}", event.getConnection());
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.reconnectedCb(new ReconnectedCallback() {
|
|
||||||
@Override
|
|
||||||
public void onReconnect(ConnectionEvent event) {
|
|
||||||
log.error("Reconnected to server: {}", event.getConnection());
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return Nats.connect(uri, options);
|
return Nats.connect(uri, options);
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
log.error("Error connecting to NATs! ", ioe);
|
log.error("Error connecting to NATs! ", ioe);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void publishMessage(String topic, String replyTo, String message) {
|
||||||
public void publishMessage(String topic, String replyTo, String message) {
|
|
||||||
try {
|
try {
|
||||||
// Simple Publisher
|
|
||||||
natsConnection.publish(topic, replyTo, message.getBytes());
|
natsConnection.publish(topic, replyTo, message.getBytes());
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
log.error("Error publishing message: {} to {} ", message, topic, ioe);
|
log.error("Error publishing message: {} to {} ", message, topic, ioe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void subscribeAsync(String topic) {
|
public void subscribeAsync(String topic) {
|
||||||
|
|
||||||
// Simple Async Subscriber
|
AsyncSubscription subscription = natsConnection.subscribe(
|
||||||
AsyncSubscription subscription = natsConnection.subscribe(topic, new MessageHandler() {
|
topic, msg -> log.info("Received message on {}", msg.getSubject()));
|
||||||
@Override
|
|
||||||
public void onMessage(Message msg) {
|
|
||||||
log.info("Received message on {}", msg.getSubject());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (subscription == null) {
|
if (subscription == null) {
|
||||||
log.error("Error subscribing to {}", topic);
|
log.error("Error subscribing to {}", topic);
|
||||||
@ -101,13 +79,11 @@ public class NatsClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public SyncSubscription subscribeSync(String topic) {
|
SyncSubscription subscribeSync(String topic) {
|
||||||
// Simple Sync Subscriber
|
|
||||||
return natsConnection.subscribe(topic);
|
return natsConnection.subscribe(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unsubscribe(String topic) {
|
public void unsubscribe(String topic) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Subscription subscription = subscriptions.get(topic);
|
Subscription subscription = subscriptions.get(topic);
|
||||||
|
|
||||||
@ -116,15 +92,12 @@ public class NatsClient {
|
|||||||
} else {
|
} else {
|
||||||
log.error("{} not found. Unable to unsubscribe.", topic);
|
log.error("{} not found. Unable to unsubscribe.", topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
log.error("Error unsubscribing from {} ", topic, ioe);
|
log.error("Error unsubscribing from {} ", topic, ioe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Message makeRequest(String topic, String request) {
|
||||||
public Message makeRequest(String topic, String request) {
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return natsConnection.request(topic, request.getBytes(), 100);
|
return natsConnection.request(topic, request.getBytes(), 100);
|
||||||
} catch (IOException | InterruptedException ioe) {
|
} catch (IOException | InterruptedException ioe) {
|
||||||
@ -133,7 +106,7 @@ public class NatsClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void installReply(String topic, String reply) {
|
void installReply(String topic, String reply) {
|
||||||
natsConnection.subscribe(topic, message -> {
|
natsConnection.subscribe(topic, message -> {
|
||||||
try {
|
try {
|
||||||
natsConnection.publish(message.getReplyTo(), reply.getBytes());
|
natsConnection.publish(message.getReplyTo(), reply.getBytes());
|
||||||
@ -143,8 +116,7 @@ public class NatsClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public SyncSubscription joinQueueGroup(String topic, String queue) {
|
SyncSubscription joinQueueGroup(String topic, String queue) {
|
||||||
return natsConnection.subscribe(topic, queue);
|
return natsConnection.subscribe(topic, queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user