Search in sources :

Example 86 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project openj9 by eclipse.

the class TestManagementAgent method test_startLocalManagementAgent.

@Test
public void test_startLocalManagementAgent() {
    logger.debug("starting " + getName());
    TargetManager target = launchTarget();
    String targetPid = target.getTargetPid();
    logger.debug("launched " + targetPid);
    try {
        VirtualMachine vm = VirtualMachine.attach(targetPid);
        String response = vm.startLocalManagementAgent();
        logger.debug("response=" + response);
        logger.debug("Connecting");
        JMXServiceURL agentURL = new JMXServiceURL(response);
        verifyManagementAgent(vm, agentURL);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("unexpected exception " + e.getMessage() + " in " + targetPid);
    } finally {
        target.terminateTarget();
    }
    logger.debug("ending " + getName());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) IOException(java.io.IOException) VirtualMachine(com.sun.tools.attach.VirtualMachine) Test(org.testng.annotations.Test)

Example 87 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project activemq-artemis by apache.

the class ConnectorServerFactory method init.

public void init() throws Exception {
    if (this.server == null) {
        throw new IllegalArgumentException("server must be set");
    }
    JMXServiceURL url = new JMXServiceURL(this.serviceUrl);
    setupArtemisRMIServerSocketFactory();
    if (isClientAuth()) {
        this.secured = true;
    }
    if (this.secured) {
        this.setupSsl();
    }
    if (!AuthenticatorType.PASSWORD.equals(this.authenticatorType)) {
        this.environment.remove("jmx.remote.authenticator");
    }
    this.connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, this.environment, this.server);
    if (this.objectName != null) {
        this.server.registerMBean(this.connectorServer, this.objectName);
    }
    try {
        this.connectorServer.start();
    } catch (Exception ex) {
        doUnregister(this.objectName);
        throw ex;
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) IOException(java.io.IOException) JMException(javax.management.JMException)

Example 88 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project activemq-artemis by apache.

the class SecurityJMXTest method testBrowseExpiredMessages.

