二.有密码方式连接
1.检查 ActiveMQ 所在服务器 hosts 文件的设置:
1).使用命令: cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2).将 127.0.0.1 改为 ActiveMQ 所在服务器的 IP
192.168.242.128 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2.在 apache-activemq-5.12.0/conf 目录下修改 activemq.xml 文件
注:在标签 <managementContext > 中可以添加 connectorHost="ActiveMQ 服务器所在服务器IP"
3.在apache-activemq-5.12.0/bin 目录的 activemq 文件最后一行加上如下配置:
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=11099 "
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.password.file=${ACTIVEMQ_CONF}/jmx.password"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.access.file=${ACTIVEMQ_CONF}/jmx.access"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.ssl=false"
4.重启 ActiveMQ 服务器
5.在本地使用 jconsole 远程连接输入:
192.168.242.128:11099
admin(用户名)/activemq(密码)
注: 在 apache-activemq-5.12.0/conf 目录下有两个文件:
jmx.access(设置用户权限的文件)
jmx.password(设置子用户名和密码的文件)
三.通过 JMX 的编程接口来与 broker 进行交互,观察 broker 的 jmx 中都提供的东西:
import java.util.Iterator;
import java.util.Set;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class TestJMX {
public static void main(String[] args) throws Exception {
String surl = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
JMXServiceURL url = new JMXServiceURL(surl);
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
System.out.println("Domains:---------------");
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("\tDomain[" + i + "] = " + domains[i]);
}
System.out.println("all ObjectName:---------------");
Set
set = mbsc.queryMBeans(null, null);
for (Iterator
it = set.iterator(); it.hasNext();) {
ObjectInstance oi = (ObjectInstance) it.next();
System.out.println("\t" + oi.getObjectName());
}
System.out.println("org.apache.activemq:BrokerName=localhost,Type=Broker:---------------");
ObjectName mbeanName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
System.out.println("Class: " + info.getClassName());
if (info.getAttributes().length > 0){
for(MBeanAttributeInfo m : info.getAttributes())
System.out.println("\t ==> Attriber:" + m.getName());
}
if (info.getOperations().length > 0){
for(MBeanOperationInfo m : info.getOperations())
System.out.println("\t ==> Operation:" + m.getName());
}
jmxc.close();
}
}