brainlag / JavaNSQClient

Fast Java client for NSQ
MIT License
90 stars 57 forks source link

Why Producer not lookup lookupd for produce connection like Consumer ? #21

Closed icanfly closed 8 years ago

icanfly commented 8 years ago

Why the implementation of producer not apply consumer model, get nsqd server address by lookup lookupd servers, then establish a connection for message sending, and decide which nsqd to send a message by scheduling lookup lookupd servers

like this: ` public NSQConsumer(final NSQLookup lookup, final String topic, final String channel, final NSQMessageCallback callback) { this(lookup, topic, channel, callback, new NSQConfig()); }

public NSQConsumer(final NSQLookup lookup, final String topic, final String channel, final NSQMessageCallback callback,
                   final NSQConfig config) {
    this(lookup, topic, channel, callback, config, null);
}

public NSQConsumer(final NSQLookup lookup, final String topic, final String channel, final NSQMessageCallback callback,
                   final NSQConfig config, final NSQErrorCallback errCallback) {
    this.lookup = lookup;
    this.topic = topic;
    this.channel = channel;
    this.config = config;
    this.callback = callback;
    this.errorCallback = errCallback;

}

` but Producer has initialized hardly like this:

` public class NSQProducer { private Set addresses = Sets.newConcurrentHashSet(); ...

protected Connection getConnection() throws NoConnectionsException { int c = 0; while (c < connectionRetries) { ServerAddress[] serverAddresses = addresses.toArray(new ServerAddress[addresses.size()]); if (serverAddresses.length != 0) { try { return pool.borrowObject(serverAddresses[roundRobinCount++ % serverAddresses.length]); } catch (NoSuchElementException e) { try { Thread.sleep(1000); } catch (InterruptedException ix) { throw new NoConnectionsException("Could not acquire a connection to a server", ix); } } catch (Exception ex) { throw new NoConnectionsException("Could not acquire a connection to a server", ex); } } } throw new IllegalStateException("No server configured for producer"); }

`

I think it's good for Increase/Decrease number of nsqd servers.

brainlag commented 8 years ago

There is a huge difference between Consumers and Producers. With Producers you want to control which nodes are used for a specific topic but as a Consumer you really don't have a choice because you want to consume the messages from all nodes of a specific topic. If you think that Producers should use lookupd then you can easily bolt it on yourself.