Activemq Artemis jmx使用

焦苏燕
2023-12-01

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();
                }
            }
        }

    }





 类似资料: