Search in sources :

Example 1 with NioParams

use of com.rabbitmq.client.impl.nio.NioParams 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 2 with NioParams

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

the class NioDeadlockOnConnectionClosing method setUp.

@Before
public void setUp() {
    nioExecutorService = Executors.newFixedThreadPool(2);
    connectionShutdownExecutorService = Executors.newFixedThreadPool(2);
    cf = TestUtils.connectionFactory();
    cf.setAutomaticRecoveryEnabled(true);
    cf.useNio();
    cf.setNetworkRecoveryInterval(1000);
    NioParams params = new NioParams().setNioExecutor(nioExecutorService).setConnectionShutdownExecutor(connectionShutdownExecutorService).setNbIoThreads(2);
    cf.setNioParams(params);
    connections = new ArrayList<>();
}
Also used : NioParams(com.rabbitmq.client.impl.nio.NioParams) Before(org.junit.Before)

Example 3 with NioParams

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

the class NoAutoRecoveryWhenTcpWindowIsFullTest method setUp.

@Before
public void setUp() throws Exception {
    // we need several threads to publish, dispatch deliveries, handle RPC responses, etc.
    executorService = Executors.newFixedThreadPool(10);
    final ConnectionFactory factory = TestUtils.connectionFactory();
    factory.setSocketConfigurator(new DefaultSocketConfigurator() {

        /* default value on Linux */
        int DEFAULT_RECEIVE_BUFFER_SIZE = 43690;

        @Override
        public void configure(Socket socket) throws IOException {
            super.configure(socket);
            socket.setReceiveBufferSize(DEFAULT_RECEIVE_BUFFER_SIZE);
        }
    });
    factory.setAutomaticRecoveryEnabled(true);
    factory.setTopologyRecoveryEnabled(true);
    // we try to set the lower values for closing timeouts, etc.
    // this makes the test execute faster.
    factory.setRequestedHeartbeat(5);
    factory.setSharedExecutor(executorService);
    // we need the shutdown executor: channel shutting down depends on the work pool,
    // which is full. Channel shutting down will time out with the shutdown executor.
    factory.setShutdownExecutor(executorService);
    factory.setNetworkRecoveryInterval(2000);
    if (TestUtils.USE_NIO) {
        factory.setWorkPoolTimeout(10 * 1000);
        factory.setNioParams(new NioParams().setWriteQueueCapacity(10 * 1000 * 1000).setNbIoThreads(4));
    }
    producingConnection = (AutorecoveringConnection) factory.newConnection("Producer Connection");
    producingChannel = (AutorecoveringChannel) producingConnection.createChannel();
    consumingConnection = (AutorecoveringConnection) factory.newConnection("Consuming Connection");
    consumingChannel = (AutorecoveringChannel) consumingConnection.createChannel();
    consumerOkLatch = new CountDownLatch(2);
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) NioParams(com.rabbitmq.client.impl.nio.NioParams) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultSocketConfigurator(com.rabbitmq.client.DefaultSocketConfigurator) Socket(java.net.Socket) Before(org.junit.Before)

Example 4 with NioParams

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

the class JavaNioTest method customWriteQueue.

