WebLogic JMS feature using Topic

The following article shows a simple usage of WebLogic JMS feature using a Topic.

1. Login into the WebLogic Admin Console, navigate to Services à Messaging à JMS Servers.

JMS Server acts as a management containers for the queues and topics.

Create a JMS Server as below.

2.Target the JMS Server to any one of the WebLogic Servers.

3. Create a JMS System Module to hold the queues and topics.

Navigate to Services –> Messaging –> JMS Modules.

4. Target the JMS System Module to the server on which the JMS Server is targeted.

5. Before adding JMS Topics, create a Sub Deployment.

6. Target the Sub Deployment to the created JMS Server.

7. Under the configuration tab, click New to add resources like Connection Factories, Queues,Topics.

8. Create a JMS Connection Factory, specify a JNDI name.

9. Target the Connection Factory to the Sub Deployment.

10. Similarly create a Topic, then target to the Sub Deployment.

11. Navigate to the JMS Resources page and you would see the Connection Factory and the JMS Topic created.

12. Open  command prompt, and set the class path.

13. Execute the below, TopicSend.java program to send a message to the Topic.

*************************************************************

 

import java.io.*;

import java.util.*;

import javax.transaction.*;

import javax.naming.*;

import javax.jms.*;

import javax.rmi.PortableRemoteObject;

public class TopicSend

{

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

public final static String JMS_FACTORY="CF1";

public final static String TOPIC="Topic1";

protected TopicConnectionFactory tconFactory;

protected TopicConnection tcon;

protected TopicSession tsession;

protected TopicPublisher tpublisher;

protected Topic topic;

protected TextMessage msg;

public void init(Context ctx, String topicName) throws NamingException, JMSException

{

tconFactory = (TopicConnectionFactory)

PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),TopicConnectionFactory.class);

tcon = tconFactory.createTopicConnection();

tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);

tpublisher = tsession.createPublisher(topic);

msg = tsession.createTextMessage();

tcon.start();

}

public void send(String message) throws JMSException {

msg.setText(message);

tpublisher.publish(msg);

}

public void close() throws JMSException {

tpublisher.close();

tsession.close();

tcon.close();

}

public static void main(String[] args) throws Exception {

if (args.length != 1) {

System.out.println("Usage: java examples.jms.topic.TopicSend WebLogicURL");

return;

}

InitialContext ic = getInitialContext(args[0]);

TopicSend ts = new TopicSend();

ts.init(ic, TOPIC);

readAndSend(ts);

ts.close();

}

protected static void readAndSend(TopicSend ts)

throws IOException, JMSException

{

BufferedReader msgStream = new BufferedReader (new InputStreamReader(System.in));

String line=null;

do {

System.out.print("Enter message ("quit" to quit): n");

line = msgStream.readLine();

if (line != null && line.trim().length() != 0) {

ts.send(line);

System.out.println("JMS Message Sent: "+line+"n");

}

} while (line != null && ! line.equalsIgnoreCase("quit"));

}

protected static InitialContext getInitialContext(String url)

throws NamingException

{

Hashtable<String,String> env = new Hashtable<String,String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

env.put(Context.PROVIDER_URL, url);

env.put("weblogic.jndi.createIntermediateContexts", "true");

return new InitialContext(env);

}}

 

*************************************************************

14.Execute the below TopicReceive.java program to retrieve the message from the Topic.

******************************************************

 

import java.io.*;

import java.util.*;

import javax.transaction.*;

import javax.naming.*;

import javax.jms.*;

import javax.rmi.PortableRemoteObject;

public class TopicReceive implements MessageListener {

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

public final static String JMS_FACTORY="CF1";

public final static String TOPIC="Topic1";

private TopicConnectionFactory tconFactory;

private TopicConnection tcon;

private TopicSession tsession;

private TopicSubscriber tsubscriber;

private Topic topic;

private boolean quit = false;

public void onMessage(Message msg) {

try {

String msgText;

if (msg instanceof TextMessage) {

msgText = ((TextMessage)msg).getText();

} else {

msgText = msg.toString();

}

System.out.println("JMS Message Received: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {

synchronized(this) {

quit = true;

this.notifyAll(); // Notify main thread to quit

}

}

} catch (JMSException jmse) {

System.err.println("An exception occurred: "+jmse.getMessage());

}

}

public void init(Context ctx, String topicName)

throws NamingException, JMSException

{

tconFactory = (TopicConnectionFactory)

PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),

TopicConnectionFactory.class);

tcon = tconFactory.createTopicConnection();

tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

topic = (Topic)

PortableRemoteObject.narrow(ctx.lookup(topicName),

Topic.class);

tsubscriber = tsession.createSubscriber(topic);

tsubscriber.setMessageListener(this);

tcon.start();

}

public void close() throws JMSException {

tsubscriber.close();

tsession.close();

tcon.close();

}

public static void main(String[] args) throws Exception {

if (args.length != 1) {

System.out.println("Usage: java examples.jms.topic.TopicReceive WebLogicURL");

return;

}

InitialContext ic = getInitialContext(args[0]);

TopicReceive tr = new TopicReceive();

tr.init(ic, TOPIC);

System.out.println("JMS Ready To Receive Messages (To quit, send a "quit" message).");

synchronized(tr) {

while (! tr.quit) {

try {

tr.wait();

} catch (InterruptedException ie) {}

}

}

tr.close();

}

private static InitialContext getInitialContext(String url)

throws NamingException

{

Hashtable<String,String> env = new Hashtable<String,String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

env.put(Context.PROVIDER_URL, url);

env.put("weblogic.jndi.createIntermediateContexts", "true");

return new InitialContext(env);

}}

 

******************************************************

Best Regards.

8 comments

  1. Hi, i tried to create a simple JMS call using this sample. I am not able to compile the TopicSend.java. Getting this error:
    C:\bea\user_projects\workspaces\default\TopicMDB\src\com\fm\mdb
    Any advice on how i can make it work.

  2. Sorry the error is:
    Exception in thread “main” java.lang.NoClassDefFoundError: TopicSend

    1. Hi Hari,

      Have you set the classpath using setDomainEnv.sh or setWLSEnv.sh? Execute the setDomainEnv.sh from the domain/bin directory and see if it works. Your java home should be set to compile the program.

      Best Regards,
      Divya

  3. Thanks,

    This is sample stand alone program.
    But I need to create JMS enabled web service.

    Can you please help me out.

    Thanks,
    Harshal

  4. Hi,
    If i run the above file using eclipse. i get the following error:
    Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.gs.fw.aig.jms.JmsContextFactory
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
    at javax.naming.InitialContext.init(InitialContext.java:223)
    at javax.naming.InitialContext.(InitialContext.java:197)
    at weblogic.deployment.jms.ForeignOpaqueReference.getReferent(ForeignOpaqueReference.java:182)
    at weblogic.jndi.internal.WLNamingManager.getObjectInstance(WLNamingManager.java:96)
    at weblogic.jndi.internal.ServerNamingNode.resolveObject(ServerNamingNode.java:377)
    at weblogic.jndi.internal.BasicNamingNode.resolveObject(BasicNamingNode.java:856)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:209)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:214)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:214)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:214)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:214)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:214)
    at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:667)
    at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:522)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:146)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:518)
    at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
    Can some one please help

  5. Hello,

    I copy this example to create a JMS client but i cant create a connection to a topic, after 2 minutos trying to connect to the topic appear this message:

    – Exception in thread “main” weblogic.jms.common.JMSException

    Can someone please help

    Thank you very much.

    Ana

Comments are closed.