Search in sources :

Example 96 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class SslContextFactoryTest method doTestSetSslContextFactory.

private void doTestSetSslContextFactory(Supplier<ConnectionFactory> supplier) throws Exception {
    ConnectionFactory connectionFactory = supplier.get();
    SslContextFactory sslContextFactory = sslContextFactory();
    connectionFactory.setSslContextFactory(sslContextFactory);
    Connection connection = connectionFactory.newConnection("connection01");
    TestUtils.close(connection);
    try {
        connectionFactory.newConnection("connection02");
        fail("The SSL context of this client should not trust the server");
    } catch (SSLHandshakeException e) {
    // OK
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SslContextFactory(com.rabbitmq.client.SslContextFactory) Connection(com.rabbitmq.client.Connection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 97 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class TrafficListenerTest method trafficListenerIsCalled.

@Test
public void trafficListenerIsCalled() throws Exception {
    ConnectionFactory cf = TestUtils.connectionFactory();
    TestTrafficListener testTrafficListener = new TestTrafficListener();
    cf.setTrafficListener(testTrafficListener);
    configurator.accept(cf);
    try (Connection c = cf.newConnection()) {
        Channel ch = c.createChannel();
        String queue = ch.queueDeclare().getQueue();
        CountDownLatch latch = new CountDownLatch(1);
        ch.basicConsume(queue, true, (consumerTag, message) -> latch.countDown(), consumerTag -> {
        });
        String messageContent = UUID.randomUUID().toString();
        ch.basicPublish("", queue, null, messageContent.getBytes());
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        assertEquals(1, testTrafficListener.outboundContent.size());
        assertEquals(messageContent, testTrafficListener.outboundContent.get(0));
        assertEquals(1, testTrafficListener.inboundContent.size());
        assertEquals(messageContent, testTrafficListener.inboundContent.get(0));
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 98 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class CloseInMainLoop method specialConnectionFactory.

private ConnectionFactory specialConnectionFactory() {
    ConnectionFactory f = TestUtils.connectionFactory();
    f.setExceptionHandler(new DefaultExceptionHandler() {

        @Override
        public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) {
            try {
                // TODO: change this to call 4-parameter close and make 6-parm one private
                ((AMQConnection) channel.getConnection()).close(AMQP.INTERNAL_ERROR, "Internal error in Consumer " + consumerTag, false, exception, -1, false);
            } catch (Throwable e) {
            // Man, this clearly isn't our day.
            // TODO: Log the nested failure
            } finally {
                closeLatch.countDown();
            }
        }
    });
    return f;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) Channel(com.rabbitmq.client.Channel) DefaultExceptionHandler(com.rabbitmq.client.impl.DefaultExceptionHandler)

Example 99 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class DnsRecordIpAddressResolverTests method loopbackInterfaceResolution.

@Test
public void loopbackInterfaceResolution() throws IOException, TimeoutException {
    DnsRecordIpAddressResolver addressResolver = new DnsRecordIpAddressResolver("127.0.0.1");
    ConnectionFactory connectionFactory = newConnectionFactory();
    Connection connection = connectionFactory.newConnection(addressResolver);
    try {
        connection.createChannel();
    } finally {
        connection.abort();
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DnsRecordIpAddressResolver(com.rabbitmq.client.DnsRecordIpAddressResolver) Connection(com.rabbitmq.client.Connection) Test(org.junit.Test)

Example 100 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.

the class AmqpUriTest method processUriQueryParameterShouldBeCalledForNotHandledParameter.

@Test
public void processUriQueryParameterShouldBeCalledForNotHandledParameter() throws Exception {
    Map<String, String> processedParameters = new HashMap<>();
    ConnectionFactory cf = new ConnectionFactory() {

        @Override
        protected void processUriQueryParameter(String key, String value) {
            processedParameters.put(key, value);
        }
    };
    cf.setUri("amqp://user:pass@host:10000/vhost?heartbeat=60&key=value");
    assertThat(processedParameters).hasSize(1).containsEntry("key", "value");
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)188 Connection (com.rabbitmq.client.Connection)87 Test (org.junit.Test)73 Channel (com.rabbitmq.client.Channel)63 IOException (java.io.IOException)53 TimeoutException (java.util.concurrent.TimeoutException)23 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)11 Envelope (com.rabbitmq.client.Envelope)11 SSLContext (javax.net.ssl.SSLContext)11 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 HashMap (java.util.HashMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AMQP (com.rabbitmq.client.AMQP)6 Consumer (com.rabbitmq.client.Consumer)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)6 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 Address (com.rabbitmq.client.Address)5 NioParams (com.rabbitmq.client.impl.nio.NioParams)4 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Path (javax.ws.rs.Path)3