Search in sources :

Example 6 with NioParams

use of com.rabbitmq.client.impl.nio.NioParams in project rabbitmq-java-client by rabbitmq.

the class NioTlsUnverifiedConnection method socketChannelConfigurator.

@Test
public void socketChannelConfigurator() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();
    connectionFactory.useSslProtocol();
    NioParams nioParams = new NioParams();
    final AtomicBoolean sslEngineHasBeenCalled = new AtomicBoolean(false);
    nioParams.setSslEngineConfigurator(sslEngine -> sslEngineHasBeenCalled.set(true));
    connectionFactory.setNioParams(nioParams);
    Connection connection = null;
    try {
        connection = connectionFactory.newConnection();
        assertTrue("The SSL engine configurator should have called", sslEngineHasBeenCalled.get());
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) NioParams(com.rabbitmq.client.impl.nio.NioParams) Connection(com.rabbitmq.client.Connection) Test(org.junit.Test)

Example 7 with NioParams

use of com.rabbitmq.client.impl.nio.NioParams in project rabbitmq-java-client by rabbitmq.

the class NioTlsUnverifiedConnection 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);
        sslContext.init(null, new TrustManager[] { new TrustEverythingTrustManager() }, null);
        ConnectionFactory cf = TestUtils.connectionFactory();
        cf.useSslProtocol(sslContext);
        cf.useNio();
        AtomicReference<SSLEngine> engine = new AtomicReference<>();
        cf.setNioParams(new NioParams().setSslEngineConfigurator(sslEngine -> engine.set(sslEngine)));
        try (Connection c = cf.newConnection()) {
            CountDownLatch latch = new CountDownLatch(1);
            basicGetBasicConsume(c, QUEUE, latch, 100);
            boolean messagesReceived = latch.await(5, TimeUnit.SECONDS);
            assertTrue("Message has not been received", messagesReceived);
            assertThat(engine.get()).isNotNull();
            assertThat(engine.get().getEnabledProtocols()).contains(protocol);
        }
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TrustManager(javax.net.ssl.TrustManager) Connection(com.rabbitmq.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) SSLEngine(javax.net.ssl.SSLEngine) TcpCrusher(org.netcrusher.tcp.TcpCrusher) SocketTimeoutException(java.net.SocketTimeoutException) Assert.fail(org.junit.Assert.fail) TrustEverythingTrustManager(com.rabbitmq.client.TrustEverythingTrustManager) NioParams(com.rabbitmq.client.impl.nio.NioParams) ExecutorService(java.util.concurrent.ExecutorService) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) BrokerTestCase(com.rabbitmq.client.test.BrokerTestCase) TestUtils(com.rabbitmq.client.test.TestUtils) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TcpCrusherBuilder(org.netcrusher.tcp.TcpCrusherBuilder) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) NioReactor(org.netcrusher.core.reactor.NioReactor) TestUtils.basicGetBasicConsume(com.rabbitmq.client.test.TestUtils.basicGetBasicConsume) NioParams(com.rabbitmq.client.impl.nio.NioParams) SSLEngine(javax.net.ssl.SSLEngine) Connection(com.rabbitmq.client.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) SSLContext(javax.net.ssl.SSLContext) CountDownLatch(java.util.concurrent.CountDownLatch) TrustEverythingTrustManager(com.rabbitmq.client.TrustEverythingTrustManager) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 8 with NioParams

use of com.rabbitmq.client.impl.nio.NioParams in project rabbitmq-java-client by rabbitmq.

the class JavaNioTest method byteBufferFactory.

@Test
public void byteBufferFactory() throws Exception {
    ConnectionFactory cf = new ConnectionFactory();
    cf.useNio();
    int baseCapacity = 32768;
    NioParams nioParams = new NioParams();
    nioParams.setReadByteBufferSize(baseCapacity / 2);
    nioParams.setWriteByteBufferSize(baseCapacity / 4);
    List<ByteBuffer> byteBuffers = new CopyOnWriteArrayList<>();
    cf.setNioParams(nioParams.setByteBufferFactory(new DefaultByteBufferFactory(capacity -> {
        ByteBuffer bb = ByteBuffer.allocate(capacity);
        byteBuffers.add(bb);
        return bb;
    })));
    try (Connection c = cf.newConnection()) {
        sendAndVerifyMessage(c, 100);
    }
    assertThat(byteBuffers).hasSize(2);
    Condition<Integer> condition = new Condition<>(c -> c == nioParams.getReadByteBufferSize() || c == nioParams.getWriteByteBufferSize(), "capacity set by factory");
    assertThat(byteBuffers.get(0).capacity()).is(condition);
    assertThat(byteBuffers.get(1).capacity()).is(condition);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Condition(org.assertj.core.api.Condition) NioParams(com.rabbitmq.client.impl.nio.NioParams) DefaultByteBufferFactory(com.rabbitmq.client.impl.nio.DefaultByteBufferFactory) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

NioParams (com.rabbitmq.client.impl.nio.NioParams)8 Test (org.junit.Test)5 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)4 IOException (java.io.IOException)4 Connection (com.rabbitmq.client.Connection)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Before (org.junit.Before)3 DefaultByteBufferFactory (com.rabbitmq.client.impl.nio.DefaultByteBufferFactory)2 TestUtils (com.rabbitmq.client.test.TestUtils)2 ByteBuffer (java.nio.ByteBuffer)2 Collection (java.util.Collection)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2