Friday 5 April 2013

OSB - OpenJMS Integration

I have been searching for tutorials about sending message to JMS from OSB business service. Most of the examples are integration between OSB and Weblogic JMS provider. So I wanted to figure out how to configure a different JMS provider in my example OpenJMS.

Requirements:-
Our Business service will be a simple message producer. And we will use a sample Java class as a consumer.

Start the OpenJMS provider by running the <OpenJMS_Home>/bin/startup.bat. 

By default the OpenJMS has 2 JNDIs configured for the Connection Factory and queue. We need to create links to these objects from the Weblogic server.

Configuration on weblogic server:- 

  • Before starting the weblogic server we need to add some OpenJMS Jars to the Weblogic domain.
  • Edit the <weblogic_domain_home>/bin/setDomainEnv.bat file and at the end add
  • set CLASSPATH=%CLASSPATH%;<OpenJMS_HOME>\lib\castor-0.9.5.jar;<OpenJMS_HOME>\lib\concurrent-1.3.4.jar;<OpenJMS_HOME>\lib\openjms-0.7.7-beta-1.jar;<OpenJMS_HOME>\lib\openjms-common-0.7.7-beta-1.jar;<OpenJMS_HOME>\lib\openjms-net-0.7.7-beta-1.jar;<OpenJMS_HOME>\lib\openjms-tools-0.7.7-beta-1.jar;<OpenJMS_HOME>\lib\spice-jndikit-1.2.jar;
  • Note <OpenJMS_HOME> is the location where you have installed your OpenJMS.
Configuring the OpenJMS JNDI on Weblogic
  • Start your Weblogic server
  • Login into Admin console
  • Goto Services-> Foreign JNDI Providers 
  • Click New, give a valid name and click Next
  • Check the target server(s) and click Finish
  • Click on the newly created Foreign JNDI provider, Click General tab and enter the properties as below
    • Initial Context Factory:  org.exolab.jms.jndi.InitialContextFactory
    • Provider URL:  rmi://localhost:1099/
  • Ignore the other 3 properties and click Save


Now we have configured the OpenJMS JNDI server, Next we need to create links to the JNDI names available on the Open JMS. In our case we have 2 JNDI names available in OpenJMS.
  1. For Queue connection  Factory : "JmsQueueConnectionFactory"
  2. For the queue "queue1"
Creating Links to the JNDI Names
  1. Again goto the newly created connection factory and Click the Links tab
  2. Click New and enter the below details
    • Name: Link1
    • Local JNDI Name: JmsQueueConnectionFactory
    • Remote JNDI Name: JmsQueueConnectionFactory
    1. Click OK
    2. Again Click on New and Enter below details
      • Name: Link2
      • Local JNDI Name: queue1
      • Remote JNDI Name: queue1
    The output should look like below
    That's it the weblogic server configuration is over. Now we can work on our OSB business service.

    Configuring Business Service:-
    1. Create the OBS Configuration and Project and create a business Service.
    2. In the General configuration select "Messaging Service"
    3. Int the Messaging tab select Text for "Request Message Type"
    4. Goto the Transport tab 
      1. Select JMS as protocol.
      2. Enter the Endpoint URI as "jms://localhost:7001/JmsQueueConnectionFactory/queue1" click Add.
    5. Goto "JMS Transport" tab 
      1. Check Queue for destination type
      2. Check Text for Message type
    6. Save the business service and Test it by Run As-> Run On Server from the "Project explorer" view.
    7. When you get the testing browser enter some text in the payload text box and click Execute.
    8. Now the message is sent to the JMS provider. Lets create a small JMS consumer example to consume the message sent

    Consumer Program:- 

    package com.mak.jms;

    import java.util.Hashtable;

    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.Queue;
    import javax.jms.QueueConnection;
    import javax.jms.QueueConnectionFactory;
    import javax.jms.QueueReceiver;
    import javax.jms.QueueSession;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;

    public class JMSReceiver {
    public static void main(String[] args)
         {
            try
            {
            Hashtable properties = new Hashtable();
        properties.put(Context.INITIAL_CONTEXT_FACTORY,
        "org.exolab.jms.jndi.InitialContextFactory");
        properties.put(Context.PROVIDER_URL, "rmi://localhost:1099/");
        Context context = new InitialContext(properties);
             
               // retrieve queue connection factory
               QueueConnectionFactory
                  queueConnectionFactory =
                  (QueueConnectionFactory)context.lookup(
                  "JmsQueueConnectionFactory");
             
               // create a queue connection
               QueueConnection queueConnection =
             queueConnectionFactory.createQueueConnection();
             
               // start delivery of incoming messages
               queueConnection.start();
             
               // create a queue session
               // set transactions to false and set auto
               // acknowledgement of receipt of messages
               QueueSession queueSession =
                  queueConnection.createQueueSession(
                  false,Session.AUTO_ACKNOWLEDGE);
             
               // retrieve queue
               Queue queue =
                  (Queue)context.lookup("queue1");
             
               // create a queue receiver and associate
               // to the retrieved queue
               QueueReceiver queueReceiver =
                  queueSession.createReceiver(queue);
             
               // receive message using the synchronous
               // receive method
               Message message = queueReceiver.receive();
               String messageText = null;
               if (message instanceof TextMessage)
                  messageText =
                     ((TextMessage)message).getText();
               System.out.println(messageText);
             
            }
            catch (NamingException e)
            {
               e.printStackTrace();
            }
            catch (JMSException e)
            {
               e.printStackTrace();
            }
         }
    }

    Add the following below into classpath and run the above code snippet which should display the text sent from the OSB BS.
    <OpenJMS_Home>/lib/castor-0.9.5.jar
    <OpenJMS_Home>/lib/concurrent-1.3.4.jar
    <OpenJMS_Home>/lib/openjms-0.7.7-beta-1.jar
    <OpenJMS_Home>/lib/openjms-common-0.7.7-beta-1.jar
    <OpenJMS_Home>/lib/openjms-net-0.7.7-beta-1.jar
    <OpenJMS_Home>/lib/openjms-tools-0.7.7-beta-1.jar
    <OpenJMS_Home>/lib/spice-jndikit-1.2.jar



    That's it. Happy coding. :)

    No comments:

    Post a Comment