Activemq Artemis jmx使用

1.修改    目录/mybroker/etc/broker.xml

修改:
<name>myartemis</name>(后面会用)

加入2行
<jmx-management-enabled>true</jmx-management-enabled>
<jmx-domain>my.org.apache.activemq</jmx-domain>(后面会用)



2.生成mybroker文件夹里面  目录/mybroker/etc/artemis.profile

在artemis.profile文件的jvm参数配置 加  -Djava.rmi.server.hostname=localhost

JAVA_ARGS="-XX:+PrintClassHistogram -XX:+UseG1GC -XX:+UseStringDeduplication -Xms512M -Xmx2G -Dhawtio.disableProxy=true -Dhawtio.realm=activemq -Dhawtio.offline=true -Dhawtio.rolePrincipalClasses=org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal  -Djava.rmi.server.hostname=localhost -Djolokia.policyLocation=${ARTEMIS_INSTANCE_ETC_URI}jolokia-access.xml "


3.目录/mybroker/etc/management.xml加入  connector-host="localhost"

<management-context xmlns="http://activemq.org/schema">
   <connector connector-port="9999" connector-host="localhost" />	
   <authorisation>
      <allowlist>
         <entry domain="hawtio"/>
      </allowlist>
      <default-access>
         <access method="list*" roles="amq"/>
         <access method="get*" roles="amq"/>
         <access method="is*" roles="amq"/>
         <access method="set*" roles="amq"/>
         <access method="*" roles="amq"/>
      </default-access>
      <role-access>
         <match domain="org.apache.activemq.artemis">
            <access method="list*" roles="amq"/>
            <access method="get*" roles="amq"/>
            <access method="is*" roles="amq"/>
            <access method="set*" roles="amq"/>
            <!-- Note count and browse are need to access the browse tab in the console-->
            <access method="browse*" roles="amq"/>
            <access method="count*" roles="amq"/>
            <access method="*" roles="amq"/>
         </match>
         <!--example of how to configure a specific object-->
         <!--<match domain="org.apache.activemq.artemis" key="subcomponent=queues">
            <access method="list*" roles="view,update,amq"/>
            <access method="get*" roles="view,update,amq"/>
            <access method="is*" roles="view,update,amq"/>
            <access method="set*" roles="update,amq"/>
            <access method="*" roles="amq"/>
         </match>-->
      </role-access>
   </authorisation>
</management-context>




代码编写

1.resources目录加入一个文件名称为jndi.properties

java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
connectionFactory.ConnectionFactory=tcp://localhost:61616
queue.queue/exampleQueue=exampleQueue

2.

{
        QueueConnection connection = null;
        InitialContext initialContext = null;


        try {
            initialContext = new InitialContext();
            QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
            connection = cf.createQueueConnection();
            String topic = "";
            //my.org.apache.activemq : broker.xml 配置的
            //myartemis : broker.xml 配置的
            ObjectName addressObjectName = ObjectNameBuilder.create("my.org.apache.activemq", "myartemis").getAddressObjectName(
                    SimpleString.toSimpleString(topic)
            );
            HashMap env = new HashMap();
            String[] creds = {"guest", "guest"};
            env.put(JMXConnector.CREDENTIALS, creds);

            JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi"), env);
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            AddressControl addressControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, addressObjectName, AddressControl.class, false);
            String[] queueNames = addressControl.getQueueNames();
            for (String queueName : queueNames) {
                ObjectName on = ObjectNameBuilder.create("my.org.apache.activemq", "myartemis").getQueueObjectName(
                        SimpleString.toSimpleString(topic),
                        SimpleString.toSimpleString(queueName),
                        RoutingType.MULTICAST);

                QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // Be sure to close the resources!
            if (initialContext != null) {
                try {
                    initialContext.close();
                } catch (NamingException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }

    }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值