This closes #218

This commit is contained in:
Clebert Suconic 2015-10-26 14:08:11 -04:00
commit 7f60ff20a5
1 changed files with 53 additions and 28 deletions

View File

@ -32,6 +32,8 @@ import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ClientMessage; import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery; import org.apache.activemq.artemis.api.core.client.ClientSession.QueueQuery;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory; import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.FailoverEventListener;
import org.apache.activemq.artemis.api.core.client.FailoverEventType;
import org.apache.activemq.artemis.api.core.client.MessageHandler; import org.apache.activemq.artemis.api.core.client.MessageHandler;
import org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal; import org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal;
import org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal; import org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal;
@ -48,7 +50,7 @@ import org.apache.activemq.artemis.utils.VersionLoader;
/** /**
* The message handler * The message handler
*/ */
public class ActiveMQMessageHandler implements MessageHandler { public class ActiveMQMessageHandler implements MessageHandler, FailoverEventListener {
/** /**
* Trace enabled * Trace enabled
@ -80,6 +82,8 @@ public class ActiveMQMessageHandler implements MessageHandler {
private ClientSessionFactory cf; private ClientSessionFactory cf;
private volatile boolean connected;
public ActiveMQMessageHandler(final ActiveMQActivation activation, public ActiveMQMessageHandler(final ActiveMQActivation activation,
final TransactionManager tm, final TransactionManager tm,
final ClientSessionInternal session, final ClientSessionInternal session,
@ -187,6 +191,8 @@ public class ActiveMQMessageHandler implements MessageHandler {
endpoint = endpointFactory.createEndpoint(null); endpoint = endpointFactory.createEndpoint(null);
useXA = false; useXA = false;
} }
connected = true;
session.addFailoverListener(this);
consumer.setMessageHandler(this); consumer.setMessageHandler(this);
} }
@ -224,6 +230,8 @@ public class ActiveMQMessageHandler implements MessageHandler {
ActiveMQRALogger.LOGGER.debug("Error releasing endpoint " + endpoint, t); ActiveMQRALogger.LOGGER.debug("Error releasing endpoint " + endpoint, t);
} }
//only do this if we haven't been disconnected at some point whilst failing over
if (connected) {
try { try {
consumer.close(); consumer.close();
if (activation.getTopicTemporaryQueue() != null) { if (activation.getTopicTemporaryQueue() != null) {
@ -249,7 +257,6 @@ public class ActiveMQMessageHandler implements MessageHandler {
catch (Throwable t) { catch (Throwable t) {
ActiveMQRALogger.LOGGER.debug("Error releasing session " + session, t); ActiveMQRALogger.LOGGER.debug("Error releasing session " + session, t);
} }
try { try {
if (cf != null) { if (cf != null) {
cf.close(); cf.close();
@ -259,6 +266,19 @@ public class ActiveMQMessageHandler implements MessageHandler {
ActiveMQRALogger.LOGGER.debug("Error releasing session factory " + session, t); ActiveMQRALogger.LOGGER.debug("Error releasing session factory " + session, t);
} }
} }
else {
//otherwise we just clean up
try {
if (cf != null) {
cf.cleanup();
}
}
catch (Throwable t) {
ActiveMQRALogger.LOGGER.debug("Error releasing session factory " + session, t);
}
}
}
public void onMessage(final ClientMessage message) { public void onMessage(final ClientMessage message) {
if (ActiveMQMessageHandler.trace) { if (ActiveMQMessageHandler.trace) {
@ -364,4 +384,9 @@ public class ActiveMQMessageHandler implements MessageHandler {
public void start() throws ActiveMQException { public void start() throws ActiveMQException {
session.start(); session.start();
} }
@Override
public void failoverEvent(FailoverEventType eventType) {
connected = eventType == FailoverEventType.FAILOVER_COMPLETED;
}
} }