Search in sources :

Example 1 with MessageHandler

use of com.rabbitmq.stream.MessageHandler in project rabbitmq-stream-java-client by rabbitmq.

the class SuperStreamUsage method consumerSimple.

void consumerSimple() {
    Environment environment = Environment.builder().build();
    // tag::consumer-simple[]
    Consumer consumer = environment.consumerBuilder().superStream(// <1>
    "invoices").messageHandler((context, message) -> {
    // message processing
    }).build();
    // ...
    // <2>
    consumer.close();
// end::consumer-simple[]
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageHandler(com.rabbitmq.stream.MessageHandler) List(java.util.List) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) Collections(java.util.Collections) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment)

Example 2 with MessageHandler

use of com.rabbitmq.stream.MessageHandler in project spring-amqp by spring-projects.

the class StreamListenerContainerTests method testAdviceChain.

@Test
void testAdviceChain() throws Exception {
    Environment env = mock(Environment.class);
    ConsumerBuilder builder = mock(ConsumerBuilder.class);
    given(env.consumerBuilder()).willReturn(builder);
    AtomicReference<MessageHandler> handler = new AtomicReference<>();
    willAnswer(inv -> {
        handler.set(inv.getArgument(0));
        return null;
    }).given(builder).messageHandler(any());
    AtomicBoolean advised = new AtomicBoolean();
    MethodInterceptor advice = (inv) -> {
        advised.set(true);
        return inv.proceed();
    };
    StreamListenerContainer container = new StreamListenerContainer(env);
    container.setAdviceChain(advice);
    AtomicBoolean called = new AtomicBoolean();
    MessageListener ml = mock(MessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(ml).onMessage(any());
    container.setupMessageListener(ml);
    Message message = mock(Message.class);
    given(message.getBodyAsBinary()).willReturn("foo".getBytes());
    Context context = mock(Context.class);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    advised.set(false);
    called.set(false);
    ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(cal).onMessage(any(), isNull());
    container.setupMessageListener(cal);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    called.set(false);
    StreamMessageListener sml = mock(StreamMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(sml).onStreamMessage(message, context);
    container.setupMessageListener(sml);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) MessageListener(org.springframework.amqp.core.MessageListener) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Context(com.rabbitmq.stream.MessageHandler.Context) BDDMockito.given(org.mockito.BDDMockito.given) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Mockito.mock(org.mockito.Mockito.mock) Context(com.rabbitmq.stream.MessageHandler.Context) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) MessageListener(org.springframework.amqp.core.MessageListener) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Environment(com.rabbitmq.stream.Environment) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

Environment (com.rabbitmq.stream.Environment)2 Message (com.rabbitmq.stream.Message)2 MessageHandler (com.rabbitmq.stream.MessageHandler)2 Consumer (com.rabbitmq.stream.Consumer)1 ConsumerBuilder (com.rabbitmq.stream.ConsumerBuilder)1 Context (com.rabbitmq.stream.MessageHandler.Context)1 Producer (com.rabbitmq.stream.Producer)1 RoutingStrategy (com.rabbitmq.stream.RoutingStrategy)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.isNull (org.mockito.ArgumentMatchers.isNull)1 BDDMockito.given (org.mockito.BDDMockito.given)1 BDDMockito.willAnswer (org.mockito.BDDMockito.willAnswer)1