Java Code Examples for org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter

The following examples show how to use org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter. These examples are extracted from open source projects. 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 Project: piper   Source File: AmqpMessageBrokerConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
private void registerListenerEndpoint(RabbitListenerEndpointRegistrar aRegistrar, Queue aQueue, Exchange aExchange, int aConcurrency, Object aDelegate, String aMethodName) {
  admin(connectionFactory).declareQueue(aQueue);
  admin(connectionFactory).declareBinding(BindingBuilder.bind(aQueue)
                                                        .to(aExchange)
                                                        .with(aQueue.getName())
                                                        .noargs());
  
  MessageListenerAdapter messageListener = new MessageListenerAdapter(aDelegate);
  messageListener.setMessageConverter(jacksonAmqpMessageConverter(objectMapper));
  messageListener.setDefaultListenerMethod(aMethodName);

  SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint();
  endpoint.setId(aQueue.getName()+"Endpoint");
  endpoint.setQueueNames(aQueue.getName());
  endpoint.setMessageListener(messageListener);

  aRegistrar.registerEndpoint(endpoint,createContainerFactory(aConcurrency));
}
 
Example 2
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void 回復可能例外の場合はデフォルトの指定回数リトライが実行されて回復可能例外キューに配信される() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new ApplicationRecoverableExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    recoverableContainer.setQueues(ctx.getBean("recoverableExceptionQueue", Queue.class));
    recoverableContainer.setMessageListener(new MessageListenerAdapter(new RecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    recoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(retry.await(30, TimeUnit.SECONDS), is(true));
    assertThat(retry.getCount(), is(0L));
    assertThat(recover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(recover.getCount(), is(0L));
    container.stop();
    recoverableContainer.stop();
}
 
Example 3
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void アプリケーション回復不能例外の場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new ApplicationUnrecoverableExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example 4
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void システム例外の場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new SystemExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example 5
Source Project: sinavi-jfw   Source File: MessageListenerRetryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void RejectAndDontRequeue例外が発生した場合はリトライが実行されない() throws Exception {
    container.setQueues(ctx.getBean("retryTestQueue", Queue.class));
    container.setMessageListener(new MessageListenerAdapter(new AmqpRejectAndDontRequeueExceptionTestHandler(), ctx.getBean(MessageConverter.class)));
    unrecoverableContainer.setQueues(ctx.getBean("unrecoverableExceptionQueue", Queue.class));
    unrecoverableContainer.setMessageListener(new MessageListenerAdapter(new UnrecoverableTestHandler(), ctx.getBean(MessageConverter.class)));
    container.start();
    unrecoverableContainer.start();
    template.convertAndSend("retry.test.exchange", "retry.test.binding", new RetryTestBean("test"));
    assertThat(unretry.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unretry.getCount(), is(0L));
    assertThat(unrecover.await(3, TimeUnit.SECONDS), is(true));
    assertThat(unrecover.getCount(), is(0L));
    container.stop();
    unrecoverableContainer.stop();
}
 
Example 6
Source Project: easy-rabbitmq   Source File: RabbitAutoConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(MessageConverter converter,
    ConnectionFactory connectionFactory, @Autowired(required = false) List<Consumer<?>> list) {
  SimpleMessageListenerContainer container =
      new SimpleMessageListenerContainer(connectionFactory);
  init(list);
  Receiver receiver = new Receiver(converter);
  container.setMessageListener(new MessageListenerAdapter(receiver, converter));
  container.setQueueNames(queueNames.toArray(new String[0]));
  container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
  container.setPrefetchCount(prefetchCount);
  container.setTxSize(txSize);
  return container;
}
 
Example 7
Source Project: rabbitmq-mock   Source File: SpringIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void basic_consume_case() {
    String messageBody = "Hello world!";
    try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
        RabbitTemplate rabbitTemplate = queueAndExchangeSetup(context);

        Receiver receiver = new Receiver();
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(context.getBean(ConnectionFactory.class));
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(new MessageListenerAdapter(receiver, "receiveMessage"));
        try {
            container.start();

            rabbitTemplate.convertAndSend(EXCHANGE_NAME, "test.key2", messageBody);

            List<String> receivedMessages = new ArrayList<>();
            assertTimeoutPreemptively(ofMillis(500L), () -> {
                    while (receivedMessages.isEmpty()) {
                        receivedMessages.addAll(receiver.getMessages());
                        TimeUnit.MILLISECONDS.sleep(100L);
                    }
                }
            );

            assertThat(receivedMessages).containsExactly(messageBody);
        } finally {
            container.stop();
        }
    }
}
 
Example 8
Source Project: rabbitmq-mock   Source File: SpringIntegrationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void reply_direct_to() throws ExecutionException, InterruptedException {
    String messageBody = "Hello world!";
    try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AmqpConfiguration.class)) {
        RabbitTemplate rabbitTemplate = queueAndExchangeSetup(context);

        // using AsyncRabbitTemplate to avoid automatic fallback to temporary queue 
        AsyncRabbitTemplate asyncRabbitTemplate = new AsyncRabbitTemplate(rabbitTemplate);
        
        Receiver receiver = new Receiver();
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(context.getBean(ConnectionFactory.class));
        container.setQueueNames(QUEUE_NAME);
        container.setMessageListener(new MessageListenerAdapter(receiver, "receiveMessageAndReply"));
        try {
            container.start();
            asyncRabbitTemplate.start();

            AsyncRabbitTemplate.RabbitConverterFuture<Object> result = asyncRabbitTemplate.convertSendAndReceive(EXCHANGE_NAME, "test.key2", messageBody);
            
            assertThat(result.get()).isEqualTo(new StringBuilder(messageBody).reverse().toString());
            assertThat(receiver.getMessages()).containsExactly(messageBody);
        } finally {
            container.stop();
            asyncRabbitTemplate.stop();
        }
    }
}
 
