Connecting to NATS cluster (pub/sub) not working
See original GitHub issueHello,
I’ve integrated NATS Java in my project, and it’s all working fine when using a single NATS instance, but it’s not working when I’m using NATS Java with a cluster. I’ve ended up writing the simplest possible code. Please check this simple code:
public class NatsSimpleClusterExample {
public NatsSimpleClusterExample() {
String[] servers = new String[] {
"nats://localhost:7244",
"nats://localhost:7245",
"nats://localhost:7246"
};
Runnable consumer = () -> {
ConnectionFactory cf = new ConnectionFactory();
cf.setServers(servers);
try {
Connection nc = cf.createConnection();
System.out.println("Consumer connected to: " + nc.getConnectedUrl());
nc.subscribe("TEST", m -> System.out.println(new String(m.getData())));
} catch (Exception e) {
System.out.println(e.getMessage());
}
};
new Thread(consumer).start();
Runnable producer = () -> {
ConnectionFactory cf = new ConnectionFactory();
cf.setServers(servers);
try {
Connection nc = cf.createConnection();
System.out.println("Producer connected to: " + nc.getConnectedUrl());
long i = 0;
while (true) {
nc.publish("TEST", String.valueOf(i).getBytes());
if (i % 1000000 == 0) {
System.out.println("Published: " + i + " elements...");
}
i++;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
};
new Thread(producer).start();
}
public static void main(String[] args) {
new NatsSimpleClusterExample();
}
}
Result being:
Consumer connected to: nats://localhost:7246
Producer connected to: nats://localhost:7245
Published: 0 elements...
Published: 1000000 elements...
Published: 2000000 elements...
Published: 3000000 elements...
Published: 4000000 elements...
Published: 5000000 elements...
Published: 6000000 elements...
So the producer IS producing data, but the consumer IS NOT subscribing to it? Am I missing something here? My NATS instances are running and all looks good.
Thanks in advance for your help!
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Connecting to NATS cluster (pub/sub) not working #96 - GitHub
Hello, I've integrated NATS Java in my project, and it's all working fine when using a single NATS instance, but it's not working...
Read more >Secure Pub/Sub With NATS - ITNEXT
In this article, we will run a NATS server and secure it; so clients' connections are TLS encrypted, and end-users are authenticated with ......
Read more >NATS Server Clients - NATS Docs - NATS.io
A NATS client is an application making a connection to one of the nats servers pointed to by its connection URL, and uses...
Read more >Nats-streaming basic set up - Google Groups
I'm trying to run one basic pub sub using Nats-streaming. ... the message and I can see the connection created in debug logs...
Read more >NATS Messaging - ThinkMicroservices.com
How does NATS clustering work? NATS servers use the Gossip Protocol to share data across nodes. Each node must connect to at least...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@magdkudama If your test program is as described above, you are setting the server URLs to:
These are the ROUTE listen ports, not the clients, which based on your config files are
4222, 4223 and 4224
.Furthermore, since you are using 2 connections for subscribe and publish and running the subscriber first but in a thread, you should make sure that the subscription has reached the server before starting publishing (if you don’t want to miss ANY message). If that’s the case, you should do a
nc.flush()
after thenc.subscribe()
call and notify the main thread that you reached that point before starting the publisher thread.That sounds good, thanks both for your help.