Hi,

Jay SenSharma

Jay SenSharma

Java Messaging Service is an API that provides reliable, asynchronous communication between applications in a distributed computing environment. It enables a Java applications that share a messaging system to exchange messages and Simplifies application development by providing a standard interface for creating, sending, and receiving messages.

What are major components of JMS system? To Know this as well as to see a Simple Demo of WebLogic Queues Please refer to: basic-jms-demo-using-weblogic-queue/

Here we are going to see how we can configure WebLogic JMS Server, JMS Modules (Topic/ConnectionFactory) how to Target them and then How to test it using a Simpel Java Programs.

Step1). Start your WebLogic Server an Login to the AdminConsole.

Step2). To Create a JMS Server Please refer to the (S t e p – 2) of  basic-jms-demo-using-weblogic-queue/

Step3). Now we need to create a JMS Module.

JMS_Module_Creation

JMS_Module_Creation

Targeting_JMS_Module

Targeting_JMS_Module

Adding_JMS_Resources

Adding_JMS_Resources

Step4). Now we need to create a ConnectionFactory.

Adding_JMS_Resource

Adding_JMS_Resource

Creating Connection_Factory

Creating Connection_Factory

Naming_ConnectionFactory

Naming_ConnectionFactory

Targeting_ConnectionFactory

Targeting_ConnectionFactory

Step5). Creating a WebLogic Topic.

Creating_Topic

Creating_Topic

Choosing_Topic_Creation

Choosing_Topic_Creation

Naming_Topic

Naming_Topic

Creating_Subdeployment

Creating_Subdeployment

Naming_Subdeployment

Naming_Subdeployment

Targeting_Topic

Targeting_Topic

Final_Configuration

Final_Configuration

Step6). Create a Directory somewhere in your file system like: “C:WebLogic_Topic_Demo” to write the TopicSend and TopicReceive programs.

Step7). Write the “TopicSend.java” program as following inside “C:WebLogic_Topic_Demo”

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="TCF";
public final static String TOPIC="TestTopic";

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;
System.out.print("nt TopicSender Started ... Enter message ("quit" to quit): n");
do {
System.out.print("Topic Sender Says > ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
ts.send(line);
}
} 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);
}
}

Step8). Write the “TopicReceive.java” program as following inside “C:WebLogic_Topic_Demo” to receive messages from the WebLogic 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="TCF";
public final static String TOPIC="TestTopic";
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();
}
}
} 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);
}
}

Step9). Open 3 – Command Windows and run “setWLSEn.cmd” in all the three Command Windows to set the Environment (PATH & CLASSPATH).
Then compile the “TopicSend.java” and “TopicReceive.java” programs.

Step10). Start the “TopicReceive.java” program in two Command Windows like following :
java TopicReceive t3:/localhost:7001

Step11). Now start the “TopicSend.java” program to send some messages to the WebLogic Topic….As soon as u send message you will see that the Receivers started getting those messages…(One to Many Communication….one sender multiple receiver Which is called as Publish-Subscribe Messaging…. TOPIC)

Communication_Happening

Communication_Happening


NOTE: If you want to see the Messages inside the Topic from AdminConsole then Please follow the Tips provided by Wolkenfels”  http://middlewaremagic.com/weblogic/?p=2431#comment-1496

.

.

Thanks
Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.