question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connecting to NATS cluster (pub/sub) not working

See original GitHub issue

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 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:closed
  • Created 7 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kozloviccommented, Jan 24, 2017

@magdkudama If your test program is as described above, you are setting the server URLs to:

String[] servers = new String[] {
        "nats://localhost:7244",
        "nats://localhost:7245",
        "nats://localhost:7246"
    };

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 the nc.subscribe() call and notify the main thread that you reached that point before starting the publisher thread.

0reactions
magdkudamacommented, Jan 25, 2017

That sounds good, thanks both for your help.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found