io.nats.client.MessageHandler Java Examples

The following examples show how to use io.nats.client.MessageHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataFlowHandler.java    From nats-connector-framework with Apache License 2.0 6 votes vote down vote up
public void subscribe(String subject, String queue, MessageHandler handler) throws Exception {

        if (subject == null)
            return;

        synchronized (pluginLock) {
            logger.debug("Plugin subscribe to '{}', queue '{}'.", subject,
                    (queue == null ? "(none)" : queue));

            // do not subscribe twice.
            if (subscriptions.containsKey(subject)) {
                logger.debug("Subscription already exists.");
                return;
            }

            AsyncSubscription s;

            if (queue == null)
                s = connection.subscribe(subject, handler);
            else
                s = connection.subscribe(subject, queue, handler);

            subscriptions.put(subject, s);
        }
    }
 
Example #2
Source File: DataFlowHandler.java    From nats-connector-framework with Apache License 2.0 6 votes vote down vote up
public void subscribe(String subject, String queue, MessageHandler handler) throws Exception {

        if (subject == null)
            return;

        synchronized (pluginLock) {
            logger.debug("Plugin subscribe to '{}', queue '{}'.", subject,
                    (queue == null ? "(none)" : queue));

            // do not subscribe twice.
            if (subscriptions.containsKey(subject)) {
                logger.debug("Subscription already exists.");
                return;
            }

            AsyncSubscription s;

            if (queue == null)
                s = connection.subscribe(subject, handler);
            else
                s = connection.subscribe(subject, queue, handler);

            subscriptions.put(subject, s);
        }
    }
 
Example #3
Source File: NATSTestPlugin.java    From nats-connector-framework with Apache License 2.0 5 votes vote down vote up
private void testConnectorAPIs() throws Exception {
    MessageHandler mh = new MessageHandler() {
        @Override
        public void onMessage(Message message) {
            ;;
        }
    };

    // test various combinatitions of subscribes and unsubscribes.
    connector.subscribe("foo1");
    connector.subscribe("foo1");

    connector.subscribe("foo2", mh);
    connector.subscribe("foo2", mh);

    connector.subscribe("foo3", "qg1");
    connector.subscribe("foo3", "qg1");

    connector.subscribe("foo4", "qg1", mh);
    connector.unsubscribe("foo1");
    connector.unsubscribe("foo2");
    connector.unsubscribe("foo3");

    connector.unsubscribe("foo4");
    connector.unsubscribe("foo4");

    connector.unsubscribe("unknown");
    connector.unsubscribe(null);

    connector.flush();

    Connection c = connector.getConnection();
    if (c == null)
        throw new Exception("Expected a valid connection.");

    ConnectionFactory cf = connector.getConnectionFactory();
    if (cf == null)
        throw new Exception("Expected a valid connection.");
}
 
Example #4
Source File: NATSTestPlugin.java    From nats-connector-framework with Apache License 2.0 5 votes vote down vote up
private void testConnectorAPIs() throws Exception {
    MessageHandler mh = new MessageHandler() {
        @Override
        public void onMessage(Message message) {
            ;;
        }
    };

    // test various combinatitions of subscribes and unsubscribes.
    connector.subscribe("foo1");
    connector.subscribe("foo1");

    connector.subscribe("foo2", mh);
    connector.subscribe("foo2", mh);

    connector.subscribe("foo3", "qg1");
    connector.subscribe("foo3", "qg1");

    connector.subscribe("foo4", "qg1", mh);
    connector.unsubscribe("foo1");
    connector.unsubscribe("foo2");
    connector.unsubscribe("foo3");

    connector.unsubscribe("foo4");
    connector.unsubscribe("foo4");

    connector.unsubscribe("unknown");
    connector.unsubscribe(null);

    connector.flush();

    Connection c = connector.getConnection();
    if (c == null)
        throw new Exception("Expected a valid connection.");

    ConnectionFactory cf = connector.getConnectionFactory();
    if (cf == null)
        throw new Exception("Expected a valid connection.");
}
 
Example #5
Source File: NatsConnection.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public Dispatcher createDispatcher(MessageHandler handler) {
    if (isClosed()) {
        throw new IllegalStateException("Connection is Closed");
    } else if (isDraining()) {
        throw new IllegalStateException("Connection is Draining");
    }

    NatsDispatcher dispatcher = new NatsDispatcher(this, handler);
    String id = this.nuid.next();
    this.dispatchers.put(id, dispatcher);
    dispatcher.start(id);
    return dispatcher;
}
 