@Test
public void customWriteQueue() throws Exception {
    ConnectionFactory cf = new ConnectionFactory();
    cf.useNio();
    AtomicInteger count = new AtomicInteger(0);
    cf.setNioParams(new NioParams().setWriteQueueFactory(ctx -> {
        count.incrementAndGet();
        return new BlockingQueueNioQueue(new LinkedBlockingQueue<>(ctx.getNioParams().getWriteQueueCapacity()), ctx.getNioParams().getWriteEnqueuingTimeoutInMs());
    }));
    try (Connection c = cf.newConnection()) {
        sendAndVerifyMessage(c, 100);
    }
    assertEquals(1, count.get());
}
Also used : BlockingQueueNioQueue(com.rabbitmq.client.impl.nio.BlockingQueueNioQueue) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertTrue(org.junit.Assert.assertTrue) com.rabbitmq.client(com.rabbitmq.client) Test(org.junit.Test) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) List(java.util.List) DefaultByteBufferFactory(com.rabbitmq.client.impl.nio.DefaultByteBufferFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Condition(org.assertj.core.api.Condition) NioParams(com.rabbitmq.client.impl.nio.NioParams) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) BlockingQueueNioQueue(com.rabbitmq.client.impl.nio.BlockingQueueNioQueue) NioParams(com.rabbitmq.client.impl.nio.NioParams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with NioParams

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

the class ConnectionFactoryConfigurator method load.

public static void load(ConnectionFactory cf, Map<String, String> properties, String prefix) {
    prefix = prefix == null ? "" : prefix;
    String uri = properties.get(prefix + "uri");
    if (uri != null) {
        try {
            cf.setUri(uri);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Error while setting AMQP URI: " + uri, e);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Error while setting AMQP URI: " + uri, e);
        } catch (KeyManagementException e) {
            throw new IllegalArgumentException("Error while setting AMQP URI: " + uri, e);
        }
    }
    String username = lookUp(USERNAME, properties, prefix);
    if (username != null) {
        cf.setUsername(username);
    }
    String password = lookUp(PASSWORD, properties, prefix);
    if (password != null) {
        cf.setPassword(password);
    }
    String vhost = lookUp(VIRTUAL_HOST, properties, prefix);
    if (vhost != null) {
        cf.setVirtualHost(vhost);
    }
    String host = lookUp(HOST, properties, prefix);
    if (host != null) {
        cf.setHost(host);
    }
    String port = lookUp(PORT, properties, prefix);
    if (port != null) {
        cf.setPort(Integer.valueOf(port));
    }
    String requestedChannelMax = lookUp(CONNECTION_CHANNEL_MAX, properties, prefix);
    if (requestedChannelMax != null) {
        cf.setRequestedChannelMax(Integer.valueOf(requestedChannelMax));
    }
    String requestedFrameMax = lookUp(CONNECTION_FRAME_MAX, properties, prefix);
    if (requestedFrameMax != null) {
        cf.setRequestedFrameMax(Integer.valueOf(requestedFrameMax));
    }
    String requestedHeartbeat = lookUp(CONNECTION_HEARTBEAT, properties, prefix);
    if (requestedHeartbeat != null) {
        cf.setRequestedHeartbeat(Integer.valueOf(requestedHeartbeat));
    }
    String connectionTimeout = lookUp(CONNECTION_TIMEOUT, properties, prefix);
    if (connectionTimeout != null) {
        cf.setConnectionTimeout(Integer.valueOf(connectionTimeout));
    }
    String handshakeTimeout = lookUp(HANDSHAKE_TIMEOUT, properties, prefix);
    if (handshakeTimeout != null) {
        cf.setHandshakeTimeout(Integer.valueOf(handshakeTimeout));
    }
    String shutdownTimeout = lookUp(SHUTDOWN_TIMEOUT, properties, prefix);
    if (shutdownTimeout != null) {
        cf.setShutdownTimeout(Integer.valueOf(shutdownTimeout));
    }
    Map<String, Object> clientProperties = new HashMap<String, Object>();
    Map<String, Object> defaultClientProperties = AMQConnection.defaultClientProperties();
    clientProperties.putAll(defaultClientProperties);
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        if (entry.getKey().startsWith(prefix + CLIENT_PROPERTIES_PREFIX)) {
            String clientPropertyKey = entry.getKey().substring((prefix + CLIENT_PROPERTIES_PREFIX).length());
            if (defaultClientProperties.containsKey(clientPropertyKey) && (entry.getValue() == null || entry.getValue().trim().isEmpty())) {
                // if default property and value is empty, remove this property
                clientProperties.remove(clientPropertyKey);
            } else {
                clientProperties.put(clientPropertyKey, entry.getValue());
            }
        }
    }
    cf.setClientProperties(clientProperties);
    String automaticRecovery = lookUp(CONNECTION_RECOVERY_ENABLED, properties, prefix);
    if (automaticRecovery != null) {
        cf.setAutomaticRecoveryEnabled(Boolean.valueOf(automaticRecovery));
    }
    String topologyRecovery = lookUp(TOPOLOGY_RECOVERY_ENABLED, properties, prefix);
    if (topologyRecovery != null) {
        cf.setTopologyRecoveryEnabled(Boolean.valueOf(topologyRecovery));
    }
    String networkRecoveryInterval = lookUp(CONNECTION_RECOVERY_INTERVAL, properties, prefix);
    if (networkRecoveryInterval != null) {
        cf.setNetworkRecoveryInterval(Long.valueOf(networkRecoveryInterval));
    }
    String channelRpcTimeout = lookUp(CHANNEL_RPC_TIMEOUT, properties, prefix);
    if (channelRpcTimeout != null) {
        cf.setChannelRpcTimeout(Integer.valueOf(channelRpcTimeout));
    }
    String channelShouldCheckRpcResponseType = lookUp(CHANNEL_SHOULD_CHECK_RPC_RESPONSE_TYPE, properties, prefix);
    if (channelShouldCheckRpcResponseType != null) {
        cf.setChannelShouldCheckRpcResponseType(Boolean.valueOf(channelShouldCheckRpcResponseType));
    }
    String useNio = lookUp(USE_NIO, properties, prefix);
    if (useNio != null && Boolean.valueOf(useNio)) {
        cf.useNio();
        NioParams nioParams = new NioParams();
        String readByteBufferSize = lookUp(NIO_READ_BYTE_BUFFER_SIZE, properties, prefix);
        if (readByteBufferSize != null) {
            nioParams.setReadByteBufferSize(Integer.valueOf(readByteBufferSize));
        }
        String writeByteBufferSize = lookUp(NIO_WRITE_BYTE_BUFFER_SIZE, properties, prefix);
        if (writeByteBufferSize != null) {
            nioParams.setWriteByteBufferSize(Integer.valueOf(writeByteBufferSize));
        }
        String nbIoThreads = lookUp(NIO_NB_IO_THREADS, properties, prefix);
        if (nbIoThreads != null) {
            nioParams.setNbIoThreads(Integer.valueOf(nbIoThreads));
        }
        String writeEnqueuingTime = lookUp(NIO_WRITE_ENQUEUING_TIMEOUT_IN_MS, properties, prefix);
        if (writeEnqueuingTime != null) {
            nioParams.setWriteEnqueuingTimeoutInMs(Integer.valueOf(writeEnqueuingTime));
        }
        String writeQueueCapacity = lookUp(NIO_WRITE_QUEUE_CAPACITY, properties, prefix);
        if (writeQueueCapacity != null) {
            nioParams.setWriteQueueCapacity(Integer.valueOf(writeQueueCapacity));
        }
        cf.setNioParams(nioParams);
    }
    String useSsl = lookUp(SSL_ENABLED, properties, prefix);
    if (useSsl != null && Boolean.valueOf(useSsl)) {
        setUpSsl(cf, properties, prefix);
    }
}
Also used : NioParams(com.rabbitmq.client.impl.nio.NioParams) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URISyntaxException(java.net.URISyntaxException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

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