@Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(queueName); container.setMessageListener(listenerAdapter); return container; }
@Bean SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames(QUEUE_NAME); container.setMessageListener(listenerAdapter); return container; }
/** * Create a listener container from the connection factory (mandatory). * * @param connectionFactory the {@link ConnectionFactory} */ public SimpleMessageListenerContainer(ConnectionFactory connectionFactory) { setConnectionFactory(connectionFactory); }
@Test public void testPubSub() { final Channel channel = mock(Channel.class); Connection connection = mock(Connection.class); doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpTemplate amqpTemplate = mock(AmqpTemplate.class); PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel", container, amqpTemplate); amqpChannel.setBeanName("noSubscribersChannel"); amqpChannel.setBeanFactory(mock(BeanFactory.class)); amqpChannel.afterPropertiesSet(); List<String> logList = insertMockLoggerInListener(amqpChannel); MessageListener listener = (MessageListener) container.getMessageListener(); listener.onMessage(new Message("Hello world!".getBytes(), null)); verifyLogReceived(logList); }
/** * Create a listener container from the connection factory (mandatory). * * @param connectionFactory the {@link ConnectionFactory} */ public SimpleMessageListenerContainer(ConnectionFactory connectionFactory) { setConnectionFactory(connectionFactory); }
@SuppressWarnings("unchecked") @Test public void testPtP() throws Exception { final Channel channel = mock(Channel.class); DeclareOk declareOk = mock(DeclareOk.class); when(declareOk.getQueue()).thenReturn("noSubscribersChannel"); when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())) .thenReturn(declareOk); Connection connection = mock(Connection.class); doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpTemplate amqpTemplate = mock(AmqpTemplate.class); PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate); amqpChannel.setBeanName("noSubscribersChannel"); amqpChannel.setBeanFactory(mock(BeanFactory.class)); amqpChannel.afterPropertiesSet(); MessageListener listener = (MessageListener) container.getMessageListener(); try { listener.onMessage(new Message("Hello world!".getBytes(), null)); fail("Exception expected"); } catch (MessageDeliveryException e) { assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'.")); } }
@Test public void testInt2809JavaTypePropertiesFromAmqp() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container); PollableChannel channel = new QueueChannel(); adapter.setOutputChannel(channel); adapter.setBeanFactory(mock(BeanFactory.class)); adapter.afterPropertiesSet(); Object payload = new Foo("bar1"); MessageProperties amqpMessageProperties = new MessageProperties(); org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties); ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener(); listener.onMessage(amqpMessage, null); Message<?> receive = channel.receive(1000); Message<?> result = new JsonToObjectTransformer().transform(receive); assertEquals(payload, result.getPayload()); }
when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpInboundGateway adapter = new AmqpInboundGateway(container); QueueChannel outputChannel = new QueueChannel();
@Test public void testAdapterConversionError() throws Exception { Connection connection = mock(Connection.class); doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container); QueueChannel outputChannel = new QueueChannel(); adapter.setOutputChannel(outputChannel); QueueChannel errorChannel = new QueueChannel(); adapter.setErrorChannel(errorChannel); adapter.setMessageConverter(new SimpleMessageConverter() { @Override public Object fromMessage(org.springframework.amqp.core.Message message) throws MessageConversionException { throw new MessageConversionException("intended"); } }); adapter.afterPropertiesSet(); ((ChannelAwareMessageListener) container.getMessageListener()) .onMessage(mock(org.springframework.amqp.core.Message.class), null); assertNull(outputChannel.receive(0)); Message<?> received = errorChannel.receive(0); assertNotNull(received); assertNotNull(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE)); }
when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
when(connectionFactory.createConnection()).thenReturn(connection); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
@Bean public SimpleMessageListenerContainer replyListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(pushConnectionFactory()); container.setQueues(pushQueue()); container.setMessageListener(pushListener()); Advice[] adviceChain = new Advice[] { retryAdvice() }; container.setAdviceChain(adviceChain); return container; }
@Bean public SimpleMessageListenerContainer listenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory()); container.setQueueNames(this.helloWorldQueueName); container.setMessageListener(new MessageListenerAdapter(new HelloWorldHandler())); return container; }
public SimpleMessageListenerContainer createDefaultContainer(String containerName, ConnectionFactory connectionFactory, MessageConverter messageConverter, Object messageHandler) { final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setAcknowledgeMode(AcknowledgeMode.AUTO); container.setAdviceChain(new Advice[] {DefaultRetryPolicyFactory.makeContainerListenerRetryPolicy()}); container.setErrorHandler(errorHandler(containerName)); container.setMessageConverter(messageConverter); container.setMessageListener(new MessageListenerAdapter(messageHandler, messageConverter)); return container; }
/** * @return The reply listener container - the rabbit template is the listener. */ @Bean public SimpleMessageListenerContainer replyListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(replyQueue()); container.setMessageListener(fixedReplyQRabbitTemplate()); return container; }
/** * @return The reply listener container - the rabbit template is the listener. */ @Bean public SimpleMessageListenerContainer replyListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(replyQueue()); container.setMessageListener(fixedReplyQRabbitTemplate()); return container; }
/** * @return The listener container that handles the dead letter. */ @Bean public SimpleMessageListenerContainer dlListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(dlq()); container.setMessageListener(new MessageListenerAdapter(deadListener())); return container; }
/** * @return The reply listener container - the rabbit template with the wrong queue is the listener. */ @Bean public SimpleMessageListenerContainer replyListenerContainerWrongQueue() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(replyQueue()); container.setMessageListener(fixedReplyQRabbitTemplateWrongQueue()); container.setAutoStartup(false); return container; }
/** * @return The listener container that handles the request and returns the reply. */ @Bean public SimpleMessageListenerContainer serviceListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(requestQueue()); container.setMessageListener(new MessageListenerAdapter(new PojoListener())); return container; }
/** * @return The listener container that handles the request and returns the reply. */ @Bean public SimpleMessageListenerContainer serviceListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(rabbitConnectionFactory()); container.setQueues(requestQueue()); container.setMessageListener(new MessageListenerAdapter(new PojoListener())); return container; }