使用JMX远程连接到Active-MQ

时间:2016-09-13 13:03:23

标签: activemq jmx

我有一个用于监视Active MQ(版本5.13.0)上各个队列的java应用程序。此应用程序的目标是连接到Active MQ代理并找出特定队列的传输消息计数。如果飞行中的消息计数超过50,则会发送警报。此应用程序每10分钟执行一次此检查(使用Quartz Scheduler)。 当我连接到使用IP地址连接到AMQ的AMQ代理时,我收到带有以下堆栈跟踪的IOException -

    java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
        java.net.ConnectException: Connection refused: connect
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
        at sun.rmi.server.UnicastRef.invoke(Unknown Source)
        at javax.management.remote.rmi.RMIServerImpl_Stub.newClient(Unknown Source)
        at javax.management.remote.rmi.RMIConnector.getConnection(Unknown Source)
        at javax.management.remote.rmi.RMIConnector.connect(Unknown Source)
        at javax.management.remote.JMXConnectorFactory.connect(Unknown Source)
        at javax.management.remote.JMXConnectorFactory.connect(Unknown Source)
        at com.globalcharge.quartz.job.CheckPendingConsumer.execute(CheckPendingConsumer.java:51)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
        ... 12 more

我使用以下代码通过JMX连接到AMQ代理

JMXServiceURL url = new     JMXServiceURL("service:jmx:rmi:///jndi/rmi://192.168.11.72:1099/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection conn = jmxc.getMBeanServerConnection(); 

ObjectName activeMq = new ObjectName("org.apache.activemq:Type=Broker,BrokerName=localhost");

BrokerViewMBean mbean = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, activeMq, BrokerViewMBean.class, true);
//for each queue 
for(ObjectName name : mbean.getQueues())
{
    if(("dbg.notmanager.netsizeDelivery".equals(name)))
    {
        //obtain an instance of QueueViewMBean 
        QueueViewMBean queueMbean = (QueueViewMBean)MBeanServerInvocationHandler.newProxyInstance(conn, name, QueueViewMBean.class, true);
        long inFlight = queueMbean.getInFlightCount(); 
        if(inFlight >= 50)
        {
            sender.sendPassiveCriticalAlert(hostname, "Passive.GCDBG", "InFlight Messages for a consumer on " + name);
        }           
}

注意: 1.如果我在本地计算机上安装AMQ并将上面的代码部署在我本地计算机上运行的Tomcat上,则可以正常运行。我的意思是,当(Java客户端和AMQ)都在同一台机器上时,它可以工作 2.我已在AMQ服务器上完成了必要的配置,如此处所述configure JMX for ActiveMQ for remoting access 3.我已经通过telnet检查了代码中提到的IP和端口的连接,它可以工作。 4.我还在AMQ服务器上记录了TCP转储,我可以看到请求到达那里。 5.两台计算机(运行Tomcat和AMQ的计算机)不是虚拟机,而是同一网络中的专用物理计算机。

请帮忙:)

2 个答案:

答案 0 :(得分:0)

如日志所说Connection refused,我认为连接配置错误。 你也需要这些证书

JMXServiceURL url = ...;
Map env = ...;
String[] creds = {"admin", "activemq"};
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);

答案 1 :(得分:0)

我现在可以使用JConsole以及我编写的Java代码进行连接。问题在env文件中(在amqHome / bin /下)有一个属性java.rmi.server.hostname,其值为127.0.0.1 我将此值更改为192.168.11.72(机器的IP)并且它有效....

感谢大家的支持......