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);
}
}
}
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<>();
}
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);
}
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());
}
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);
}
}
Aggregations