Skip to content

WebSphere 6.1 JMS with Standalone Java and Spring Clients

May 23, 2011

There are many resources on configuring JMS based applications in IBM WebSphere but most of them do not cover stand-alone clients. Here is a very small sample J2SE standalone Java application which lookups webSphere JMS JNDI resources and interacts with them both directly classic Java and Spring 2.5 JMS API.

Steup Reources:

To setup JMS Queues and Connection Factories in IBM WebSphere 6.1 you
may visit here.

Download J2SE Client libs:

Also need to download IBM Client for JMS on J2SE with IBM WebSphere Application Server.

Main Application:

Now that you have JMS resources and unziped Client libraries, here is a sample code to:

package au.com.osaglobal;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

/**
* Note: Compile and run this with IBM JDK in ~/IBM/WebSphere6.1/java */
public class App {

private static final Logger log = Logger.getLogger(App.class);

public static void publish() {
final Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "iiop://localhost:2810");

final Context jndiContext;
try {
jndiContext = new InitialContext(env);
} catch (NamingException e) {
log.error("Could not create JNDI API context: " + e.toString(), e); System.exit(1);
return;
}

try {
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/queueConnectionFactory");
Connection qConn = connectionFactory.createConnection(); Session qSession = qConn.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) jndiContext.lookup("jms/inQueue");

MessageProducer producer = qSession.createProducer(q); TextMessage message = qSession.createTextMessage(); message.setText("test message");
producer.send(message);

producer.close();
qSession.close();
qConn.close();
} catch (Exception e) {
log.error("Could not perfrom JMS operation: " + e.toString(), e); System.exit(2);
}
}

private static void spring_receiver() {
new ClassPathXmlApplicationContext(new
String[]{"applicationContext.xml"});
}

public static void main(String[] args) {
publish();
//spring_receiver();
}
}

Spring Application Context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
                <prop key="java.naming.provider.url">iiop://localhost:2810</prop>
            </props>
        </property>
    </bean>

    <bean id="queueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate" ref="jndiTemplate"/>
        <property name="jndiName" value="jms/queueConnectionFactory"/>
    </bean>

    <bean id="inQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate" ref="jndiTemplate"/>
        <property name="jndiName" value="jms/inQueue"/>
    </bean>

    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="destination" ref="inQueue"/>
        <property name="connectionFactory" ref="queueConnectionFactory"/>
        <property name="messageListener" ref="inMessageListener"/>
    </bean>

    <bean id="inMessageListener" class="au.com.osaglobal.JmsReceiver"/>
</beans>

Jms Receiver Object

package au.com.osaglobal;

import org.apache.log4j.Logger;

import javax.jms.Message;
import javax.jms.MessageListener;

public class JmsReceiver implements MessageListener {

private static final Logger log = Logger.getLogger(JmsReceiver.class);

public void onMessage(Message message) {
log.info("Received message: " + message);
}
}

Log4J Properties

log4j.rootCategory=DEBUG, S

log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

Maven Project pom.xml And Dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>au.com.osaglobal</groupId>
    <artifactId>sample-spring-was-jms</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>sample-spring-was-jms</name>
    <url>http://www.osaglobal.com.au/</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.spring>2.5.6</version.spring>
        <version.was>6.1</version.was>
    </properties>

    <dependencies>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>

        <!-- javax.jms api -->
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jms_1.1_spec</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- Springs 2.5.6 (core, beans, jms) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${version.spring}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${version.spring}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${version.spring}</version>
        </dependency>

        <!-- WebSphere 6.1 (rt, mq, mqjms, thinclient) -->
        <dependency>
            <groupId>com.ibm.websphere</groupId>
            <artifactId>runtime</artifactId>
            <version>${version.was</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/WebSphere6.1/AppServer1/deploytool/ itp/plugins/com.ibm.websphere.v61_6.1.0/ws_runtime.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>mq</artifactId>
            <version>${version.was}</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/WebSphere6.1/AppServer1/lib/WMQ/java/ lib/com.ibm.mq.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>mqjms</artifactId>
            <version>${version.was}</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/WebSphere6.1/AppServer1/lib/WMQ/java/ lib/com.ibm.mqjms.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.mq</groupId>
            <artifactId>dhbcore</artifactId>
            <version>${version.was}</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/WebSphere6.1/AppServer1/lib/WMQ/java/ lib/dhbcore.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.ws.webservices</groupId>
            <artifactId>thinclient</artifactId>
            <version>${version.was}</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/WebSphere6.1/AppServer1/runtimes/ com.ibm.ws.webservices.thinclient_6.1.0.jar</systemPath>
        </dependency>

        <!-- IBM Client for JMS on J2SE with IBM WebSphere Application Server -->
        <!-- see: http://www-01.ibm.com/support/docview.wss?uid=swg24012804 -->
        <dependency>
            <groupId>com.ibm.sibc</groupId>
            <artifactId>sibc.jms</artifactId>
            <version>6.1</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/sibc/sibc.jms.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.sibc</groupId>
            <artifactId>sibc.jndi</artifactId>
            <version>6.1</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/sibc/sibc.jndi.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.ibm.sibc</groupId>
            <artifactId>sibc.orb</artifactId>
            <version>6.1</version>
            <scope>system</scope>
            <systemPath>/home/amin/IBM/sibc/sibc.orb.jar</systemPath>
        </dependency>

    </dependencies>
</project>

Note: You will need to fix systemPath to fit your installation. Now can submit and receive simply by (un)commenting related methods in main().

Advertisements

From → java

9 Comments
  1. Ara permalink

    Baz gire in websphere oftadi ke!

  2. Szabi permalink

    Hi!

    I did everything you have told, but I got the following exception:

    Failed to initialize the ORB [Root exception is org.omg.CORBA.INITIALIZE: Unable to init plugins vmcid: IBM minor code: 504 completed: No]

    I’m using Websphere 7.0.

    Thanks a lot!

    Szabi

  3. venkat permalink

    This is really useful information to those who are newbee to was 6.1

    Thanks
    Sitaram

  4. Rama permalink

    I have a question here..how can I configure a jms queue on one websphere 6.1 server and and a MessageDrivenBean listening to that queue on other websphere 6.1
    server. If not MessageDrivenBean any Messagelistener is fine.

    • abbaspour permalink

      This is quite easy Rama. You may use above code in the second websphere instance and just change the jndi address to point to the first one.

  5. npatel permalink

    Greate Post. Just make sure to update SSL Secuirty for IIOP to SSL Supported when testing locally on Websphere 8.5 server (http://mresetar.blogspot.com/2012_06_01_archive.html)

Trackbacks & Pingbacks

  1. WebSphere 6.1 JMS with Standalone Java and Spring Clients « Amin … | Portal Technology

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: