Activemq reconnection Mechanism

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.