The reconnection mechanism is one of the specific manifestations of activemq's high availability.
Specifically, you can use the Failover method to connect to one or more brokerurls after the connection is disconnected.
For example: failover :( TCP: // 127.0.0.1: 61616). Multiple URLs can be used here.
By default, if the client and the broker are disconnected directly, the client starts a new thread,
Continuously obtain a URL from the URL parameter to retry the connection.
This mechanism has a problem with the connection wood used in the container.
In the test of activemq-core source code, multiple clients and brokers are disconnected and reconnected 10 times:
Http://svn.apache.org/repos/asf/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/transport/failover/ReconnectTest.java
But for a simple implementation of an independent running client, generally reconnect once will appear process exit BUG: https://issues.apache.org/jira/browse/AMQ-796
The test is as follows:
1. Start a broker through the command line
2. Create a connection in Java and add a transportlistener. Use the following code to disconnect and reconnect the connection.
@ Override
Public void transportinterupted (){
System. Out. println ("==>> disconnected ");
}
@ Override
Public void transportresumed (){
System. Out. println ("==>> reconnect ");
}
3. Run the program
4. Stop the broker and then start the broker.
5. Disconnection and reconnection are observed.
6. Stop the broker and find that the program exits automatically.
The reason why the program does not reconnect again is that the re-connection thread is daemon. After a connection error occurs, all other threads exit and the thread is destroyed immediately.
Once officially fixed, daemon = false was set on activemq connection executor, but this thread may not be created. So the bug is still there.
The solution is simple:
Failovertransport. Java's 132 line
Reconnecttaskfactory = new taskrunnerfactory ();
Reconnecttaskfactory. setdaemon (false); // To Set Daemon = false by kimmking
Reconnecttaskfactory. INIT ();
Set the reconnection thread to daemon = false.
Then, follow the steps above and find that the broker can be automatically reconnected after multiple restart.