Example 9
Source Project: java-specialagent   Source File: SpringRabbitMQITest.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(final Receiver receiver) {
  return new MessageListenerAdapter(receiver, "receiveMessage") {
    @Override
    public void onMessage(final Message message, final Channel channel) throws Exception {
      TestUtil.checkActiveSpan();
      super.onMessage(message, channel);
    }
  };
}
 
Example 10
Source Project: JetfireCloud   Source File: BusConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer mqContainer(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    log.info("init mqContainer");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(listenerAdapter);
    return container;
}
 
Example 11
Source Project: SpringCloud   Source File: BusConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer mqContainer(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
    log.info("init mqContainer");
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(QUEUE_NAME);
    container.setMessageListener(listenerAdapter);
    return container;
}
 
Example 12
Source Project: SpringCloud   Source File: BusConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter, Queue queue) {
    log.info("init simpleMessageListenerContainer {}", queue.getName());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 13
Source Project: SpringCloud   Source File: BusConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
SimpleMessageListenerContainer simpleMessageListenerContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter, Queue queue) {
    log.info("init simpleMessageListenerContainer: {}", queue.getName());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueueNames(queue.getName());
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 14
Source Project: SkyEye   Source File: AmqpConfiguration.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setQueues(this.queue);
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(this.maxConcurrentConsumers);
    container.setConcurrentConsumers(this.concurrentConsumers);
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setMessageListener(messageListenerAdapter);
    return container;
}
 
Example 15
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer(
		ConnectionFactory connectionFactory,
		MessageListenerAdapter listenerAdapter) {
	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
	container.setConnectionFactory(connectionFactory);
	container.setQueueNames("test.queue");
	container.setMessageListener(listenerAdapter);

	return container;
}
 
Example 16
Source Project: demo_springboot_rabbitmq   Source File: AmqpConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer messageListenerContainer(MessageListenerAdapter listenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueueNames(AmqpConfig.QUEUE_NAME);
    container.setExposeListenerChannel(true);
    container.setMaxConcurrentConsumers(1);
    container.setConcurrentConsumers(1);
    container.setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认
    container.setMessageListener(listenerAdapter);
    return container;
}
 
Example 17
Source Project: pinpoint   Source File: MessageListenerConfig_Pre_1_4_0.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory, TestMessageHandler testMessageHandler) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(RabbitMQTestConstants.QUEUE_PUSH);
    container.setMessageListener(new MessageListenerAdapter(testMessageHandler));
    return container;
}
 
Example 18
Source Project: bearchoke   Source File: SpringIntegrationConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Wraps our LoggingHandler in a MessageListenerAdapter to be used by a MessageListenerContainer
 * @return
 */
@Bean(name = "loggingMessageListenerAdapter")
public MessageListenerAdapter loggingMessageListenerAdapter() {
    MessageListenerAdapter adapter = new MessageListenerAdapter(loggingHandler());

    return adapter;
}
 
Example 19
Source Project: rabbitmq-mock   Source File: AmqpApplication.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example 20
Source Project: rabbitmq-mock   Source File: SpringIntegrationTest.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example 21
Source Project: java-specialagent   Source File: SpringRabbitMQTest.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(final Receiver receiver) {
  return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example 22
Source Project: JetfireCloud   Source File: BusReceiver.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter mqListenerAdapter() {
    log.info("new listener");
    return new MessageListenerAdapter(this);
}
 
Example 23
Source Project: articles   Source File: EventSubscriberConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(EventSubscriber eventSubscriber) {
    return new MessageListenerAdapter(eventSubscriber, "receive");
}
 
Example 24
Source Project: code-examples   Source File: EventSubscriberConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(EventSubscriber eventSubscriber) {
  return new MessageListenerAdapter(eventSubscriber, "receive");
}
 
Example 25
Source Project: code-examples   Source File: MessageConsumerConfiguration.java    License: MIT License 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(MessageConsumer messageConsumer) {
	return new MessageListenerAdapter(messageConsumer, "consumeStringMessage");
}
 
Example 26
Source Project: lemon-rabbitmq   Source File: MQConsumerConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(MQAwareListener listener, MessageConverter converter) {
    return new MessageListenerAdapter(listener, converter);
}
 
Example 27
Source Project: SpringCloud   Source File: BusReceiver.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter mqListenerAdapter() {
    log.info("new listener");
    return new MessageListenerAdapter(this);
}
 
Example 28
Source Project: SpringCloud   Source File: BusConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter messageListenerAdapter(BusReceiver busReceiver, MessageConverter messageConverter) {
    log.info("new listener");
    return new MessageListenerAdapter(busReceiver, messageConverter);
}
 
Example 29
Source Project: SpringCloud   Source File: BusConfig.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter messageListenerAdapter(BusReceiver busReceiver, MessageConverter messageConverter) {
    log.info("new listener");
    return new MessageListenerAdapter(busReceiver, messageConverter);
}
 
Example 30
@Bean
public MessageListenerAdapter messageListenerAdapter(
		MessageSubscriber messageSubscriber, MessageConverter messageConverter) {
	return new MessageListenerAdapter(messageSubscriber, messageConverter);
}