mirror of https://github.com/apache/activemq.git
Check for old network Subscriptions as well as connections
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@636420 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f67415d879
commit
58cf400836
|
@ -34,112 +34,170 @@ import org.apache.activemq.broker.BrokerFilter;
|
||||||
import org.apache.activemq.broker.ConnectionContext;
|
import org.apache.activemq.broker.ConnectionContext;
|
||||||
import org.apache.activemq.broker.TransportConnection;
|
import org.apache.activemq.broker.TransportConnection;
|
||||||
import org.apache.activemq.broker.TransportConnector;
|
import org.apache.activemq.broker.TransportConnector;
|
||||||
|
import org.apache.activemq.broker.region.Subscription;
|
||||||
import org.apache.activemq.command.ActiveMQMessage;
|
import org.apache.activemq.command.ActiveMQMessage;
|
||||||
import org.apache.activemq.command.BrokerId;
|
import org.apache.activemq.command.BrokerId;
|
||||||
import org.apache.activemq.command.ConnectionId;
|
import org.apache.activemq.command.ConnectionId;
|
||||||
import org.apache.activemq.command.ConnectionInfo;
|
import org.apache.activemq.command.ConnectionInfo;
|
||||||
|
import org.apache.activemq.command.ConsumerId;
|
||||||
|
import org.apache.activemq.command.ConsumerInfo;
|
||||||
import org.apache.activemq.command.DataStructure;
|
import org.apache.activemq.command.DataStructure;
|
||||||
import org.apache.activemq.command.Message;
|
import org.apache.activemq.command.Message;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Monitors for client connections that may fail to another
|
* Monitors for client connections that may fail to another broker - but this
|
||||||
* broker - but this broker isn't aware they've gone.
|
* broker isn't aware they've gone. Can occur with network glitches or client
|
||||||
* Can occur with network glitches or client error
|
* error
|
||||||
*
|
*
|
||||||
* @version $Revision$
|
* @version $Revision$
|
||||||
*/
|
*/
|
||||||
public class ConnectionSplitBroker extends BrokerFilter implements MessageListener{
|
public class ConnectionSplitBroker extends BrokerFilter implements
|
||||||
private static final Log LOG = LogFactory.getLog(ConnectionSplitBroker.class);
|
MessageListener {
|
||||||
|
private static final Log LOG = LogFactory
|
||||||
|
.getLog(ConnectionSplitBroker.class);
|
||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private Map <ConnectionId,ConnectionContext>clientMap = new ConcurrentHashMap<ConnectionId,ConnectionContext>();
|
|
||||||
|
private Map<ConnectionId, ConnectionContext> clientMap = new ConcurrentHashMap<ConnectionId, ConnectionContext>();
|
||||||
|
private Map<ConsumerId,ConsumerInfo>consumerMap = new ConcurrentHashMap<ConsumerId,ConsumerInfo>();
|
||||||
public ConnectionSplitBroker(Broker next) {
|
public ConnectionSplitBroker(Broker next) {
|
||||||
super(next);
|
super(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {
|
public void addConnection(ConnectionContext context, ConnectionInfo info)
|
||||||
if (info != null){
|
throws Exception {
|
||||||
clientMap.put(info.getConnectionId(),context);
|
if (info != null) {
|
||||||
}
|
removeStaleConnection(info);
|
||||||
|
clientMap.put(info.getConnectionId(), context);
|
||||||
|
}
|
||||||
super.addConnection(context, info);
|
super.addConnection(context, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable error)
|
public void removeConnection(ConnectionContext context,
|
||||||
throws Exception {
|
ConnectionInfo info, Throwable error) throws Exception {
|
||||||
if (info != null){
|
if (info != null) {
|
||||||
clientMap.remove(info.getConnectionId());
|
clientMap.remove(info.getConnectionId());
|
||||||
}
|
}
|
||||||
super.removeConnection(context, info, error);
|
super.removeConnection(context, info, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws Exception{
|
public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception{
|
||||||
super.start();
|
|
||||||
ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(getBrokerService().getVmConnectorURI());
|
if (info.isNetworkSubscription()) {
|
||||||
fac.setCloseTimeout(1);
|
List<ConsumerId>list = info.getNetworkConsumerIds();
|
||||||
fac.setWarnAboutUnstartedConnectionTimeout(10000);
|
for (ConsumerId id:list) {
|
||||||
fac.setWatchTopicAdvisories(false);
|
consumerMap.put(id,info);
|
||||||
fac.setAlwaysSessionAsync(true);
|
}
|
||||||
fac.setClientID(getBrokerId().toString()+":" + getBrokerName() + ":ConnectionSplitBroker");
|
}else {
|
||||||
connection = fac.createConnection();
|
ConsumerInfo networkInfo = consumerMap.get(info.getConsumerId());
|
||||||
connection.start();
|
if (networkInfo != null) {
|
||||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
networkInfo.removeNetworkConsumerId(info.getConsumerId());
|
||||||
MessageConsumer consumer = session.createConsumer(AdvisorySupport.getConnectionAdvisoryTopic());
|
if (networkInfo.isNetworkConsumersEmpty()) {
|
||||||
consumer.setMessageListener(this);
|
consumerMap.remove(info.getConsumerId());
|
||||||
}
|
super.removeConsumer(context,networkInfo);
|
||||||
|
}
|
||||||
public synchronized void stop() throws Exception{
|
|
||||||
if (connection != null){
|
}
|
||||||
connection.stop();
|
}
|
||||||
connection = null;
|
return super.addConsumer(context, info);
|
||||||
}
|
|
||||||
super.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onMessage(javax.jms.Message m) {
|
|
||||||
ActiveMQMessage message = (ActiveMQMessage) m;
|
public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception{
|
||||||
|
if (info.isNetworkSubscription()) {
|
||||||
DataStructure o = message.getDataStructure();
|
List<ConsumerId>list = info.getNetworkConsumerIds();
|
||||||
if (o != null && o.getClass() == ConnectionInfo.class) {
|
for (ConsumerId id:list) {
|
||||||
ConnectionInfo info = (ConnectionInfo) o;
|
consumerMap.remove(id);
|
||||||
String brokerId=null;
|
}
|
||||||
try {
|
|
||||||
brokerId = message.getStringProperty(AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_ID);
|
|
||||||
if (brokerId != null && !brokerId.equals(getBrokerId().getValue())) {
|
|
||||||
//see if it already exits
|
|
||||||
ConnectionContext old = clientMap.remove(info.getConnectionId());
|
|
||||||
if (old != null && old.getConnection() != null) {
|
|
||||||
String str = "connectionId=" + old.getConnectionId() +",clientId="+old.getClientId();
|
|
||||||
LOG.warn("Removing stale connection: " + str);
|
|
||||||
try {
|
|
||||||
//remove connection states
|
|
||||||
TransportConnection connection = (TransportConnection) old.getConnection();
|
|
||||||
connection.processRemoveConnection(old.getConnectionId());
|
|
||||||
connection.stopAsync();
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("Failed to remove stale connection: " + str,e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JMSException e) {
|
|
||||||
LOG.warn("Failed to get message property "+ AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_ID,e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
super.removeConsumer(context, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean contains(BrokerId[] brokerPath, BrokerId brokerId) {
|
public void start() throws Exception {
|
||||||
if (brokerPath != null) {
|
super.start();
|
||||||
for (int i = 0; i < brokerPath.length; i++) {
|
ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(
|
||||||
if (brokerId.equals(brokerPath[i])) {
|
getBrokerService().getVmConnectorURI());
|
||||||
return true;
|
fac.setCloseTimeout(1);
|
||||||
}
|
fac.setWarnAboutUnstartedConnectionTimeout(10000);
|
||||||
|
fac.setWatchTopicAdvisories(false);
|
||||||
|
fac.setAlwaysSessionAsync(true);
|
||||||
|
fac.setClientID(getBrokerId().toString() + ":" + getBrokerName()
|
||||||
|
+ ":ConnectionSplitBroker");
|
||||||
|
connection = fac.createConnection();
|
||||||
|
connection.start();
|
||||||
|
Session session = connection.createSession(false,
|
||||||
|
Session.AUTO_ACKNOWLEDGE);
|
||||||
|
MessageConsumer consumer = session.createConsumer(AdvisorySupport
|
||||||
|
.getConnectionAdvisoryTopic());
|
||||||
|
consumer.setMessageListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void stop() throws Exception {
|
||||||
|
if (connection != null) {
|
||||||
|
connection.stop();
|
||||||
|
connection = null;
|
||||||
|
}
|
||||||
|
super.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onMessage(javax.jms.Message m) {
|
||||||
|
ActiveMQMessage message = (ActiveMQMessage) m;
|
||||||
|
|
||||||
|
DataStructure o = message.getDataStructure();
|
||||||
|
if (o != null && o.getClass() == ConnectionInfo.class) {
|
||||||
|
ConnectionInfo info = (ConnectionInfo) o;
|
||||||
|
|
||||||
|
String brokerId = null;
|
||||||
|
try {
|
||||||
|
brokerId = message
|
||||||
|
.getStringProperty(AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_ID);
|
||||||
|
if (brokerId != null
|
||||||
|
&& !brokerId.equals(getBrokerId().getValue())) {
|
||||||
|
// see if it already exits
|
||||||
|
removeStaleConnection(info);
|
||||||
|
}
|
||||||
|
} catch (JMSException e) {
|
||||||
|
LOG.warn("Failed to get message property "
|
||||||
|
+ AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_ID, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean contains(BrokerId[] brokerPath, BrokerId brokerId) {
|
||||||
|
if (brokerPath != null) {
|
||||||
|
for (int i = 0; i < brokerPath.length; i++) {
|
||||||
|
if (brokerId.equals(brokerPath[i])) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void removeStaleConnection(ConnectionInfo info) {
|
||||||
|
// see if it already exits
|
||||||
|
ConnectionContext old = clientMap.remove(info
|
||||||
|
.getConnectionId());
|
||||||
|
if (old != null && old.getConnection() != null) {
|
||||||
|
String str = "connectionId=" + old.getConnectionId()
|
||||||
|
+ ",clientId=" + old.getClientId();
|
||||||
|
LOG.warn("Removing stale connection: " + str);
|
||||||
|
try {
|
||||||
|
// remove connection states
|
||||||
|
TransportConnection connection = (TransportConnection) old
|
||||||
|
.getConnection();
|
||||||
|
connection.processRemoveConnection(old
|
||||||
|
.getConnectionId());
|
||||||
|
connection.stopAsync();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("Failed to remove stale connection: "
|
||||||
|
+ str, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue