List of usage examples for com.rabbitmq.client ConnectionFactory newConnection
public Connection newConnection(ExecutorService executor) throws IOException, TimeoutException
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
License:Apache License
@Test public void testWithConnectionListener() throws IOException { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection1, mockConnection2);// www. j av a 2 s. co m when(mockConnection1.isOpen()).thenReturn(true); when(mockChannel.isOpen()).thenReturn(true); when(mockConnection1.createChannel()).thenReturn(mockChannel); when(mockConnection2.createChannel()).thenReturn(mockChannel); final AtomicReference<Connection> created = new AtomicReference<Connection>(); final AtomicReference<Connection> closed = new AtomicReference<Connection>(); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); connectionFactory.addConnectionListener(new ConnectionListener() { @Override public void onCreate(Connection connection) { created.set(connection); } @Override public void onClose(Connection connection) { closed.set(connection); } }); ((CachingConnectionFactory) connectionFactory).setChannelCacheSize(1); Connection con = connectionFactory.createConnection(); Channel channel = con.createChannel(false); assertSame(con, created.get()); channel.close(); con.close(); verify(mockConnection1, never()).close(); Connection same = connectionFactory.createConnection(); channel = con.createChannel(false); assertSame(con, same); channel.close(); when(mockConnection1.isOpen()).thenReturn(false); when(mockChannel.isOpen()).thenReturn(false); // force a connection refresh channel.basicCancel("foo"); channel.close(); Connection notSame = connectionFactory.createConnection(); assertNotSame(con, notSame); assertSame(con, closed.get()); assertSame(notSame, created.get()); connectionFactory.destroy(); verify(mockConnection2, atLeastOnce()).close(anyInt()); assertSame(notSame, closed.get()); verify(mockConnectionFactory, times(2)).newConnection((ExecutorService) null); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java
License:Apache License
private void testNoExistingReplyToOrCorrelationGuts(final boolean standardHeader) throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); Queue replyQueue = new Queue("new.replyTo"); template.setReplyQueue(replyQueue);//from w ww. j av a 2 s.c o m if (!standardHeader) { template.setCorrelationKey(CORRELATION_HEADER); } MessageProperties messageProperties = new MessageProperties(); Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference<String> replyTo = new AtomicReference<String>(); final AtomicReference<String> correlationId = new AtomicReference<String>(); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3]; replyTo.set(basicProps.getReplyTo()); if (standardHeader) { correlationId.set(basicProps.getCorrelationId()); } else { correlationId.set((String) basicProps.getHeaders().get(CORRELATION_HEADER)); } MessageProperties springProps = new DefaultMessagePropertiesConverter() .toMessageProperties(basicProps, null, "UTF-8"); Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; } }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); Message reply = template.sendAndReceive(message); assertNotNull(reply); assertNotNull(replyTo.get()); assertEquals("new.replyTo", replyTo.get()); assertNotNull(correlationId.get()); assertNull(reply.getMessageProperties().getReplyTo()); if (standardHeader) { assertNull(reply.getMessageProperties().getCorrelationId()); } else { assertNull(reply.getMessageProperties().getHeaders().get(CORRELATION_HEADER)); } }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java
License:Apache License
@Test public void testReplyToOneDeep() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); Queue replyQueue = new Queue("new.replyTo"); template.setReplyQueue(replyQueue);/*from ww w . j a v a 2s . c o m*/ template.setReplyTimeout(60000); MessageProperties messageProperties = new MessageProperties(); messageProperties.setReplyTo("replyTo1"); messageProperties.setCorrelationId("saveThis".getBytes()); Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference<String> replyTo = new AtomicReference<String>(); final AtomicReference<String> correlationId = new AtomicReference<String>(); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3]; replyTo.set(basicProps.getReplyTo()); correlationId.set(basicProps.getCorrelationId()); MessageProperties springProps = new DefaultMessagePropertiesConverter() .toMessageProperties(basicProps, null, "UTF-8"); Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; } }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); Message reply = template.sendAndReceive(message); assertNotNull(reply); assertNotNull(replyTo.get()); assertEquals("new.replyTo", replyTo.get()); assertNotNull(correlationId.get()); assertFalse("saveThis".equals(correlationId.get())); assertEquals("replyTo1", reply.getMessageProperties().getReplyTo()); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java
License:Apache License
@Test public void testReplyToThreeDeep() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); Queue replyQueue = new Queue("replyTo2"); template.setReplyQueue(replyQueue);//from w w w. ja v a 2s . c om MessageProperties messageProperties = new MessageProperties(); messageProperties.setReplyTo("replyTo1"); messageProperties.setCorrelationId("a".getBytes()); Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicInteger count = new AtomicInteger(); final List<String> nestedReplyTo = new ArrayList<String>(); final List<String> nestedCorrelation = new ArrayList<String>(); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3]; nestedReplyTo.add(basicProps.getReplyTo()); nestedCorrelation.add(basicProps.getCorrelationId()); MessageProperties springProps = new DefaultMessagePropertiesConverter() .toMessageProperties(basicProps, null, "UTF-8"); Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); if (count.incrementAndGet() < 2) { Message anotherMessage = new Message("Second".getBytes(), springProps); template.setReplyQueue(new Queue("replyTo3")); replyMessage = template.sendAndReceive(anotherMessage); nestedReplyTo.add(replyMessage.getMessageProperties().getReplyTo()); nestedCorrelation .add(new String(replyMessage.getMessageProperties().getCorrelationId(), "UTF-8")); } template.onMessage(replyMessage); return null; } }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); Message reply = template.sendAndReceive(message); assertNotNull(reply); assertEquals(3, nestedReplyTo.size()); assertEquals("replyTo2", nestedReplyTo.get(0)); assertEquals("replyTo3", nestedReplyTo.get(1)); assertEquals("replyTo2", nestedReplyTo.get(2)); // intermediate reply assertEquals("replyTo1", reply.getMessageProperties().getReplyTo()); assertEquals("a", new String(reply.getMessageProperties().getCorrelationId(), "UTF-8")); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java
License:Apache License
@Test public void testReplyToOneDeepCustomCorrelationKey() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); template.setCorrelationKey(CORRELATION_HEADER); Queue replyQueue = new Queue("new.replyTo"); template.setReplyQueue(replyQueue);//from w w w . j av a 2 s . c o m MessageProperties messageProperties = new MessageProperties(); messageProperties.setReplyTo("replyTo1"); messageProperties.setCorrelationId("saveThis".getBytes()); Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicReference<String> replyTo = new AtomicReference<String>(); final AtomicReference<String> correlationId = new AtomicReference<String>(); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3]; replyTo.set(basicProps.getReplyTo()); correlationId.set((String) basicProps.getHeaders().get(CORRELATION_HEADER)); MessageProperties springProps = new DefaultMessagePropertiesConverter() .toMessageProperties(basicProps, null, "UTF-8"); Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); template.onMessage(replyMessage); return null; } }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); Message reply = template.sendAndReceive(message); assertNotNull(reply); assertNotNull(replyTo.get()); assertEquals("new.replyTo", replyTo.get()); assertNotNull(correlationId.get()); assertEquals("replyTo1", reply.getMessageProperties().getReplyTo()); assertTrue(!"saveThis".equals(correlationId.get())); assertEquals("replyTo1", reply.getMessageProperties().getReplyTo()); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateHeaderTests.java
License:Apache License
@Test public void testReplyToThreeDeepCustomCorrelationKey() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); template.setCorrelationKey(CORRELATION_HEADER); Queue replyQueue = new Queue("replyTo2"); template.setReplyQueue(replyQueue);/*w w w .j a va 2s .c om*/ MessageProperties messageProperties = new MessageProperties(); messageProperties.setReplyTo("replyTo1"); messageProperties.setCorrelationId("a".getBytes()); Message message = new Message("Hello, world!".getBytes(), messageProperties); final AtomicInteger count = new AtomicInteger(); final List<String> nestedReplyTo = new ArrayList<String>(); final List<String> nestedCorrelation = new ArrayList<String>(); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { BasicProperties basicProps = (BasicProperties) invocation.getArguments()[3]; nestedReplyTo.add(basicProps.getReplyTo()); nestedCorrelation.add(basicProps.getCorrelationId()); MessageProperties springProps = new DefaultMessagePropertiesConverter() .toMessageProperties(basicProps, null, "UTF-8"); Message replyMessage = new Message("!dlrow olleH".getBytes(), springProps); if (count.incrementAndGet() < 2) { Message anotherMessage = new Message("Second".getBytes(), springProps); template.setReplyQueue(new Queue("replyTo3")); replyMessage = template.sendAndReceive(anotherMessage); nestedReplyTo.add(replyMessage.getMessageProperties().getReplyTo()); nestedCorrelation .add((String) replyMessage.getMessageProperties().getHeaders().get(CORRELATION_HEADER)); } template.onMessage(replyMessage); return null; } }).when(mockChannel).basicPublish(Mockito.any(String.class), Mockito.any(String.class), Mockito.anyBoolean(), Mockito.any(BasicProperties.class), Mockito.any(byte[].class)); Message reply = template.sendAndReceive(message); assertNotNull(reply); assertEquals(3, nestedReplyTo.size()); assertEquals("replyTo2", nestedReplyTo.get(0)); assertEquals("replyTo3", nestedReplyTo.get(1)); assertEquals("replyTo2", nestedReplyTo.get(2)); //intermediate reply assertEquals("replyTo1", reply.getMessageProperties().getReplyTo()); assertEquals("a", new String(reply.getMessageProperties().getCorrelationId(), "UTF-8")); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
License:Apache License
@Test public void testPublisherConfirmNotReceived() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); doReturn(new PublisherCallbackChannelImpl(mockChannel)).when(mockConnection).createChannel(); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); final AtomicBoolean confirmed = new AtomicBoolean(); template.setConfirmCallback(new ConfirmCallback() { @Override/*from w ww.j a v a2s . co m*/ public void confirm(CorrelationData correlationData, boolean ack, String cause) { confirmed.set(true); } }); template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); Thread.sleep(5); Collection<CorrelationData> unconfirmed = template.getUnconfirmed(0); assertEquals(1, unconfirmed.size()); assertEquals("abc", unconfirmed.iterator().next().getId()); assertFalse(confirmed.get()); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
License:Apache License
@Test public void testPublisherConfirmNotReceivedMultiThreads() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); PublisherCallbackChannelImpl channel1 = new PublisherCallbackChannelImpl(mockChannel); PublisherCallbackChannelImpl channel2 = new PublisherCallbackChannelImpl(mockChannel); when(mockConnection.createChannel()).thenReturn(channel1).thenReturn(channel2); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); final AtomicBoolean confirmed = new AtomicBoolean(); template.setConfirmCallback(new ConfirmCallback() { @Override/* w w w . j a v a 2 s.c o m*/ public void confirm(CorrelationData correlationData, boolean ack, String cause) { confirmed.set(true); } }); // Hold up the first thread so we get two channels final CountDownLatch threadLatch = new CountDownLatch(1); final CountDownLatch threadSentLatch = new CountDownLatch(1); //Thread 1 Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { template.execute(new ChannelCallback<Object>() { @Override public Object doInRabbit(Channel channel) throws Exception { try { threadLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } template.doSend(channel, "", ROUTE, new SimpleMessageConverter().toMessage("message", new MessageProperties()), new CorrelationData("def")); threadSentLatch.countDown(); return null; } }); } }); // Thread 2 template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); threadLatch.countDown(); assertTrue(threadSentLatch.await(5, TimeUnit.SECONDS)); Thread.sleep(5); Collection<CorrelationData> unconfirmed = template.getUnconfirmed(0); assertEquals(2, unconfirmed.size()); Set<String> ids = new HashSet<String>(); Iterator<CorrelationData> iterator = unconfirmed.iterator(); ids.add(iterator.next().getId()); ids.add(iterator.next().getId()); assertTrue(ids.remove("abc")); assertTrue(ids.remove("def")); assertFalse(confirmed.get()); DirectFieldAccessor dfa = new DirectFieldAccessor(template); Map<?, ?> pendingConfirms = (Map<?, ?>) dfa.getPropertyValue("pendingConfirms"); assertEquals(2, pendingConfirms.size()); channel1.close(); assertEquals(1, pendingConfirms.size()); channel2.close(); assertEquals(0, pendingConfirms.size()); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
License:Apache License
@Test public void testPublisherConfirmNotReceivedAged() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); doReturn(new PublisherCallbackChannelImpl(mockChannel)).when(mockConnection).createChannel(); final AtomicInteger count = new AtomicInteger(); doAnswer(new Answer<Object>() { @Override// w w w .jav a 2 s . c o m public Object answer(InvocationOnMock invocation) throws Throwable { return count.incrementAndGet(); } }).when(mockChannel).getNextPublishSeqNo(); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); final AtomicBoolean confirmed = new AtomicBoolean(); template.setConfirmCallback(new ConfirmCallback() { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { confirmed.set(true); } }); template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); Thread.sleep(100); template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("def")); Collection<CorrelationData> unconfirmed = template.getUnconfirmed(50); assertEquals(1, unconfirmed.size()); assertEquals("abc", unconfirmed.iterator().next().getId()); assertFalse(confirmed.get()); Thread.sleep(100); unconfirmed = template.getUnconfirmed(50); assertEquals(1, unconfirmed.size()); assertEquals("def", unconfirmed.iterator().next().getId()); assertFalse(confirmed.get()); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
License:Apache License
@Test public void testPublisherConfirmMultiple() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); PublisherCallbackChannelImpl callbackChannel = new PublisherCallbackChannelImpl(mockChannel); when(mockConnection.createChannel()).thenReturn(callbackChannel); final AtomicInteger count = new AtomicInteger(); doAnswer(new Answer<Object>() { @Override//from w ww.ja v a 2 s. com public Object answer(InvocationOnMock invocation) throws Throwable { return count.incrementAndGet(); } }).when(mockChannel).getNextPublishSeqNo(); final RabbitTemplate template = new RabbitTemplate(new SingleConnectionFactory(mockConnectionFactory)); final List<String> confirms = new ArrayList<String>(); final CountDownLatch latch = new CountDownLatch(2); template.setConfirmCallback(new ConfirmCallback() { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { if (ack) { confirms.add(correlationData.getId()); latch.countDown(); } } }); template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc")); template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("def")); callbackChannel.handleAck(2, true); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); Collection<CorrelationData> unconfirmed = template.getUnconfirmed(0); assertNull(unconfirmed); }