Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I send messages to a JMS queue from outside the app server?

As I understand it, a J2EE container is required to include a JMS provider. Is it possible for a standalone Java application to send messages to a JMS queue provided by the container? If so, how do I access the JNDI lookups from outside the container?

(I am trying this with Geronimo if it makes any difference, but I am hoping there is a standard way of doing this.)

like image 314
Simon Nickerson Avatar asked Aug 25 '09 15:08

Simon Nickerson


People also ask

Does JMS need Application Server?

The JMS configuration is application server-specific. You must configure JMS queues within the environment and make them accessible through the Java Naming Directory Interface (JNDI).

How do I connect to a JMS server?

You can connect to any JMS server by using the Java Naming and Directory Interface (JNDI) to locate an existing JMS connection factory. Depending on where the connection factory is bound, the connection URL can begin with the string lookup or the string jndi.

Which of the following method is used to send a message in JMS?

To send a message, an application uses the send() method of a MessageProducer object, as shown in the following example: producer. send(outMessage); An application can use the send() method to send messages in either messaging domain.

What is the use of JMS in integration environment?

JMS is the standard messaging API for passing data between application components and allowing business integration in heterogeneous and legacy environments. JMS provides two programming models: Point-to-Point—Messages are sent to a single consumer using a JMS queue.


1 Answers

You should be able to create an InitialContext that uses the JNDI server in Geronimo. You can then use this to lookup your JMS Connection Factory and Queue.

The following example was adapted from http://forums.sun.com/thread.jspa?threadID=5283256 to use the Geronimo JNDI Factory.

Context                  jndiContext = null;
ConnectionFactory   connectionFactory = null;
Connection             connection = null;
Session                  session = null;
Queue                    queue = null;
MessageProducer     messageProducer = null;   

try
{
    //[1] Create a JNDI API InitialContext object.
    Hashtable properties = new Hashtable(2);

    // CHANGE these to match Geronimos JNDI service

    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    properties.put(Context.PROVIDER_URL, "ejbd://127.0.0.1:4201");
    jndiContext = new InitialContext(properties);

    //[2] Look up connection factory and queue.
    connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/ConnectionFactory");
    queue = (Queue)jndiContext.lookup("jms/Queue");

    //[3]
    // - Create connection
    // - Create session from connection; false means session is not transacted.
    // - Create sender and text message.
    // - Send messages, varying text slightly.
    connection = connectionFactory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    messageProducer = session.createProducer(queue);

   //send a message
   TextMessage message = session.createTextMessage(this.jTextSend.getText()); 
   messageProducer.send(message); 

   //example for send some object
   //ObjectMessage message = session.createObjectMessage();
   //MyObj myObj = new MyObj ("Name"); //this class must be serializable 
   //message.setObject(myObj );
   //messageProducer.send(message);
}
catch(Exception ex)
{
   LOG.error(ex);
}
finally
{
     if(connection !=null)
     {
         try
         {
             connection.close();
         }
         catch(JMSException e)
         {
              LOG.error(e);
         }
     }
}
like image 108
pjp Avatar answered Sep 28 '22 16:09

pjp