简体   繁体   English

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

[英]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.

相关问题 如何在两侧使用 Active-mq (jBoss EAP 7.2) 配置远程排队 - How to configure remote queueing using Active-mq (jBoss EAP 7.2) on both sides 如何在java中关闭客户端套接字 - how to close the client socket in java 在Active MQ队列中搜索特定消息 - Search for a specific message in Active MQ queue 如何正确关闭Java客户端套接字? - How to close java client socket correctly? 一旦启动了 active-mq 代理,是否可以创建 virtualDestinations 和复合队列? - Is it possible to create virtualDestinations and compositeQueue once the active-mq broker has been started up? 无法在不同的Spring启动应用程序中的不同端口上启动2个嵌入式active-mq - Could not start 2 embedded active-mq on different ports within different spring boot applications 如何使用正确的协议从基于Java的程序接收活动的MQ Web套接字践踏 - How to use the correct protocol for receiving active mq web socket stomp from java based program Java IBM MQ Client已连接但未从队列中获取消息 - Java IBM MQ Client connected but not getting messages from queue Java套接字。 服务器如何知道客户端关闭流 - java socket. How to server know client close stream 如何使用JMS将响应xml放置在活动MQ响应队列中 - How to place a response xml in active MQ response queue using JMS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM