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:-
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:-
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();
}
}
}
Requirements:-
- Latest version of OpenJMS (http://openjms.sourceforge.net/downloads.html.)
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.
- For Queue connection Factory : "JmsQueueConnectionFactory"
- For the queue "queue1"
Creating Links to the JNDI Names
- Again goto the newly created connection factory and Click the Links tab
- Click New and enter the below details
- Name: Link1
- Local JNDI Name: JmsQueueConnectionFactory
- Remote JNDI Name: JmsQueueConnectionFactory
- Click OK
- 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:-
- Create the OBS Configuration and Project and create a business Service.
- In the General configuration select "Messaging Service"
- Int the Messaging tab select Text for "Request Message Type"
- Goto the Transport tab
- Select JMS as protocol.
- Enter the Endpoint URI as "jms://localhost:7001/JmsQueueConnectionFactory/queue1" click Add.
- Goto "JMS Transport" tab
- Check Queue for destination type
- Check Text for Message type
- Save the business service and Test it by Run As-> Run On Server from the "Project explorer" view.
- When you get the testing browser enter some text in the payload text box and click Execute.
- Now the message is sent to the JMS provider. Lets create a small JMS consumer example to consume the message sent
Consumer Program:-
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. :)