Skip to content

Commit

Permalink
Support idle timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
acogoluegnes committed Apr 17, 2024
1 parent b60fda8 commit b28c426
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/rabbitmq/model/ConnectionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// [email protected].
package com.rabbitmq.model;

import java.time.Duration;
import java.util.List;
import java.util.function.Function;

Expand All @@ -38,5 +39,7 @@ public interface ConnectionSettings<T> {

T credentialsProvider(CredentialsProvider credentialsProvider);

T idleTimeout(Duration idleTimeout);

T addressSelector(Function<List<Address>, Address> selector);
}
2 changes: 2 additions & 0 deletions src/main/java/com/rabbitmq/model/amqp/AmqpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private org.apache.qpid.protonj2.client.Connection connect(
connectionOptions.virtualHost("vhost:" + connectionSettings.virtualHost());
// only the mechanisms supported in RabbitMQ
connectionOptions.saslOptions().addAllowedMechanism("PLAIN").addAllowedMechanism("EXTERNAL");
connectionOptions.idleTimeout(
connectionSettings.idleTimeout().toMillis(), TimeUnit.MILLISECONDS);
connectionOptions.disconnectedHandler(disconnectHandler);
if (name != null) {
connectionOptions.properties(singletonMap("connection_name", name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public ConnectionBuilder credentialsProvider(CredentialsProvider credentialsProv
return this.connectionSettings.credentialsProvider(credentialsProvider);
}

@Override
public ConnectionBuilder idleTimeout(Duration idleTimeout) {
return this.connectionSettings.idleTimeout(idleTimeout);
}

@Override
public ConnectionBuilder addressSelector(Function<List<Address>, Address> selector) {
return this.connectionSettings.addressSelector(selector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import org.apache.qpid.protonj2.client.ConnectionOptions;

abstract class DefaultConnectionSettings<T> implements ConnectionSettings<T> {

Expand All @@ -46,6 +48,7 @@ void copyTo(ConnectionSettings<?> copy) {
copy.virtualHost(this.virtualHost);
copy.uris(this.uris.stream().map(URI::toString).toArray(String[]::new));
copy.addressSelector(this.addressSelector);
copy.idleTimeout(this.idleTimeout);
}

private String host = DEFAULT_HOST;
Expand All @@ -54,6 +57,7 @@ void copyTo(ConnectionSettings<?> copy) {
new DefaultUsernamePasswordCredentialsProvider(DEFAULT_USERNAME, DEFAULT_PASSWORD);
private String virtualHost = DEFAULT_VIRTUAL_HOST;
private List<URI> uris = Collections.emptyList();
private Duration idleTimeout = Duration.ofMillis(ConnectionOptions.DEFAULT_IDLE_TIMEOUT);
private static final Random RANDOM = new Random();
private Function<List<Address>, Address> addressSelector =
addresses -> {
Expand Down Expand Up @@ -131,6 +135,15 @@ public T credentialsProvider(CredentialsProvider credentialsProvider) {
return this.toReturn();
}

@Override
public T idleTimeout(Duration idleTimeout) {
if (idleTimeout.isNegative()) {
throw new IllegalArgumentException("Idle timeout cannot be negative");
}
this.idleTimeout = idleTimeout;
return this.toReturn();
}

@Override
public T addressSelector(Function<List<Address>, Address> selector) {
this.addressSelector = selector;
Expand All @@ -145,6 +158,10 @@ String virtualHost() {
return virtualHost;
}

Duration idleTimeout() {
return idleTimeout;
}

Address selectAddress() {
return this.addressSelector.apply(this.addresses);
}
Expand Down

0 comments on commit b28c426

Please sign in to comment.