In this article we would show how to create a Topic in AS 6 which uses JBoss Messaging, in this example we would use MySQL as the persistence as we all know that Hypersonic SQL database is not recommended in production environment. Here we would be referring profile which can be “all” or “production”.

Before we start creating a Topic would suggest you to follow the article How to switch Hypersonic with an alternative database in JBoss ? which would guide you to switch the non-supported database and once that’s done you can follow the below procedure.

Steps for creating a Topic in JBoss

1) Replace “hsqldb-persistence-service.xml” with “mysql-persistence-service.xml” file which can be picked up from “/jboss-as/docs/examples/jms” directory and have to put in “/profile/deploy/messaging” folder

2) Now you can create a Topic from the Admin-console or even create “*-service.xml” file and place it in the “/profile/deploy” directory. Where * means your Topic name as shown in the below example

TestTopic-service.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<server>
    <mbean xmbean-dd="xmdesc/Topic-xmbean.xml" name="jboss.messaging.destination:service=Topic,name=TestTopic" code="org.jboss.jms.server.destination.TopicService">
        <attribute name="JNDIName">TestTopic</attribute>
        <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
        <depends>jboss.messaging:service=PostOffice</depends>
	<attribute name="Clustered">true</attribute>
    </mbean>
</server>

3) That’s it !!! You are done configuring a simple Topic, now you can test it using the sender and receiver java code.

Testing

  1. Create a Directory somewhere in your file system like: “/urs/JBoss/JBM/Topic” to write the TopicSend.java and TopicReceive.java programs.
  2. In TopicSend.java copy the below program
    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="org.jnp.interfaces.NamingContextFactory";
    
    //*************** Connection Factory JNDI name *************************
    public final static String CONN_FACTORY="/ConnectionFactory";
    
    //*************** Topic JNDI name *************************
    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(CONN_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 TopicSend URL");
    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("org.jnp.interfaces.NamingContextFactory", "true");
    return new InitialContext(env);
    }
    }
    
  3. In TopicReceive.java copy the below program
    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="org.jnp.interfaces.NamingContextFactory";
    
    //*************** Connection Factory JNDI name *************************
    public final static String CONN_FACTORY="/ConnectionFactory";
    
    //*************** Topic JNDI name *************************
    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(CONN_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 URL");
    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("org.jnp.interfaces.NamingContextFactory", "true");
    return new InitialContext(env);
    }
    }
    
  4. Now open 2– command prompt and run the “. ./setJBossEnv.sh” by adding two DOTs separated by a single space, in both the prompts to set the Environment (PATH & CLASSPATH).
  5. setJBossEnv.sh

    #!/bin/sh
    
    echo "Exporting JBOSS_HOME...."
    export JBOSS_HOME=/Jboss/AS6-5.1/jboss-as   #<---- Change as per your environment
    echo "JBOSS_HOME Exported....!!!"
    echo ""
    
    echo "Exporting JAVA_HOME...."
    export JAVA_HOME=/Jdk/jdk1.6.0_21    #<---- Change as per your environment
    echo "JBOSS_HOME Exported....!!!"
    echo ""
    
    echo "Exporting CLASSPATH...."
    export CLASSPATH=$JBOSS_HOME/client/jbossall-client.jar:$JBOSS_HOME/client/jboss-javaee.jar:$JBOSS_HOME/common/lib/jnpserver.jar:$JBOSS_HOME/client/jboss-logging-spi.jar:$JBOSS_HOME/lib/jboss-javaee.jar:$JBOSS_HOME/common/lib/servlet-api.jar:$JBOSS_HOME/common/lib/log4j.jar:$CLASSPATH:.:
    echo "CLASSPATH Exported....!!!"
    echo ""
    
    echo "Exporting PATH...."
    export PATH=$JAVA_HOME/bin:$PATH
    echo "PATH Exported....!!!"
    echo ""
    
    echo "========================================================================"
    echo "JBOSS_HOME = " $JBOSS_HOME
    echo ""
    echo "JAVA_HOME = " $JAVA_HOME
    echo ""
    echo "CLASSPATH = " $CLASSPATH
    echo ""
    echo "PATH = " $PATH
    echo "========================================================================"
    
  6. Then compile the TopicSend.java and TopicReceive.java programs.
  7. Now run the code with the following command
  8. Prompt-1

    java TopicSend jnp://localhost:1099
     

    Prompt-2

    java TopicReceive jnp://localhost:1099
     

    Note:
    With the “java TopicSend” and “java TopicReceive” command you have to give the<Protocol>://<IP-address>:<Port number>of the managed servers in different prompts.

  9. Once this is been done try to send some messages in the Prompt-1 which can be revived on Prompt-2.

NOTE:

– Here we are be creating a Topic with MySQL as the database, however you can use any supported database with this article as the steps would be similar.

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