JMS - Java Message Service

JMS example application (chat... using publish-subscribe strategy) in the Weblogic way..

JMS publish-subscribe application the quick way... A chat application.... Need to add to config.xml :

What you need to add to config.xml in your Weblogic installation to get rolling..

<JMSTopic JNDIName="dk.topsecurity.topic" Name="dk.topsecurity.topic" StoreEnabled="default"/> ...

Furthermore you need a "" in your local directory to tell the applications which setup to use when contacting JMS service :

The, which ties everything together - eliminating the need to gardcode everything in the application - or set bunch of stuff through commandline parameters...


The chat application, which handles everything..

package dk.topsecurity;

import javax.jms.*;
import javax.naming.*;
import java.util.Properties;
public class JMSChat implements javax.jms.MessageListener {

    public final static String TOPIC_NAME = "dk.topsecurity.topic";
    public final static String JMS_FACTORY = "weblogic.jms.ConnectionFactory";
//    public final static String JMS_FACTORY = "javax.jms.QueueConnectionFactory";

    private TopicSession pubSession, subSession;
    private TopicPublisher publisher;
    private TopicConnection connection;
    private String username;
    /* Constructor. Establish JMS publisher and subscriber */
    public JMSChat(String topicName, String logonusername, String logonpassword)
      throws Exception {
//requires definition of in java.naming.factory.initial,java.naming.provider.url in
        InitialContext jndi = new InitialContext();
        TopicConnectionFactory conFactory =
          (TopicConnectionFactory)jndi.lookup(JMS_FACTORY); //look up a JMS connection factory
        connection =
          conFactory.createTopicConnection(); //conFactory.createTopicConnection(logonusername,logonpassword); //create a JMS connection
        pubSession =
          connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); // create 2x JMS session objects
        subSession =
        Topic chatTopic = (Topic)jndi.lookup(topicName); //look up a JMS topic
        publisher = 
            pubSession.createPublisher(chatTopic); //create a JMS publisher and subscriber
        TopicSubscriber subscriber = 
        subscriber.setMessageListener(this); //set a JMS message listener
        username = logonusername;

        connection.start(); //start JMS connection; allows messages to be delivered
    public void onMessage(Message message) { //receive message from topic subscriber
        try {
            System.out.println( ((TextMessage) message).getText() );
        } catch (JMSException jmse){ jmse.printStackTrace( ); }
    protected void writeMessage(String text) throws JMSException { //create and send message using topic publisher
        TextMessage message = pubSession.createTextMessage( );
        message.setText(username+" : "+text);
    public void close( ) throws JMSException { //close the JMS connection
        connection.close( );
    public static void main(String [] args) { //run chat client
        try {
            if (args.length!=3)
                System.out.println("Topic or username missing");
            // args[0]=topicName; args[1]=username; args[2]=password
            JMSChat chat = new JMSChat(args[0],args[1],args[2]);
           BufferedReader commandLine = new 
     InputStreamReader(; //read from command line
           for(;;){ //loop until the word "exit" is typed
                String s = commandLine.readLine( );
                if (s.equalsIgnoreCase("exit")){
                    chat.close( ); // close down connection
                    System.exit(0);// exit program
                } else 
        } catch (Exception e){ e.printStackTrace( ); }

Two ways to to receive messages: call the receive method, which waits for messages - or create a listener object, which receives messages when they becomes available...

This chat client uses a listener... Should be rather straightforward to implement a chat client using a receiver instead...

Using Windows platform (not by preferred choice).. we need command scripts to run out client.. Startting the chat client i 3 different instances.. This script takes an argument (the client name) - be sure to include it on startup - and to remember to set wl_home to your weblogic installation directory of choice..

set BEA_HOME=C:\weblogicplatform
set WL_HOME=C:\weblogicplatform\weblogic81

java -classpath .;TopsecurityJMS.jar;%WL_HOME%\server\lib\weblogic.jar dk/topsecurity/JMSChat dk.topsecurity.topic %1 password

Compiling... we get something like this...

And away we go with our 3 clients (named "client1", "client2", client3",..).