如何心跳感知与ActiveMQ服务器的连接状态
package com.jms.failover;
import java.io.IOException;
import org.apache.activemq.transport.TransportListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ActiveMQTransportListener implements TransportListener{
protected final Logger logger = LoggerFactory.getLogger(ActiveMQTransportListener.class);
/**
* 对消息传输命令进行监控
* @param command
*/
@Override
public void onCommand(Object o) {
}
/**
* 对监控到的异常进行触发
* @param error
*/
@Override
public void onException(IOException error) {
logger.error("onException -> 消息服务器连接错误......");
}
/**
* 当failover时触发
*/
@Override
public void transportInterupted() {
logger.error("transportInterupted -> 消息服务器连接发生中断......");
//这里就可以状态进行标识了
}
/**
* 监控到failover恢复后进行触发
*/
@Override
public void transportResumed() {
logger.info("transportResumed -> 消息服务器连接已恢复......");
//这里就可以进行状态标识了
}
}
<!-- ActiveMQ 连接工厂,用于连接远程broker -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" destroy-method="stop">
<property name="brokerURL" value="${activemq.broker.failover.uri}" />
<property name="userName" value="${activemq.username}" />
<property name="password" value="${activemq.password}" />
<property name="useAsyncSend" value="true" />
<property name="alwaysSessionAsync" value="true" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
<property name="prefetchPolicy" ref="prefetchPolicy" />
<!-- 消息传输监听器 -->
<property name="transportListener">
<bean class="com.jms.failover.ActiveMQTransportListener" />
</property>
<property name="dispatchAsync" value="true" />
</bean>