如何关闭JAVA客户端和Active-MQ队列之间的特定套接字

时间:2017-02-09 10:39:53

标签: java sockets activemq

我必须通过关闭Active-MQ使用者套接字来测试特定方案,而不会终止java客户端进程和Active-MQ服务器。 在JAVA客户端和Active-MQ队列之间关闭特定套接字的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

可能通过调用org.apache.activemq.ActiveMQMessageConsumer.close()org.apache.activemq.ActiveMQConnection.close()来关闭所有消费者/生产者会话

<强>更新 您可以使用VisualVM并调用stop方法see here

请注意,如果您的客户端使用故障转移,他将自动重新连接

或通过以下代码

按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();
            }
        }
    }
}