[英]How to close specific socket between JAVA client and Active-MQ queue
I have to test a specific scenario by closing the Active-MQ consumer socket without killing the java client process and Active-MQ server. 我必须通过关闭Active-MQ使用者套接字来测试特定情况,而又不终止Java客户端进程和Active-MQ服务器。 what is the best way to close specific socket between JAVA client and Active-MQ queue?
关闭JAVA客户端和Active-MQ队列之间的特定套接字的最佳方法是什么?
maybe by calling org.apache.activemq.ActiveMQMessageConsumer.close()
or org.apache.activemq.ActiveMQConnection.close()
to close all sessions of consumers/producers 也许通过调用
org.apache.activemq.ActiveMQMessageConsumer.close()
或org.apache.activemq.ActiveMQConnection.close()
关闭消费者/生产者的所有会话
UPDATE you can use VisualVM and call stop method, UPDATE可以使用VisualVM和调用stop方法,
Please consider that if your client is using failover he will reconnect automatically 请注意,如果您的客户端正在使用故障转移,他将自动重新连接
or by code like below 或像下面的代码
to close 1 consumer by ID or RemoteAddress 通过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(); } } }
to close all consumers by connector name 通过连接器名称关闭所有使用者
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();
}
}
}
}
or 要么
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();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.