List of usage examples for com.rabbitmq.client Envelope Envelope
public Envelope(long deliveryTag, boolean redeliver, String exchange, String routingKey)
From source file:org.springframework.amqp.rabbit.listener.ExternalTxManagerTests.java
License:Apache License
/** * Verifies that an up-stack RabbitTemplate uses the listener's * channel (ChannelAwareMessageListener). *//*from w ww . j a va2 s.c o m*/ @SuppressWarnings("unchecked") @Test public void testChannelAwareMessageListener() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel onlyChannel = mock(Channel.class); when(onlyChannel.isOpen()).thenReturn(true); final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); return channel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(onlyChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(onlyChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Channel> exposed = new AtomicReference<Channel>(); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new ChannelAwareMessageListener() { @Override public void onMessage(Message message, Channel channel) { exposed.set(channel); RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setShutdownTimeout(100); container.setTransactionManager(new DummyTxManager()); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } verify(mockConnection, Mockito.times(1)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(onlyChannel).txCommit(); verify(onlyChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); // verify close() was never called on the channel verify(onlyChannel, Mockito.never()).close(); container.stop(); assertSame(onlyChannel, exposed.get()); }
From source file:org.springframework.amqp.rabbit.listener.ExternalTxManagerTests.java
License:Apache License
/** * Verifies that an up-stack RabbitTemplate uses the listener's * channel (ChannelAwareMessageListener). exposeListenerChannel=false * is ignored (ChannelAwareMessageListener). *//*from ww w. j ava2s .c o m*/ @SuppressWarnings("unchecked") @Test public void testChannelAwareMessageListenerDontExpose() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel onlyChannel = mock(Channel.class); when(onlyChannel.isOpen()).thenReturn(true); final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); return channel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(onlyChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(onlyChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Channel> exposed = new AtomicReference<Channel>(); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new ChannelAwareMessageListener() { @Override public void onMessage(Message message, Channel channel) { exposed.set(channel); RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setExposeListenerChannel(false); container.setShutdownTimeout(100); container.setTransactionManager(new DummyTxManager()); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } verify(mockConnection, Mockito.times(1)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(onlyChannel).txCommit(); verify(onlyChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); // verify close() was never called on the channel verify(onlyChannel, Mockito.never()).close(); container.stop(); assertSame(onlyChannel, exposed.get()); }
From source file:org.springframework.amqp.rabbit.listener.ExternalTxManagerTests.java
License:Apache License
/** * Verifies the proper channel is bound when using a RabbitTransactionManager. * Previously, the wrong channel was bound. See AMQP-260. * @throws Exception//from www. j a v a 2 s. c om */ @SuppressWarnings("unchecked") @Test public void testMessageListenerWithRabbitTxManager() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel onlyChannel = mock(Channel.class); when(onlyChannel.isOpen()).thenReturn(true); final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory( mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); return channel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(onlyChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(onlyChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cachingConnectionFactory); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setShutdownTimeout(100); container.setTransactionManager(new RabbitTransactionManager(cachingConnectionFactory)); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } verify(mockConnection, Mockito.times(1)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(onlyChannel).txCommit(); verify(onlyChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); // verify close() was never called on the channel DirectFieldAccessor dfa = new DirectFieldAccessor(cachingConnectionFactory); List<?> channels = (List<?>) dfa.getPropertyValue("cachedChannelsTransactional"); assertEquals(0, channels.size()); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.LocallyTransactedTests.java
License:Apache License
/** * Verifies that an up-stack RabbitTemplate uses the listener's * channel (MessageListener).//from w ww .j a v a 2 s. co m */ @SuppressWarnings("unchecked") @Test public void testMessageListener() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel onlyChannel = mock(Channel.class); when(onlyChannel.isOpen()).thenReturn(true); final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory( mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); return channel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(onlyChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(onlyChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cachingConnectionFactory); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setShutdownTimeout(100); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } verify(mockConnection, Mockito.times(1)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(onlyChannel).txCommit(); verify(onlyChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); // verify close() was never called on the channel DirectFieldAccessor dfa = new DirectFieldAccessor(cachingConnectionFactory); List<?> channels = (List<?>) dfa.getPropertyValue("cachedChannelsTransactional"); assertEquals(0, channels.size()); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.LocallyTransactedTests.java
License:Apache License
/** * Verifies that an up-stack RabbitTemplate uses the listener's * channel (ChannelAwareMessageListener). *///from w w w.ja v a 2s .c om @SuppressWarnings("unchecked") @Test public void testChannelAwareMessageListener() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel onlyChannel = mock(Channel.class); when(onlyChannel.isOpen()).thenReturn(true); final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return onlyChannel; } tooManyChannels.set(new Exception("More than one channel requested")); Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); return channel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(onlyChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(onlyChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Channel> exposed = new AtomicReference<Channel>(); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new ChannelAwareMessageListener() { @Override public void onMessage(Message message, Channel channel) { exposed.set(channel); RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setShutdownTimeout(100); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } verify(mockConnection, Mockito.times(1)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(onlyChannel).txCommit(); verify(onlyChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); // verify close() was never called on the channel verify(onlyChannel, Mockito.never()).close(); container.stop(); assertSame(onlyChannel, exposed.get()); }
From source file:org.springframework.amqp.rabbit.listener.LocallyTransactedTests.java
License:Apache License
/** * Verifies that the listener channel is not exposed when so configured and * up-stack RabbitTemplate uses the additional channel. * created when exposeListenerChannel is false (ChannelAwareMessageListener). */// w w w . j a va 2 s. c o m @SuppressWarnings("unchecked") @Test public void testChannelAwareMessageListenerDontExpose() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); final Channel firstChannel = mock(Channel.class); when(firstChannel.isOpen()).thenReturn(true); final Channel secondChannel = mock(Channel.class); when(secondChannel.isOpen()).thenReturn(true); final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(mockConnectionFactory); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); final AtomicReference<Exception> tooManyChannels = new AtomicReference<Exception>(); doAnswer(new Answer<Channel>() { boolean done; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { if (!done) { done = true; return firstChannel; } return secondChannel; } }).when(mockConnection).createChannel(); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); return null; } }).when(firstChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch commitLatch = new CountDownLatch(1); doAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { commitLatch.countDown(); return null; } }).when(firstChannel).txCommit(); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Channel> exposed = new AtomicReference<Channel>(); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new ChannelAwareMessageListener() { @Override public void onMessage(Message message, Channel channel) { exposed.set(channel); RabbitTemplate rabbitTemplate = new RabbitTemplate(singleConnectionFactory); rabbitTemplate.setChannelTransacted(true); // should use same channel as container rabbitTemplate.convertAndSend("foo", "bar", "baz"); latch.countDown(); } }); container.setQueueNames("queue"); container.setChannelTransacted(true); container.setExposeListenerChannel(false); container.setShutdownTimeout(100); container.afterPropertiesSet(); container.start(); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 }); assertTrue(latch.await(10, TimeUnit.SECONDS)); Exception e = tooManyChannels.get(); if (e != null) { throw e; } // once for listener, once for exposed + 0 for template (used bound) verify(mockConnection, Mockito.times(2)).createChannel(); assertTrue(commitLatch.await(10, TimeUnit.SECONDS)); verify(firstChannel).txCommit(); verify(secondChannel).txCommit(); verify(secondChannel).basicPublish(Mockito.anyString(), Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); assertSame(secondChannel, exposed.get()); verify(firstChannel, Mockito.never()).close(); verify(secondChannel, Mockito.times(1)).close(); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*w w w. j a v a 2 s. c o m*/ public void testTxSizeAcks() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("1"); return null; } }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { latch.countDown(); return null; } }).when(channel).basicAck(anyLong(), anyBoolean()); final List<Message> messages = new ArrayList<Message>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setTxSize(2); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { messages.add(message); } }); container.start(); BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(4L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(4, messages.size()); Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { container.stop(); } }); consumer.get().handleCancelOk("1"); verify(channel, times(2)).basicAck(anyLong(), anyBoolean()); verify(channel).basicAck(2, true); verify(channel).basicAck(4, true); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
License:Apache License
@SuppressWarnings("unchecked") @Test/* w w w .j a v a 2s . c om*/ public void testTxSizeAcksWIthShortSet() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("1"); return null; } }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { latch.countDown(); return null; } }).when(channel).basicAck(anyLong(), anyBoolean()); final List<Message> messages = new ArrayList<Message>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setTxSize(2); container.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { messages.add(message); } }); container.start(); BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(3, messages.size()); Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { container.stop(); } }); consumer.get().handleCancelOk("1"); verify(channel, times(2)).basicAck(anyLong(), anyBoolean()); verify(channel).basicAck(2, true); // second set was short verify(channel).basicAck(3, true); container.stop(); }
From source file:org.springframework.amqp.rabbit.test.TestRabbitTemplate.java
License:Apache License
@Override protected Message doSendAndReceiveWithFixed(String exchange, String routingKey, Message message, CorrelationData correlationData) { Listeners listeners = this.listeners.get(routingKey); if (listeners == null) { throw new IllegalArgumentException("No listener for " + routingKey); }/*from w w w .java2 s . c o m*/ Channel channel = mock(Channel.class); final AtomicReference<Message> reply = new AtomicReference<>(); Object listener = listeners.next(); if (listener instanceof AbstractAdaptableMessageListener) { try { AbstractAdaptableMessageListener adapter = (AbstractAdaptableMessageListener) listener; willAnswer(i -> { Envelope envelope = new Envelope(1, false, "", REPLY_QUEUE); reply.set(MessageBuilder .withBody(i.getArgument(4)).andProperties(getMessagePropertiesConverter() .toMessageProperties(i.getArgument(3), envelope, adapter.getEncoding())) .build()); return null; }).given(channel).basicPublish(anyString(), anyString(), anyBoolean(), any(BasicProperties.class), any(byte[].class)); message.getMessageProperties().setReplyTo(REPLY_QUEUE); adapter.onMessage(message, channel); } catch (Exception e) { throw RabbitExceptionTranslator.convertRabbitAccessException(e); } } else { throw new IllegalStateException("sendAndReceive not supported for " + listener.getClass().getName()); } return reply.get(); }
From source file:org.springframework.integration.amqp.inbound.AmqpMessageSourceTests.java
License:Apache License
@Test public void testAck() throws Exception { Channel channel = mock(Channel.class); willReturn(true).given(channel).isOpen(); Envelope envelope = new Envelope(123L, false, "ex", "rk"); BasicProperties props = new BasicProperties.Builder().build(); GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0); willReturn(getResponse).given(channel).basicGet("foo", false); Connection connection = mock(Connection.class); willReturn(true).given(connection).isOpen(); willReturn(channel).given(connection).createChannel(); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory); AmqpMessageSource source = new AmqpMessageSource(ccf, "foo"); source.setRawMessageHeader(true);//from w w w . j a v a 2s . c o m Message<?> received = source.receive(); assertThat(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE)) .isInstanceOf(org.springframework.amqp.core.Message.class); assertThat(received.getHeaders().get(IntegrationMessageHeaderAccessor.SOURCE_DATA)) .isSameAs(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE)); assertThat(received.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE)).isEqualTo("foo"); // make sure channel is not cached org.springframework.amqp.rabbit.connection.Connection conn = ccf.createConnection(); Channel notCached = conn.createChannel(false); // should not have been "closed" verify(connection, times(2)).createChannel(); StaticMessageHeaderAccessor.getAcknowledgmentCallback(received).acknowledge(Status.ACCEPT); verify(channel).basicAck(123L, false); Channel cached = conn.createChannel(false); // should have been "closed" verify(connection, times(2)).createChannel(); notCached.close(); cached.close(); ccf.destroy(); verify(channel, times(2)).close(); verify(connection).close(30000); }