我必须通过关闭Active-MQ使用者套接字来测试特定方案,而不会终止java客户端进程和Active-MQ服务器。 在JAVA客户端和Active-MQ队列之间关闭特定套接字的最佳方法是什么?
答案 0 :(得分:0)
可能通过调用org.apache.activemq.ActiveMQMessageConsumer.close()
或org.apache.activemq.ActiveMQConnection.close()
来关闭所有消费者/生产者会话
<强>更新强> 您可以使用VisualVM并调用stop方法
请注意,如果您的客户端使用故障转移,他将自动重新连接
或通过以下代码
按ID或RemoteAddress关闭1个消费者
import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.management.MBeanServerConnection; import javax.management.MBeanServerInvocationHandler; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import org.apache.activemq.broker.jmx.ConnectionViewMBean; public class JMXCloseConsumer { public static void main(String[] args) throws Exception { JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"); Map<String, String[]> env = new HashMap<>(); String[] creds = { "admin", "admin" }; env.put(JMXConnector.CREDENTIALS, creds); JMXConnector jmxc = JMXConnectorFactory.connect(url, env); MBeanServerConnection conn = jmxc.getMBeanServerConnection(); ObjectName conName = new ObjectName( "org.apache.activemq:type=Broker,brokerName=localhost,connector=clientConnectors,connectorName=openwire,connectionViewType=clientId,connectionName=*"); Set connections = conn.queryNames(conName, null); Object[] c = connections.toArray(); for (int s = 0; s < c.length; s++) { ObjectName con = (ObjectName) c[s]; System.out.println(con.toString()); ConnectionViewMBean connectionView = MBeanServerInvocationHandler.newProxyInstance(conn, con, ConnectionViewMBean.class, true); String address = connectionView.getRemoteAddress(); System.out.println(address); connectionView.stop(); } } }
按连接器名称
关闭所有使用者import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.ConnectorViewMBean;
public class JMXCloseAllConsumers {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
Map<String, String[]> env = new HashMap<>();
String[] creds = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection conn = jmxc.getMBeanServerConnection();
ObjectName activeMq = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(conn, activeMq, BrokerViewMBean.class,
true);
Map<String, String> props = mbean.getTransportConnectors();
for (Iterator<?> iter = props.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String value1 = props.get(name);
System.out.println(value1);
ObjectName connector = new ObjectName(
"org.apache.activemq:type=Broker,brokerName=localhost,connector=clientConnectors,connectorName="
+ name);
ConnectorViewMBean connectorViewMBean = MBeanServerInvocationHandler.newProxyInstance(conn, connector,
ConnectorViewMBean.class, true);
System.out.println(connectorViewMBean.connectionCount());
if (connectorViewMBean.connectionCount() > 0) {
connectorViewMBean.stop();
}
}
}
}
或
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.ConnectionViewMBean;
import org.apache.activemq.broker.jmx.ConnectorViewMBean;
public class JMXCloseAllConsumers {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
Map<String, String[]> env = new HashMap<>();
String[] creds = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection conn = jmxc.getMBeanServerConnection();
ObjectName activeMq = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(conn, activeMq, BrokerViewMBean.class,
true);
Map<String, String> props = mbean.getTransportConnectors();
for (Iterator<?> iter = props.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
System.out.println(name);
ObjectName connector = new ObjectName(
"org.apache.activemq:type=Broker,brokerName=localhost,connector=clientConnectors,connectorName="
+ name);
ConnectorViewMBean connectorViewMBean = MBeanServerInvocationHandler.newProxyInstance(conn, connector,
ConnectorViewMBean.class, true);
System.out.println(connectorViewMBean.connectionCount());
if (connectorViewMBean.connectionCount() > 0) {
ObjectName conName = new ObjectName(
"org.apache.activemq:type=Broker,brokerName=localhost,connector=clientConnectors,connectorName="
+ name + ",connectionViewType=clientId,connectionName=*");
Set connections = conn.queryNames(conName, null);
Object[] c = connections.toArray();
for (int s = 0; s < c.length; s++) {
ObjectName con = (ObjectName) c[s];
System.out.println(con.toString());
ConnectionViewMBean connectionView = MBeanServerInvocationHandler.newProxyInstance(conn, con,
ConnectionViewMBean.class, true);
String address = connectionView.getRemoteAddress();
System.out.println(address);
connectionView.stop();
}
connectorViewMBean.stop();
}
}
}
}