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

The following examples show how to use org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter. 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 File: MessageListenerRetryTest.java    From sinavi-jfw with 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 #2
Source File: MessageListenerRetryTest.java    From sinavi-jfw with 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 #3
Source File: MessageListenerRetryTest.java    From sinavi-jfw with 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 File: MessageListenerRetryTest.java    From sinavi-jfw with 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 #5
Source File: AmqpMessageBrokerConfiguration.java    From piper with 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 #6
Source File: BusConfig.java    From SpringCloud with 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 #7
Source File: SpringIntegrationConfig.java    From bearchoke with 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 #8
Source File: MessageListenerConfig_Pre_1_4_0.java    From pinpoint with 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 #9
Source File: AmqpConfig.java    From demo_springboot_rabbitmq with 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 #10
Source File: AmqpMessagingApplication.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: AmqpConfiguration.java    From SkyEye with 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 #12
Source File: RabbitAutoConfiguration.java    From easy-rabbitmq with 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 #13
Source File: BusConfig.java    From SpringCloud with 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 File: BusConfig.java    From SpringCloud with 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 #15
Source File: SpringIntegrationTest.java    From rabbitmq-mock with 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 #16
Source File: SpringIntegrationTest.java    From rabbitmq-mock with 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 #17
Source File: SpringRabbitMQITest.java    From java-specialagent with 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 #18
Source File: BusConfig.java    From JetfireCloud with 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 #19
Source File: AmqpApplication.java    From rabbitmq-mock with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example #20
Source File: SpringIntegrationTest.java    From rabbitmq-mock with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example #21
Source File: SpringRabbitMQTest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(final Receiver receiver) {
  return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example #22
Source File: RabbitMQService.java    From micro-service with MIT License 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
	return new MessageListenerAdapter(receiver, "receiveMessage");
}
 
Example #23
Source File: ScrapingResultConsumerConfiguration.java    From scraping-microservice-java-python-rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter messageListenerAdapter() {
    return new MessageListenerAdapter(scrapingResultHandler, jsonMessageConverter());
}
 
Example #24
Source File: EMSConfiguration.java    From generic-vnfm with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter_emsRegistrator() {
  if (registrator != null) return new MessageListenerAdapter(registrator, "registerFromEms");
  else return null;
}
 
Example #25
Source File: Application.java    From journeyFromMonolithToMicroservices with MIT License 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(EmailMessageReceiver receiver) {
    return new MessageListenerAdapter(receiver, "process");
}
 
Example #26
Source File: AmqpConfig.java    From demo_springboot_rabbitmq with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
    return new MessageListenerAdapter(receiver, "onMessage");
}
 
Example #27
Source File: BusReceiver.java    From JetfireCloud with Apache License 2.0 4 votes vote down vote up
@Bean
MessageListenerAdapter mqListenerAdapter() {
    log.info("new listener");
    return new MessageListenerAdapter(this);
}
 
Example #28
Source File: AmqpMessagingApplication.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter messageListenerAdapter(
		MessageSubscriber messageSubscriber, MessageConverter messageConverter) {
	return new MessageListenerAdapter(messageSubscriber, messageConverter);
}
 
Example #29
Source File: EventSubscriberConfiguration.java    From articles with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(EventSubscriber eventSubscriber) {
    return new MessageListenerAdapter(eventSubscriber, "receive");
}
 
Example #30
Source File: EventSubscriberConfiguration.java    From code-examples with MIT License 4 votes vote down vote up
@Bean
public MessageListenerAdapter listenerAdapter(EventSubscriber eventSubscriber) {
  return new MessageListenerAdapter(eventSubscriber, "receive");
}