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.listener.LocallyTransactedTests.java
License:Apache License
/** * Verifies that an up-stack RabbitTemplate uses the listener's * channel (ChannelAwareMessageListener). *//*from ww w. j ava 2 s .co 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.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). *//*from w w w . j av a 2s.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.wso2.carbon.inbound.endpoint.protocol.rabbitmq.RabbitMQUtils.java
License:Open Source License
public static Connection createConnection(ConnectionFactory factory, Address[] addresses) throws IOException { Connection connection = null; try {// w w w. j av a 2 s .com connection = factory.newConnection(addresses); } catch (TimeoutException e) { log.error("Error while creating new connection", e); } return connection; }
From source file:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQConsumer.java
License:Open Source License
/** * Consumes the sample data// ww w . j a va 2s .com * */ public void consume(String queue, String host, String host1, int port1, String host2, int port2, String host3, int port3, int executorSize) { TASK_QUEUE_NAME = queue; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); final Connection connection; final Channel channel; final Consumer consumer; try { connection = factory.newConnection(EXECUTORS); channel = connection.createChannel(); consumer = new EventDispatcher(channel, host1, port1, host2, port2, host3, port3, executorSize); boolean autoAck = true; // acknowledgment is covered below channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } }
From source file:pl.nask.hsn2.bus.test.rabbitmq.RbtAbstractEndPointTest.java
License:Open Source License
protected Connection getConnection(int numberOfconsumerThreads) throws BusException { ConnectionFactory factory = getConnectionFactory(); Connection connection = null; try {//from ww w . j a v a 2s. c om connection = factory .newConnection(new ConfigurableExecutorService("rbt-test-consumer-", numberOfconsumerThreads)); } catch (IOException e) { throw new BusException("Cannot establish connection to the broker.", e); } if (connection == null) { throw new BusException("Cannot establish connection to the broker."); } LOGGER.info("Got connection."); return connection; }