Search in sources :

Example 1 with SyncSubscription

use of io.nats.client.SyncSubscription in project tutorials by eugenp.

the class NatsClientLiveTest method whenWildCardSubscription_andMatchTopic_MessageReceived.

@Test
public void whenWildCardSubscription_andMatchTopic_MessageReceived() throws Exception {
    NatsClient client = connectClient();
    SyncSubscription fooSubscription = client.subscribeSync("foo.*");
    client.publishMessage("foo.bar", "bar.foo", "hello there");
    Message message = fooSubscription.nextMessage(200);
    assertNotNull("No message!", message);
    assertEquals("hello there", new String(message.getData()));
}
Also used : SyncSubscription(io.nats.client.SyncSubscription) Message(io.nats.client.Message) Test(org.junit.Test)

Example 2 with SyncSubscription

use of io.nats.client.SyncSubscription in project tutorials by eugenp.

the class NatsClientLiveTest method givenMessageExchange_MessagesReceived.

@Test
public void givenMessageExchange_MessagesReceived() throws Exception {
    NatsClient client = connectClient();
    SyncSubscription fooSubscription = client.subscribeSync("foo.bar");
    SyncSubscription barSubscription = client.subscribeSync("bar.foo");
    client.publishMessage("foo.bar", "bar.foo", "hello there");
    Message message = fooSubscription.nextMessage(200);
    assertNotNull("No message!", message);
    assertEquals("hello there", new String(message.getData()));
    client.publishMessage(message.getReplyTo(), message.getSubject(), "hello back");
    message = barSubscription.nextMessage(200);
    assertNotNull("No message!", message);
    assertEquals("hello back", new String(message.getData()));
}
Also used : SyncSubscription(io.nats.client.SyncSubscription) Message(io.nats.client.Message) Test(org.junit.Test)

Example 3 with SyncSubscription

use of io.nats.client.SyncSubscription in project tutorials by eugenp.

the class NatsClientLiveTest method givenQueueMessage_OnlyOneReceived.

@Test
public void givenQueueMessage_OnlyOneReceived() throws Exception {
    NatsClient client = connectClient();
    SyncSubscription queue1 = client.joinQueueGroup("foo.bar.requests", "queue1");
    SyncSubscription queue2 = client.joinQueueGroup("foo.bar.requests", "queue1");
    client.publishMessage("foo.bar.requests", "queuerequestor", "foobar");
    List<Message> messages = new ArrayList<>();
    Message message = queue1.nextMessage(200);
    if (message != null)
        messages.add(message);
    message = queue2.nextMessage(200);
    if (message != null)
        messages.add(message);
    assertEquals(1, messages.size());
}
Also used : SyncSubscription(io.nats.client.SyncSubscription) Message(io.nats.client.Message) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with SyncSubscription

use of io.nats.client.SyncSubscription in project tutorials by eugenp.

the class NatsClientLiveTest method whenWildCardSubscription_andNotMatchTopic_NoMessageReceived.

@Test
public void whenWildCardSubscription_andNotMatchTopic_NoMessageReceived() throws Exception {
    NatsClient client = connectClient();
    SyncSubscription fooSubscription = client.subscribeSync("foo.*");
    client.publishMessage("foo.bar.plop", "bar.foo", "hello there");
    Message message = fooSubscription.nextMessage(200);
    assertNull("Got message!", message);
    SyncSubscription barSubscription = client.subscribeSync("foo.>");
    client.publishMessage("foo.bar.plop", "bar.foo", "hello there");
    message = barSubscription.nextMessage(200);
    assertNotNull("No message!", message);
    assertEquals("hello there", new String(message.getData()));
}
Also used : SyncSubscription(io.nats.client.SyncSubscription) Message(io.nats.client.Message) Test(org.junit.Test)

Aggregations

Message (io.nats.client.Message)4 SyncSubscription (io.nats.client.SyncSubscription)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)1