@Override public void subscribe() { // do nothing if already subscribed if (subs != null) { return; } try { ensureConnected(); // Create subject/queue subscription if the queue has been provided if (StringUtils.isNotEmpty(queue)) { logger.info("No subscription. Creating a queue subscription. subject={}, queue={}", subject, queue); subs = conn.subscribe(subject, queue, msg -> onMessage(msg.getSubject(), msg.getData())); } else { logger.info("No subscription. Creating a pub/sub subscription. subject={}", subject); subs = conn.subscribe(subject, msg -> onMessage(msg.getSubject(), msg.getData())); } } catch (Exception ex) { logger.error("Subscription failed with " + ex.getMessage() + " for queueURI " + queueURI, ex); } }
/** * Subscribes to the configured inbox subject. * * @throws TTransportException if unable to open the transport */ @Override public void open() throws TTransportException { if (conn.getState() != Nats.ConnState.CONNECTED) { throw getClosedConditionException(conn.getState(), "open:"); } if (sub != null) { throw new TTransportException(TTransportExceptionType.ALREADY_OPEN, "NATS transport already open"); } sub = conn.subscribe(inbox, new Handler()); }
@Test(expected = IllegalStateException.class) public void testUnsubscribeInAnotherThread() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); Thread t = new Thread(() -> { sub.unsubscribe(); }); t.start(); sub.nextMessage(Duration.ofMillis(5000)); // throw assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe("subject", ""); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe(null, "quque"); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe(null); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe(""); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe("subject", null); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe("", "quque"); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsOnSubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.close(); nc.subscribe("subject"); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsOnUnsubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Subscription sub = nc.subscribe("subject"); nc.close(); sub.unsubscribe(); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testAutoUnsubscribe() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject").unsubscribe(1); nc.publish("subject", new byte[16]); Message msg = sub.nextMessage(Duration.ofMillis(500)); // should get 1 assertNotNull(msg); msg = sub.nextMessage(Duration.ofMillis(500)); // Will throw an exception assertFalse(true); } }
@Test public void testManualRequestReply() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { nc.publish(msg.getReplyTo(), msg.getData()); }); d.subscribe("request"); Subscription sub = nc.subscribe("reply"); nc.publish("request", "reply", "hello".getBytes(StandardCharsets.UTF_8)); Message msg = sub.nextMessage(Duration.ofMillis(400)); assertNotNull(msg); assertEquals("hello", new String(msg.getData(), StandardCharsets.UTF_8)); } }
@Test(expected = IllegalStateException.class) public void testAutoUnsubAfterMaxIsReached() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { int msgCount = 10; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } nc.flush(Duration.ofMillis(1000)); // Slow things down so we have time to unsub for (int i = 0; i < msgCount; i++) { sub.nextMessage(null); } sub.unsubscribe(msgCount); // we already have that many sub.nextMessage(Duration.ofMillis(500)); // Will throw an exception assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testMultiAutoUnsubscribe() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { int msgCount = 10; Message msg = null; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject").unsubscribe(msgCount); for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } for (int i = 0; i < msgCount; i++) { msg = sub.nextMessage(Duration.ofMillis(500)); // should get 1 assertNotNull(msg); } msg = sub.nextMessage(Duration.ofMillis(500)); // Will throw an exception assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testUnsubscribe() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); nc.publish("subject", new byte[16]); Message msg = sub.nextMessage(Duration.ofMillis(500)); assertNotNull(msg); sub.unsubscribe(); assertFalse(sub.isActive()); msg = sub.nextMessage(Duration.ofMillis(500)); // Will throw an exception assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testOnlyOneAutoUnsubscribe() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject").unsubscribe(1); nc.publish("subject", new byte[16]); Message msg = sub.nextMessage(Duration.ofMillis(500)); // should get 1 assertNotNull(msg); sub.unsubscribe(); // Will throw an exception assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testOnlyOneUnsubscribe() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); sub.unsubscribe(); sub.unsubscribe(); // Will throw an exception assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsOnAutoUnsubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); nc.close(); sub.unsubscribe(1); assertFalse(true); } }
@Test public void testSingleMessage() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); nc.publish("subject", new byte[16]); Message msg = sub.nextMessage(Duration.ofMillis(500)); assertTrue(sub.isActive()); assertEquals("subject", msg.getSubject()); assertEquals(sub, msg.getSubscription()); assertNull(msg.getReplyTo()); assertEquals(16, msg.getData().length); } }