Example #6
Source File: NatsDispatcher.java    From nats.java with Apache License 2.0 5 votes vote down vote up
NatsDispatcher(NatsConnection conn, MessageHandler handler) {
    super(conn);
    this.defaultHandler = handler;
    this.incoming = new MessageQueue(true);
    this.subscriptionsUsingDefaultHandler = new ConcurrentHashMap<>();
    this.subscriptionsWithHandlers = new ConcurrentHashMap<>();
    this.subscriptionHandlers = new ConcurrentHashMap<>();
    this.running = new AtomicBoolean(false);
    this.waitForMessage = Duration.ofMinutes(5); // This can be long since we aren't doing anything
}
 
Example #7
Source File: NatsDispatcher.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public Subscription subscribe(String subject, MessageHandler handler) {
    if (subject == null || subject.length() == 0) {
        throw new IllegalArgumentException("Subject is required in subscribe");
    }

    if (handler == null) {
        throw new IllegalArgumentException("MessageHandler is required in subscribe");
    }
    return this.subscribeImpl(subject, null, handler);
}
 
Example #8
Source File: NatsDispatcher.java    From nats.java with Apache License 2.0 5 votes vote down vote up
public Subscription subscribe(String subject, String queueName,  MessageHandler handler) {
    if (subject == null || subject.length() == 0) {
        throw new IllegalArgumentException("Subject is required in subscribe");
    }

    if (queueName == null || queueName.length() == 0) {
        throw new IllegalArgumentException("QueueName is required in subscribe");
    }

    if (handler == null) {
        throw new IllegalArgumentException("MessageHandler is required in subscribe");
    }
    return this.subscribeImpl(subject, queueName, handler);
}
 
Example #9
Source File: DispatcherTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void testThrowOnNullHandler() throws IOException, InterruptedException, TimeoutException {
    try (NatsTestServer ts = new NatsTestServer(false);
                Connection nc = Nats.connect(ts.getURI())) {
        Dispatcher d = nc.createDispatcher((msg) -> {});
        d.subscribe("test", (MessageHandler)null);
        assertFalse(true);
    }
}
 
Example #10
Source File: DispatcherTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void testThrowOnNullHandlerWithQueue() throws IOException, InterruptedException, TimeoutException {
    try (NatsTestServer ts = new NatsTestServer(false);
                Connection nc = Nats.connect(ts.getURI())) {
        Dispatcher d = nc.createDispatcher((msg) -> {});
        d.subscribe("test", "queue", (MessageHandler)null);
        assertFalse(true);
    }
}
 
Example #11
Source File: DataFlowHandler.java    From nats-connector-framework with Apache License 2.0 4 votes vote down vote up
public void subscribe(String subject, MessageHandler handler) throws Exception {
    subscribe(subject, null, handler);
}
 
Example #12
Source File: DataFlowHandler.java    From nats-connector-framework with Apache License 2.0 4 votes vote down vote up
public void subscribe(String subject, MessageHandler handler) throws Exception {
    subscribe(subject, null, handler);
}
 
Example #13
Source File: NATSConnector.java    From nats-connector-framework with Apache License 2.0 2 votes vote down vote up
/***
 * Adds interest in a NATS subject, with a custom handle.
 * @param subject - subject of interest.
 * @param handler - message handler
 * @throws Exception - an error occurred in the subsciption process.
 */
public void subscribe(String subject, MessageHandler handler) throws Exception;
 
Example #14
Source File: NATSConnector.java    From nats-connector-framework with Apache License 2.0 2 votes vote down vote up
/***
 * Adds interest in a NATS subject with a queue group, with a custom handler.
 * @param subject - subject of interest.
 * @param queue - work queue
 * @param handler - message handler
 * @throws Exception - an error occurred in the subsciption process.
 */
public void subscribe(String subject, String queue, MessageHandler handler) throws Exception;
 
Example #15
Source File: NATSConnector.java    From nats-connector-framework with Apache License 2.0 2 votes vote down vote up
/***
 * Adds interest in a NATS subject, with a custom handle.
 * @param subject - subject of interest.
 * @param handler - message handler
 * @throws Exception - an error occurred in the subsciption process.
 */
public void subscribe(String subject, MessageHandler handler) throws Exception;
 
Example #16
Source File: NATSConnector.java    From nats-connector-framework with Apache License 2.0 2 votes vote down vote up
/***
 * Adds interest in a NATS subject with a queue group, with a custom handler.
 * @param subject - subject of interest.
 * @param queue - work queue
 * @param handler - message handler
 * @throws Exception - an error occurred in the subsciption process.
 */
public void subscribe(String subject, String queue, MessageHandler handler) throws Exception;