Tag Archives: queue

JMX remote method invoking to JMS queue

jmxConnect
Remote method invokation via JMX showed below:

package com.test;

import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class JmxInvokeExample {

    public static void main(String[] args) {
        try {
            // Define settings
            final String JMX_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://ServerIP:Port/jmxrmi";

            // Preparing
            JMXServiceURL serviceURL = new JMXServiceURL(JMX_SERVICE_URL);
            JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
            MBeanServerConnection connection = connector.getMBeanServerConnection();

            // Create object to invoke
            ObjectName objectName = new ObjectName("org.hornetq:module=JMS,type=Queue,name=\"Some.Queue.Name\"");

            // Compose invoking params
            String opName = "listMessages";
            String filter = "";
            // filter = "propKey1 = 'some value'";
            // filter += " AND ";
            // filter = "propKey2 = 'some next'";
            Object opParams[] = { filter };
            String opSig[] = { String.class.getName() };

            // Make invoking operation via JMX!
            Object object = connection.invoke(objectName, opName, opParams, opSig);

            // Check and out result
            if (object != null) {
                for (Map<String, Object> map : Arrays.asList((Map<String, Object>[]) object)) {
                    System.out.println("\nMESSAGE:\n--------");
                    for (Entry<String, Object> entry : map.entrySet()) {
                        System.out.println(entry.getKey() + " = " + entry.getValue());
                    }
                }
            }

            // Close connection
            connector.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

See also,
Standard MBeans and JMX
Basic Example of JMX Technology
Standard MBeans
Invoking MBean Operations

JMS queue message browsing

jms-programmingModel
To browse messages in jms queue use next snippet:

package com.test;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.hornetq.jms.client.HornetQObjectMessage;

public class JmsBrowserExample {

    public static void main(String[] args) {
        try {
            // Define settings
            final String PROVIDER_URL = "jnp://ServerIP:Port";
            final String CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
            final String CONNECTION_FACTORY_JNDI = "SOME/JNDI/NAME";
            final String QUEUE_JNDI = "Some/Queue/Name";

            // Preparing
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, PROVIDER_URL);
            InitialContext ctx = new InitialContext(env);
            Queue queue = (Queue) ctx.lookup(QUEUE_JNDI);
            QueueConnectionFactory qconnFactory = (QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY_JNDI);
            QueueConnection qconn = qconnFactory.createQueueConnection();
            QueueSession qession = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create queue browser
            String filter = "";
            // filter = "propKey = 'some value'";
            QueueBrowser qbrowser = qession.createBrowser(queue, filter);

            // Start connection
            qconn.start();

            // Browse messages
            int numMsgs = 0;

            @SuppressWarnings("unchecked")
            Enumeration<HornetQObjectMessage> enumeration = qbrowser.getEnumeration();
            while (enumeration.hasMoreElements()) {
                try {
                    HornetQObjectMessage message = enumeration.nextElement();
                    Object obj = message.getObject();
                    if (obj instanceof String) {
                        String payload = (String) obj;
                        System.out.println("\nEvent: " + payload);
                    }
                    numMsgs++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            System.out.println("\n\n" + queue + " has " + numMsgs + " messages");

            // Close connection
            qbrowser.close();
            qession.close();
            qconn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

See also,
Browser
Message Selection and Filtering
QueueBrowserExample

JMS queue message sending

jmsMessage
Here is an example of how to send messages in JMS queue:

package com.test;

import java.util.Hashtable;

import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JmsSendExample {

    public static void main(String[] args) {
        try {
            // Define settings
            final String PROVIDER_URL = "jnp://ServerIP:Port";
            final String CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";
            final String CONNECTION_FACTORY_JNDI = "SOME/JNDI/NAME";
            final String QUEUE_JNDI = "Some/Queue/Name";

            // Preparing
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
            env.put(Context.PROVIDER_URL, PROVIDER_URL);
            InitialContext ctx = new InitialContext(env);
            QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY_JNDI);
            QueueConnection qcon = qconFactory.createQueueConnection();
            QueueSession qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = (Queue) ctx.lookup(QUEUE_JNDI);
            QueueSender qsender = qsession.createSender(queue);
            qcon.start();
            System.out.println("Connection established.");

            // Compose message
            ObjectMessage message = qsession.createObjectMessage();
            String payloadObject = "Any serializable payload object here...";
            message.setObject(payloadObject);
            // Set additional properties
            message.setStringProperty("prop1_key", "prop1_val");
            message.setStringProperty("prop2_key", "prop2_val");
            System.out.println("Message composed.");

            // Make send
            Destination dest = (Destination) ctx.lookup(QUEUE_JNDI);
            MessageProducer producer = qsession.createProducer(dest);
            producer.send(message);
            System.out.println("Message sent.");

            // Close all
            producer.close();
            qsender.close();
            qsession.close();
            qcon.close();
            System.out.println("Connection closed.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Documentation about JMS:
JMS message structure
Message selectors in JMS
All about JMS messages