@Override public synchronized void start() { if (!this.running) { if (!this.lazyConnect && this.connectionFactory != null) { try { Connection connection = this.connectionFactory.createConnection(); // NOSONAR (close) if (connection != null) { connection.close(); } } catch (RuntimeException e) { logger.error("Failed to eagerly establish the connection.", e); } } doStart(); this.running = true; } }
@Test public void testInt2773WithOverrideToDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("overrideTemplateAttributesToEmpty", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testInt2773WithDefaultAmqpTemplateExchangeAndRoutingKey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("withDefaultAmqpTemplateExchangeAndRoutingKey", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq(""), Mockito.eq(""), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testInt2773UseDefaultAmqpTemplateExchangeAndRoutingLey() throws IOException { ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(mockConnection); PublisherCallbackChannelImpl publisherCallbackChannel = new PublisherCallbackChannelImpl(mockChannel, mock(ExecutorService.class)); when(mockConnection.createChannel(false)).thenReturn(publisherCallbackChannel); MessageChannel requestChannel = context.getBean("toRabbitOnlyWithTemplateChannel", MessageChannel.class); requestChannel.send(MessageBuilder.withPayload("test").build()); Mockito.verify(mockChannel, Mockito.times(1)).basicPublish(Mockito.eq("default.test.exchange"), Mockito.eq("default.routing.key"), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); }
@Test public void testInt3430FailForNotLazyConnect() { RabbitTemplate amqpTemplate = spy(new RabbitTemplate()); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); RuntimeException toBeThrown = new RuntimeException("Test Connection Exception"); doThrow(toBeThrown).when(connectionFactory).createConnection(); when(amqpTemplate.getConnectionFactory()).thenReturn(connectionFactory); AmqpOutboundEndpoint handler = new AmqpOutboundEndpoint(amqpTemplate); Log logger = spy(TestUtils.getPropertyValue(handler, "logger", Log.class)); new DirectFieldAccessor(handler).setPropertyValue("logger", logger); doNothing().when(logger).error("Failed to eagerly establish the connection.", toBeThrown); ApplicationContext context = mock(ApplicationContext.class); handler.setApplicationContext(context); handler.setBeanFactory(context); handler.afterPropertiesSet(); handler.start(); handler.stop(); verify(logger, never()).error(anyString(), any(RuntimeException.class)); handler.setLazyConnect(false); handler.start(); verify(logger).error("Failed to eagerly establish the connection.", toBeThrown); handler.stop(); }
/** * Create a RabbitMQ Connection via this template's ConnectionFactory and its host and port values. * @return the new RabbitMQ Connection * @throws IOException if thrown by RabbitMQ API methods * @see ConnectionFactory#createConnection */ protected Connection createConnection() throws IOException { return this.connectionFactory.createConnection(); }
@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); }
@Override public Connection createConnection() throws AmqpException { return this.defaultConnectionFactory.createConnection(); }
/** * Create a RabbitMQ Connection via this template's ConnectionFactory and its host and port values. * @return the new RabbitMQ Connection * @throws IOException if thrown by RabbitMQ API methods * @see ConnectionFactory#createConnection */ protected Connection createConnection() throws IOException { return this.connectionFactory.createConnection(); }
@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()); }
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);
@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)); }
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);
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);
@Override public Connection createConnection() throws AmqpException { return this.determineTargetConnectionFactory().createConnection(); }
@Override protected AbstractIntegrationMessageBuilder<Object> doReceive() { Connection connection = this.connectionFactory.createConnection(); // NOSONAR - RabbitUtils Channel channel = connection.createChannel(this.transacted); try {
@Bean public ConnectionFactory rabbitConnectionFactory() { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection mockConn = mock(Connection.class); when(mockCF.createConnection()).thenReturn(mockConn); Channel mockChannel = mock(Channel.class); when(mockChannel.isOpen()).thenReturn(true); when(mockConn.createChannel(anyBoolean())).thenReturn(mockChannel); return mockCF; }
public int getMessageCount(final String queue) throws IOException { Connection connection = connectionFactory.createConnection(); final Channel channel = connection.createChannel(false); final AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(queue); return declareOk.getMessageCount(); } }
@Bean public ConnectionFactory connectionFactory() throws IOException { ConnectionFactory factory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); willReturn(connection).given(factory).createConnection(); willReturn(channel).given(connection).createChannel(anyBoolean()); given(channel.isOpen()).willReturn(true); return factory; }