Search in sources :

Example 66 with ConnectionFactory

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

the class VerifiedConnection method connectionGetConsumeProtocols.

@Test
public void connectionGetConsumeProtocols() throws Exception {
    Collection<String> availableProtocols = TlsTestUtils.availableTlsProtocols();
    Collection<String> protocols = Stream.of("TLSv1.2", "TLSv1.3").filter(p -> availableProtocols.contains(p)).collect(Collectors.toList());
    for (String protocol : protocols) {
        SSLContext sslContext = SSLContext.getInstance(protocol);
        ConnectionFactory cf = TestUtils.connectionFactory();
        cf.useSslProtocol(TlsTestUtils.verifiedSslContext(() -> sslContext));
        AtomicReference<Supplier<String[]>> protocolsSupplier = new AtomicReference<>();
        if (TestUtils.USE_NIO) {
            cf.useNio();
            cf.setNioParams(new NioParams().setSslEngineConfigurator(sslEngine -> {
                protocolsSupplier.set(() -> sslEngine.getEnabledProtocols());
            }));
        } else {
            cf.setSocketConfigurator(socket -> {
                SSLSocket s = (SSLSocket) socket;
                protocolsSupplier.set(() -> s.getEnabledProtocols());
            });
        }
        try (Connection c = cf.newConnection()) {
            CountDownLatch latch = new CountDownLatch(1);
            TestUtils.basicGetBasicConsume(c, VerifiedConnection.class.getName(), latch, 100);
            boolean messagesReceived = latch.await(5, TimeUnit.SECONDS);
            assertTrue("Message has not been received", messagesReceived);
            assertThat(protocolsSupplier.get()).isNotNull();
            assertThat(protocolsSupplier.get().get()).contains(protocol);
        }
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TestUtils(com.rabbitmq.client.test.TestUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collection(java.util.Collection) SSLSocket(javax.net.ssl.SSLSocket) LoggerFactory(org.slf4j.LoggerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Test(org.junit.Test) Connection(com.rabbitmq.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Assert.fail(org.junit.Assert.fail) NioParams(com.rabbitmq.client.impl.nio.NioParams) NioParams(com.rabbitmq.client.impl.nio.NioParams) SSLSocket(javax.net.ssl.SSLSocket) Connection(com.rabbitmq.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) SSLContext(javax.net.ssl.SSLContext) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Supplier(java.util.function.Supplier) Test(org.junit.Test)

Example 67 with ConnectionFactory

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

the class LoopbackUsers method getFactory.

private ConnectionFactory getFactory(String name, String addr) {
    ConnectionFactory factory = TestUtils.connectionFactory();
    factory.setUsername(name);
    factory.setPassword(name);
    factory.setHost(addr);
    return factory;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory)

Example 68 with ConnectionFactory

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

the class NioTlsUnverifiedConnection method connectionShouldEnforceConnectionTimeout.

@Test
public void connectionShouldEnforceConnectionTimeout() throws Exception {
    // assumes RabbitMQ server running on localhost;
    int amqpPort = 5671;
    int amqpProxyPort = TestUtils.randomNetworkPort();
    int connectionTimeout = 3_000;
    int handshakeTimeout = 1_000;
    try (NioReactor reactor = new NioReactor();
        TcpCrusher tcpProxy = TcpCrusherBuilder.builder().withReactor(reactor).withBindAddress(new InetSocketAddress(amqpProxyPort)).withConnectAddress("localhost", amqpPort).build()) {
        tcpProxy.open();
        tcpProxy.freeze();
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(amqpProxyPort);
        factory.useSslProtocol();
        factory.useNio();
        factory.setConnectionTimeout(connectionTimeout);
        factory.setHandshakeTimeout(handshakeTimeout);
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        try {
            CountDownLatch latch = new CountDownLatch(1);
            executorService.submit(() -> {
                try {
                    factory.newConnection();
                    latch.countDown();
                } catch (SocketTimeoutException e) {
                    latch.countDown();
                } catch (Exception e) {
                // not supposed to happen
                }
            });
            boolean connectionCreatedTimedOut = latch.await(10, TimeUnit.SECONDS);
            assertThat(connectionCreatedTimedOut).isTrue();
        } finally {
            executorService.shutdownNow();
        }
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TcpCrusher(org.netcrusher.tcp.TcpCrusher) SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) ExecutorService(java.util.concurrent.ExecutorService) NioReactor(org.netcrusher.core.reactor.NioReactor) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) Test(org.junit.Test)

Example 69 with ConnectionFactory

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

the class TlsConnectionLogging method certificateInfoAreProperlyExtracted.

@Test
public void certificateInfoAreProperlyExtracted() throws Exception {
    SSLContext sslContext = TlsTestUtils.getSSLContext();
    sslContext.init(null, new TrustManager[] { new AlwaysTrustTrustManager() }, null);
    ConnectionFactory connectionFactory = TestUtils.connectionFactory();
    connectionFactory.useSslProtocol(sslContext);
    Supplier<SSLSession> sslSessionSupplier = configurer.apply(connectionFactory);
    try (Connection ignored = connectionFactory.newConnection()) {
        SSLSession session = sslSessionSupplier.get();
        assertNotNull(session);
        String info = TlsUtils.peerCertificateInfo(session.getPeerCertificates()[0], "some prefix");
        Assertions.assertThat(info).contains("some prefix").contains("CN=").contains("X.509 usage extensions").contains("KeyUsage");
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Test(org.junit.Test)

Example 70 with ConnectionFactory

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

the class SaslMechanisms method connectionWithoutCapabilities.

// start a connection without capabilities, causing authentication failures
// to be reported by the broker by closing the connection
private Connection connectionWithoutCapabilities(String username, String password) throws IOException, TimeoutException {
    ConnectionFactory customFactory = TestUtils.connectionFactory();
    customFactory.setUsername(username);
    customFactory.setPassword(password);
    Map<String, Object> customProperties = AMQConnection.defaultClientProperties();
    customProperties.remove("capabilities");
    customFactory.setClientProperties(customProperties);
    return customFactory.newConnection();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) LongString(com.rabbitmq.client.LongString)

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