activemq开启 jmx

来源:互联网 发布:天天pk10计划软件 编辑:程序博客网 时间:2024/05/01 18:40

博主这边activemq使用的是5.13的版本


activemq开启jmx还是相对简单的

首先说说不用密码的的方式开始jmx

1.修改activemq。xml的配置文件

<beans  xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <value>file:${activemq.conf}/credentials.properties</value>        </property>    </bean>  <bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"          lazy-init="false" scope="singleton"          init-method="start" destroy-method="stop">    </bean>    <broker xmlns="http://activemq.apache.org/schema/core"<span style="color:#FF0000;"><strong> useJmx="true"</strong></span> brokerName="localhost" dataDirectory="${activemq.data}">        <destinationPolicy>            <policyMap>              <policyEntries>                <policyEntry topic=">" >                    <!-- The constantPendingMessageLimitStrategy is used to prevent                         slow topic consumers to block producers and affect other consumers                         by limiting the number of messages that are retained                         For more information, see:                         http://activemq.apache.org/slow-consumer-handling.html                    -->                  <pendingMessageLimitStrategy>                    <constantPendingMessageLimitStrategy limit="1000"/>                  </pendingMessageLimitStrategy>                </policyEntry>              </policyEntries>            </policyMap>        </destinationPolicy>        <managementContext>            <managementContext <span style="color:#FF0000;">createConnector="true" connectorPort="1099"  connectorHost="192.168.157.128</span>"/>        </managementContext>        <persistenceAdapter>            <kahaDB directory="${activemq.data}/kahadb"/>        </persistenceAdapter>          <systemUsage>            <systemUsage>                <memoryUsage>                    <memoryUsage percentOfJvmHeap="70" />                </memoryUsage>                <storeUsage>                    <storeUsage limit="100 gb"/>                </storeUsage>                <tempUsage>                    <tempUsage limit="50 gb"/>                </tempUsage>            </systemUsage>        </systemUsage>        <transportConnectors>            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>        </transportConnectors>       <shutdownHooks>            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />        </shutdownHooks>    </broker>    <import resource="jetty.xml"/></beans>
修改上面的红色的地方 然后启动mq就开始不用密码的jmx了

activemq  java代码访问如下

import com.yihu.ehr.mqhelper.config.ActiveMQConfig;import org.apache.activemq.web.RemoteJMXBrokerFacade;import org.apache.activemq.web.config.SystemPropertiesConfiguration;/** * Created by chenweida on 2015/12/19. */class ActiveMQJMSHelper {    /**     * 得到队列大小     *     * @return     * @queueName 队列名字     */    public Long getQueueSize(String queueName) throws Exception {        return getRemoteJMXBrokerFacade().getQueue(queueName).getQueueSize();    }    /**     * 得到队列的消费者     *     * @return     * @queueName 队列名字     */    public Long getConsumerCount(String queueName) throws Exception {        return getRemoteJMXBrokerFacade().getQueue(queueName).getConsumerCount();    }    /**     * 得到入列消息数量     *     * @return     * @queueName 队列名字     */    public Long getEnqueueCount(String queueName) throws Exception {        return getRemoteJMXBrokerFacade().getQueue(queueName).getEnqueueCount();    }    /**     * 得到出列消息数量     *     * @return     * @queueName 队列名字     */    public Long getDequeueCount(String queueName) throws Exception {        return getRemoteJMXBrokerFacade().getQueue(queueName).getDequeueCount();    }    /**     * 得到队列数     *     * @return     * @queueName 队列名字     */    public int getQueueCount() throws Exception {        return getRemoteJMXBrokerFacade().getQueues().size();    }    private RemoteJMXBrokerFacade getRemoteJMXBrokerFacade() throws Exception {        RemoteJMXBrokerFacade createConnector = null;        try {            createConnector = new RemoteJMXBrokerFacade();            // 填写链接属性            System.setProperty("webconsole.jmx.url", "service:jmx:rmi:///jndi/rmi://" + ActiveMQConfig.get(ActiveMQConfig.IP) + ":" + ActiveMQConfig.get(ActiveMQConfig.PORT)                    + "/jmxrmi");            System.setProperty("webconsole.jmx.user", "admin");            System.setProperty("webconsole.jmx.password", "admin");            // 创建配置            SystemPropertiesConfiguration configuration = new SystemPropertiesConfiguration();            // 创建链接            createConnector.setConfiguration(configuration);        } catch (Exception e) {            e.printStackTrace();            throw new Exception("jmx链接失败");        }        return createConnector;    }}

访问结果如下



1 0