Hi,
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: http://weblogic-wonders.com/weblogic/2010/06/26/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 http://weblogic-wonders.com/weblogic/2010/06/26/basic-jms-demo-using-weblogic-queue/
Step3). Now we need to create a JMS Module.
Step4). Now we need to create a ConnectionFactory.
Step5). Creating a WebLogic Topic.
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(“\n\t 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)
NOTE: If you want to see the Messages inside the Topic from AdminConsole then Please follow the Tips provided by “Wolkenfels” http://weblogic-wonders.com/weblogic/2010/07/20/basic-jms-demo-using-weblogic-topic/#comment-1198
.
.
Thanks
Jay SenSharma















July 22nd, 2010 on 12:10 pm
Thanks for the good walkthrough.
you may know that you can look into a queue with the admin console but there is also a way to see the topic-messages there. To do that you have to create a durable subscriber to that topic in the console and then you can see the messages there.
Wanted to add that you website is a real gem for weblogic users! Kudos!
July 22nd, 2010 on 12:14 pm
Hi Wolkenfels,
I really appreciate your Tips for viewing Topic Message. I didn’t know that. I will try it once. That will be a good Point here.
Thanks a Lot, Thanks for visting Wonders. We will be waiting to see some more tips from u.
.
.
Keep Posting
Thanks
jay SenSharma
July 28th, 2010 on 1:12 pm
Hi Jay,
Thanks for the document , well appreciated!
Till step(6) I’m done successfully , now small doubt with step(7) –
Writing “TopicSend.java” program means is it normal text file or ?
I working on windows platform
Can you help from here – thanks much.
–Arun
July 28th, 2010 on 1:46 pm
Hi Arun,
“TopicSend.java” is a Java Program … to run this program u need to do the following
Step1). Save the content of this post in your file “TopicSend.java”
Step2). open a command window and then run the “setWLSEnv.cmd” (This script we need to set the environment like CLASSPATH & PATH in our command prompt. This script can be found inside “C:\bea103\wlserver_10.3\server\bin” Location….Location may be different in your case according to your installation directory)
Step3). In the same command prompt compile the “TopicSend.java” like following:
javac TopicTest.java
Step4). Run the TopicSend program like described in the Image.
java TopicSend
.
.
Keep Posting
Thanks
Jay SenSharma
July 29th, 2010 on 11:36 am
Thanks Jay.
Now I’m struck at executing the programs;
Error after executing TopicSend.java:-
D:\WLS_HOME_103>java TopicSend t3://127.0.0.1:10001
Exception in thread “Main Thread” java.lang.NoClassDefFoundError: TopicSend
Info:
My Weblogic Admin Console URL
http://127.0.0.1:10001/console
Please let me know if you need any information more..
Thanks
July 29th, 2010 on 12:04 pm
Hi Arun,
Please follow the below Steps…
Step1). Open a fresh command prompt …then run the “setWLSEnv.cmd” (which is inside %WL_HOME%\server\bin)
Step2). in the same command prompt do the following: (Below step is very important to tell the command prompt that pick the classes from the CLASSPATH including the current directory…. see i added a DOT also in the classpath).
set CLASSPATH=%CLASSPATH%;.;
Step3). In the same prompt do the following
Move inside the D-Drive
cd WLS_HOME_103
Step4). compile and run the program like:
D:\WLS_HOME_103>javac TopicSend.java
D:\WLS_HOME_103>java TopicSend t3://127.0.0.1:10001
.
.
Keep Posting
Thanks
Jay SenSharma
July 29th, 2010 on 2:56 pm
Please see this how i executed your steps :
D:\WLS_HOME_103>cd wlserver_10.3\server\bin
D:\WLS_HOME_103\wlserver_10.3\server\bin>setWLSEnv.cmd
CLASSPATH=D:\WLS_HO~2\patch_wlw1030\profiles\default\sys_manifest_classpath\webl
ogic_patch.jar;D:\WLS_HO~2\patch_wls1030\profiles\default\sys_manifest_classpath
\weblogic_patch.jar;D:\WLS_HO~2\patch_cie660\profiles\default\sys_manifest_class
path\weblogic_patch.jar;D:\WLS_HO~2\JROCKI~1\lib\tools.jar;D:\WLS_HO~2\WLSERV~1.
3\server\lib\weblogic_sp.jar;D:\WLS_HO~2\WLSERV~1.3\server\lib\weblogic.jar;D:\W
LS_HO~2\modules\features\weblogic.server.modules_10.3.0.0.jar;D:\WLS_HO~2\WLSERV
~1.3\server\lib\webservices.jar;D:\WLS_HO~2\modules\ORGAPA~1.5/lib/ant-all.jar;D
:\WLS_HO~2\modules\NETSFA~1.0_1/lib/ant-contrib.jar;D:\WLS_HO~2\patch_wlw1030\pr
ofiles\default\sys_manifest_classpath\weblogic_patch.jar;D:\WLS_HO~2\patch_wls10
30\profiles\default\sys_manifest_classpath\weblogic_patch.jar;D:\WLS_HO~2\patch_
cie660\profiles\default\sys_manifest_classpath\weblogic_patch.jar;D:\WLS_HO~2\JR
OCKI~1\lib\tools.jar;D:\WLS_HO~2\WLSERV~1.3\server\lib\weblogic_sp.jar;D:\WLS_HO
~2\WLSERV~1.3\server\lib\weblogic.jar;D:\WLS_HO~2\modules\features\weblogic.serv
er.modules_10.3.0.0.jar;D:\WLS_HO~2\WLSERV~1.3\server\lib\webservices.jar;D:\WLS
_HO~2\modules\ORGAPA~1.5/lib/ant-all.jar;D:\WLS_HO~2\modules\NETSFA~1.0_1/lib/an
t-contrib.jar;;.;
PATH=D:\WLS_HO~2\patch_wlw1030\profiles\default\native;D:\WLS_HO~2\patch_wls1030
\profiles\default\native;D:\WLS_HO~2\patch_cie660\profiles\default\native;D:\WLS
_HO~2\WLSERV~1.3\server\native\win\32;D:\WLS_HO~2\WLSERV~1.3\server\bin;D:\WLS_H
O~2\modules\ORGAPA~1.5\bin;D:\WLS_HO~2\JROCKI~1\jre\bin;D:\WLS_HO~2\JROCKI~1\bin
;D:\WLS_HO~2\patch_wlw1030\profiles\default\native;D:\WLS_HO~2\patch_wls1030\pro
files\default\native;D:\WLS_HO~2\patch_cie660\profiles\default\native;D:\WLS_HO~
2\WLSERV~1.3\server\native\win\32;D:\WLS_HO~2\WLSERV~1.3\server\bin;D:\WLS_HO~2\
modules\ORGAPA~1.5\bin;D:\WLS_HO~2\JROCKI~1\jre\bin;D:\WLS_HO~2\JROCKI~1\bin;C:\
Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\TortoiseSV
N\bin;D:\WLS_HO~2\WLSERV~1.3\server\native\win\32\oci920_8;D:\WLS_HO~2\WLSERV~1.
3\server\native\win\32\oci920_8
Your environment has been set.
D:\WLS_HOME_103\wlserver_10.3\server\bin>set CLASSPATH=%CLASSPATH%;.;
D:\WLS_HOME_103\wlserver_10.3\server\bin>cd ../..
D:\WLS_HOME_103\wlserver_10.3>cd ..
D:\WLS_HOME_103>javac TopicSend.java
TopicSend.java:9: illegal character: \8221
public final static String JNDI_FACTORY=öweblogic.jndi.WLInitialContextFactoryö;
^
TopicSend.java:9: illegal character: \8221
public final static String JNDI_FACTORY=öweblogic.jndi.WLInitialContextFactoryö;
^
TopicSend.java:10: illegal character: \8221
public final static String JMS_FACTORY=öTCFö;
^
TopicSend.java:10: illegal character: \8221
public final static String JMS_FACTORY=öTCFö;
^
TopicSend.java:11: illegal character: \8221
public final static String TOPIC=öTestTopicö;
^
TopicSend.java:11: illegal character: \8221
public final static String TOPIC=öTestTopicö;
^
TopicSend.java:44: illegal character: \8220
System.out.println(ôUsage: java examples.jms.topic.TopicSend WebLogicURLö);
^
TopicSend.java:44: ‘;’ expected
System.out.println(ôUsage: java examples.jms.topic.TopicSend WebLogicURLö);
^
TopicSend.java:44: illegal start of expression
System.out.println(ôUsage: java examples.jms.topic.TopicSend WebLogicURLö);
^
TopicSend.java:44: ‘;’ expected
System.out.println(ôUsage: java examples.jms.topic.TopicSend WebLogicURLö);
^
TopicSend.java:44: illegal character: \8221
System.out.println(ôUsage: java examples.jms.topic.TopicSend WebLogicURLö);
^
TopicSend.java:58: illegal character: \8220
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \92
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \92
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: not a statement
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: ‘;’ expected
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \8230
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: not a statement
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: ‘;’ expected
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \92
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \8221
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \92
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: not a statement
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \8221
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: ‘;’ expected
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \92
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: illegal character: \8221
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:58: not a statement
System.out.print(ô\n\t TopicSender Started à Enter message (\öquit\ö to quit): \
nö);
^
TopicSend.java:60: illegal character: \8220
System.out.print(ôTopic Sender Says > ô);
^
TopicSend.java:60: ‘;’ expected
System.out.print(ôTopic Sender Says > ô);
^
TopicSend.java:60: ‘;’ expected
System.out.print(ôTopic Sender Says > ô);
^
TopicSend.java:60: illegal character: \8220
System.out.print(ôTopic Sender Says > ô);
^
TopicSend.java:65: illegal character: \8220
} while (line != null && ! line.equalsIgnoreCase(ôquitö));
^
TopicSend.java:65: ‘)’ expected
} while (line != null && ! line.equalsIgnoreCase(ôquitö));
^
TopicSend.java:65: illegal character: \8221
} while (line != null && ! line.equalsIgnoreCase(ôquitö));
^
TopicSend.java:65: illegal start of expression
} while (line != null && ! line.equalsIgnoreCase(ôquitö));
^
TopicSend.java:65: ‘;’ expected
} while (line != null && ! line.equalsIgnoreCase(ôquitö));
^
TopicSend.java:74: illegal character: \8220
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: ‘;’ expected
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: illegal start of expression
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: ‘;’ expected
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: illegal character: \8221
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: not a statement
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: illegal character: \8220
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
TopicSend.java:74: illegal character: \8221
env.put(ôweblogic.jndi.createIntermediateContextsö, ôtrueö);
^
45 errors
D:\WLS_HOME_103>
July 29th, 2010 on 3:57 pm
Hi Arun,
Whenever u copy paste some programs from weblogic – wonders …in a Noteopad or textpad then …always replace/alter some special characters (which are UTF characters) from the program while pasting it. Like replace all double quote marks with your Notepad/TextPad double quote marks…etc. which takes another 1-2 minutes to correct all the characters before running it.
.
.
Keep Posting
Thanks
Jay SenSharma
July 30th, 2010 on 12:27 pm
Thanks alot Jay , that works perfect for me!
I am able to send and receive messages successfully now
Publish-Subscribe messages works fine!
Do we have any option to test the same sending and receiving stuff from Admin-Console itself ?
Thanks Jay for helping me.
–Arun