Is it possible to set the ActiveMQ transport parameters such as maxReconnectAttempts
using a java API at the runtime?
In my case I'm creating the ActiveMQ connection factory initially by providing a basic failover url failover:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory("(ssl://192.168.1.112:61617,ssl://192.168.1.112:61619)?randomize=false")
However later I would require to set transport parameter to this connection factory such as maxReconnectAttempts
. Is it possible?
certainly, simply like this:
ActiveMQSslConnectionFactory connectionFactory = new ActiveMQSslConnectionFactory("failover:(ssl://192.168.1.112:61617,ssl://192.168.1.112:61619)?randomize=false&maxReconnectAttempts=Value")
All failover Transport Options can be set in the url
http://activemq.apache.org/failover-transport-reference.html
If you want to change the url later, you can call connectionFactory.setBrokerURL("newURL")
, after that all new connections created will be configured with the new parameters of the url.
if you want to change that after the creation of the ConnectionFactory, keep in mind that a new instance of FailoverTransport is created for each new Connection based on the url parameters and each Connection holds an instance of his FailoverTransport, so to change his state you can access it like this :
((FailoverTransport) ((TransportFilter) ((TransportFilter) ((ActiveMQConnection) connection).getTransport()).getNext()).getNext())
.setMaxReconnectAttempts(10);
or more readable :
org.apache.activemq.transport.TransportFilter responseCorrelator = (TransportFilter) ((ActiveMQConnection) connection).getTransport();
TransportFilter mutexTransport = (TransportFilter) responseCorrelator.getNext();
FailoverTransport failoverTransport = (FailoverTransport) mutexTransport.getNext();
failoverTransport.setMaxReconnectAttempts(10);
to understand why all these casts, you can take a look at source code of this method :
org.apache.activemq.transport.failover.FailoverTransportFactory.doConnect(URI)