public void testBrowseExpiredMessages() throws Exception {
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1199/jmxrmi");
    JMXConnector connector = JMXConnectorFactory.connect(url, null);
    connector.connect();
    MBeanServerConnection connection = connector.getMBeanServerConnection();
    ObjectName name = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost," + "destinationType=Queue,destinationName=TEST.Q");
    QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(connection, name, QueueViewMBean.class, true);
    HashMap<String, String> headers = new HashMap<>();
    headers.put("timeToLive", Long.toString(2000));
    headers.put("JMSDeliveryMode", Integer.toString(DeliveryMode.PERSISTENT));
    queueMbean.sendTextMessage(headers, "test", "system", "manager");
    // allow message to expire on the queue
    TimeUnit.SECONDS.sleep(4);
    Connection c = new ActiveMQConnectionFactory("vm://localhost").createConnection("system", "manager");
    c.start();
    // browser consumer will force expiration check on addConsumer
    QueueBrowser browser = c.createSession(false, Session.AUTO_ACKNOWLEDGE).createBrowser(new ActiveMQQueue("TEST.Q"));
    assertTrue("no message in the q", !browser.getEnumeration().hasMoreElements());
    // verify dlq got the message, no security exception as brokers context is now used
    browser = c.createSession(false, Session.AUTO_ACKNOWLEDGE).createBrowser(new ActiveMQQueue("ActiveMQ.DLQ"));
    assertTrue("one message in the dlq", browser.getEnumeration().hasMoreElements());
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) QueueViewMBean(org.apache.activemq.broker.jmx.QueueViewMBean) Connection(javax.jms.Connection) MBeanServerConnection(javax.management.MBeanServerConnection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MBeanServerConnection(javax.management.MBeanServerConnection) QueueBrowser(javax.jms.QueueBrowser) ObjectName(javax.management.ObjectName)

Example 89 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project activemq-artemis by apache.

the class MessageCounterExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Connection, session and a producer for the queue
        connection = cf.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        // Step 5. Create and send a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        producer.send(message);
        System.out.println("Sent message: " + message.getText());
        // Step 6. Sleep a little bit so that the queue is sampled
        System.out.println("Sleep a little bit to have the queue sampled...");
        Thread.sleep(3000);
        // Step 7. Use JMX to retrieve the message counters using the JMSQueueControl
        ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
        // we dont actually need credentials as the guest login i sused but this is how its done
        HashMap env = new HashMap();
        String[] creds = { "guest", "guest" };
        env.put(JMXConnector.CREDENTIALS, creds);
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), env);
        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        // Step 8. List the message counters and convert them to MessageCounterInfo data structure.
        String counters = queueControl.listMessageCounter();
        MessageCounterInfo messageCounter = MessageCounterInfo.fromJSON(counters);
        // Step 9. Display the message counter
        displayMessageCounter(messageCounter);
        // Step 10. Sleep again to have the queue sampled again
        System.out.println("Sleep a little bit again...");
        Thread.sleep(3000);
        // Step 11. List the messages counters again
        counters = queueControl.listMessageCounter();
        messageCounter = MessageCounterInfo.fromJSON(counters);
        displayMessageCounter(messageCounter);
        // Step 12. Create a JMS consumer on the queue
        MessageConsumer consumer = session.createConsumer(queue);
        // Step 13. Start the connection to receive messages on the consumer
        connection.start();
        // Step 14. Receive a JMS message from the queue. It corresponds to the message sent at step #5
        TextMessage messageReceived = (TextMessage) consumer.receive(5000);
        System.out.format("Received message: %s%n%n", messageReceived.getText());
        // Step 15. Sleep on last time to have the queue sampled
        System.out.println("Sleep a little bit one last time...");
        Thread.sleep(3000);
        // Step 16. Display one last time the message counter
        counters = queueControl.listMessageCounter();
        messageCounter = MessageCounterInfo.fromJSON(counters);
        displayMessageCounter(messageCounter);
    } finally {
        // Step 17. Be sure to close our JMS resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MessageConsumer(javax.jms.MessageConsumer) HashMap(java.util.HashMap) QueueConnectionFactory(javax.jms.QueueConnectionFactory) MessageCounterInfo(org.apache.activemq.artemis.api.core.management.MessageCounterInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialContext(javax.naming.InitialContext) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ObjectName(javax.management.ObjectName) QueueConnection(javax.jms.QueueConnection) JMXConnector(javax.management.remote.JMXConnector) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 90 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project activemq-artemis by apache.

the class JMXExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 5. Create a JMS Session
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(queue);
        // Step 7. Create a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        System.out.println("Sent message: " + message.getText());
        // Step 8. Send the Message
        producer.send(message);
        // Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage
        ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
        // Step 10. Create JMX Connector to connect to the server's MBeanServer
        // we dont actually need credentials as the guest login i sused but this is how its done
        HashMap env = new HashMap();
        String[] creds = { "guest", "guest" };
        env.put(JMXConnector.CREDENTIALS, creds);
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMXExample.JMX_URL), env);
        // Step 11. Retrieve the MBeanServerConnection
        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        // Step 12. Create a QueueControl proxy to manage the queue on the server
        QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        // Step 13. Display the number of messages in the queue
        System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
        // Step 14. Remove the message sent at step #8
        System.out.println("message has been removed: " + queueControl.removeMessages(null));
        // Step 15. Display the number of messages in the queue
        System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
        // Step 16. We close the JMX connector
        connector.close();
        // Step 17. Create a JMS Message Consumer on the queue
        MessageConsumer messageConsumer = session.createConsumer(queue);
        // Step 18. Start the Connection
        connection.start();
        // Step 19. Trying to receive a message. Since the only message in the queue was removed by a management
        // operation, there is none to consume.
        // The call will timeout after 5000ms and messageReceived will be null
        TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
        if (messageReceived != null) {
            throw new IllegalStateException("message should be null!");
        }
        System.out.println("Received message: " + messageReceived);
    } finally {
        // Step 20. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MessageConsumer(javax.jms.MessageConsumer) HashMap(java.util.HashMap) QueueConnectionFactory(javax.jms.QueueConnectionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialContext(javax.naming.InitialContext) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ObjectName(javax.management.ObjectName) QueueConnection(javax.jms.QueueConnection) JMXConnector(javax.management.remote.JMXConnector) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) MBeanServerConnection(javax.management.MBeanServerConnection)

Aggregations

JMXServiceURL (javax.management.remote.JMXServiceURL)280 JMXConnector (javax.management.remote.JMXConnector)150 MBeanServerConnection (javax.management.MBeanServerConnection)107 ObjectName (javax.management.ObjectName)90 IOException (java.io.IOException)78 HashMap (java.util.HashMap)75 MBeanServer (javax.management.MBeanServer)71 JMXConnectorServer (javax.management.remote.JMXConnectorServer)64 MalformedURLException (java.net.MalformedURLException)45 RemoteException (java.rmi.RemoteException)22 Test (org.junit.Test)22 Map (java.util.Map)16 UnknownHostException (java.net.UnknownHostException)14 Properties (java.util.Properties)14 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 MalformedObjectNameException (javax.management.MalformedObjectNameException)13 LocateRegistry (java.rmi.registry.LocateRegistry)12 Registry (java.rmi.registry.Registry)12 ArrayList (java.util.ArrayList)11