Search in sources :

Example 1 with Message

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

the class NatsClientLiveTest method givenRequest_ReplyReceived.

@Test
public void givenRequest_ReplyReceived() {
    NatsClient client = connectClient();
    client.installReply("salary.requests", "denied!");
    Message reply = client.makeRequest("salary.requests", "I need a raise.");
    assertNotNull("No message!", reply);
    assertEquals("denied!", new String(reply.getData()));
}
Also used : Message(io.nats.client.Message) Test(org.junit.Test)

Example 2 with Message

use of io.nats.client.Message 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 3 with Message

use of io.nats.client.Message 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 4 with Message

use of io.nats.client.Message 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 5 with Message

use of io.nats.client.Message 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)8 Test (org.junit.Test)5 SyncSubscription (io.nats.client.SyncSubscription)4 BeanContext (io.micronaut.context.BeanContext)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 StringUtils (io.micronaut.core.util.StringUtils)2 ExecutableMethod (io.micronaut.inject.ExecutableMethod)2 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)2 NatsConnection (io.micronaut.nats.annotation.NatsConnection)2 Subject (io.micronaut.nats.annotation.Subject)2 NatsMessageSerDes (io.micronaut.nats.serdes.NatsMessageSerDes)2 NatsMessageSerDesRegistry (io.micronaut.nats.serdes.NatsMessageSerDesRegistry)2 Singleton (jakarta.inject.Singleton)2 Map (java.util.Map)2 Optional (java.util.Optional)2 InterceptedMethod (io.micronaut.aop.InterceptedMethod)1 InterceptorBean (io.micronaut.aop.InterceptorBean)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 MethodInvocationContext (io.micronaut.aop.MethodInvocationContext)1 Cache (io.micronaut.caffeine.cache.Cache)1