在ubuntu下启动ActiveMq5.5.1,并配置使其可用JMX远程访问,这样配置的需求是:必须管理队列,可删除队列中指定信息。
配置中需要注意:
1. 在conf/activemq.xml中broker标签中添加 useJmx="true"属性;
2. 将启动文件bin/activemq 中的配置放开
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=11099 "
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.password.file=${ACTIVEMQ_CONFIG_DIR}/jmx.password"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.access.file=${ACTIVEMQ_CONFIG_DIR}/jmx.access"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.ssl=false"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote"
# Set jvm jmx configuration for controlling the broker process
# You only have to configure the first four settings if you run a ibm jvm, caused by the
# fact that IBM's jvm does not support VirtualMachine.attach(PID)
# (see also com.sun.management.jmxremote.port, .jmx.password.file and .jmx.access.file )
ACTIVEMQ_SUNJMX_CONTROL="--jmxurl service:jmx:rmi:///jndi/rmi://127.0.0.1:11099/jmxrmi --jmxuser controlRole --jmxpassword abcd1234"
#ACTIVEMQ_SUNJMX_CONTROL=""
3. 使用命令 sudo ./activemq console 启动
会出现以下异常:Error: Password file read access must be restricted: /opt/activemq/conf/jmx.password
原因是因为对文件没有访问权限
执行
sudo chmod 600 /opt/activemq/conf/jmx.password
sudo chmod 600 /opt/activemq/conf/jmx.access
即可正常启动
4. 在客户端使用java代码链接
RemoteJMXBrokerFacade createConnector = new RemoteJMXBrokerFacade();
// 填写链接属性
System.setProperty("webconsole.jmx.url",
"service:jmx:rmi:///jndi/rmi://192.168.1.5:11099/jmxrmi");
System.setProperty("webconsole.jmx.user", "admin");
System.setProperty("webconsole.jmx.password", "activemq");
会出现异常:
java.lang.RuntimeException: java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:
原因 在ubuntu下host设置不正确,修改/etc/hosts文件,对应的127.0.1.1为192.168.1.5 重启activemq可正常连接
5. 删除队列中的某个消息java代码
String queueName="queue"; //队列名称
String id =""; //队列中要删除信息的id
QueueViewMBean qvm = createConnector.getQueue(queueName);
qvm.removeMessage(id);