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.

Latest Comments

  1. Hari March 9, 2011
  2. Hari March 9, 2011
    • divya March 9, 2011
  3. harshal April 25, 2011
  4. Shilpa March 19, 2013
  5. Ana April 21, 2015

Leave a Reply