Java Tutorial - Java Scipt :
Standalone JMS Clients
In this section, we’ll demonstrate how to write a standalone JMS client just to test that JBossMQ is available and working properly. This is a fairly simple program. For the sake of organization, you will want to create a new directory to hold the JMS test programs. Without further ado, let’s see the code for a simple JMS publisher. Here’s the code for SimplePublisher.java:
import java.util.*;
import javax.jms.*;
import javax.naming.*;
/**
* Simple JMS Publisher.
*/
public class SimplePublisher
{
public static void main( String[] args )
{
TopicConnection connection = null;
TopicSession session = null;
try
{
// --- Create default properties to find JBoss’s JNDI. --- Hashtable default_properties = new Hashtable();
default_properties.put( Context.INITIAL_CONTEXT_FACTORY, “org.jnp.interfaces.NamingContextFactory” );
default_properties.put( Context.PROVIDER_URL, “localhost:1099” );
// --- Create and cache various JMS resources. ---
// -- Note that we are using the transaction aware versions of
// -- everything.
Context context = new InitialContext( default_properties );
TopicConnectionFactory factory = (TopicConnectionFactory)
context.lookup( “java:/XAConnectionFactory” );
connection = factory.createTopicConnection();
session =
connection.createTopicSession( true, Session.AUTO_ACKNOWLEDGE );
Topic topic = (Topic) context.lookup( “topic/testTopic” );
// -- We use topic/testTopic as the topic to publish to because it
// -- is defined by default by JBossMQ. You can find the queues
// -- and topics available at JBoss startup time within the
// -- jbossmq-destinations-service.xml file inside the
// -- %JBOSS_HOME%\server\default directory.
TopicPublisher publisher = session.createPublisher( topic );
// --- Publish a trivial text message. ---
TextMessage message = session.createTextMessage();
message.setText( “” + new Date().getTime() );
publisher.publish( message );
session.commit();
System.out.println( “Published “ + message );
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
try
{
if( session != null )
{
session.close();
}
}
catch( JMSException e2 )
{
e2.printStackTrace();
}
try
{
if( connection != null )
{
connection.close();
}
}
catch( JMSException e2 )
{
e2.printStackTrace();
}
}
}
}
Now, compile the program. To compile it in Windows, use the following command:
javac -classpath .;%JBOSS_HOME%\client\jbossall-client.jar SimplePublisher.java
To compile it on Linux, use the following command:
javac -classpath .:$JBOSS_HOME/client/jbossall-client.jar SimplePublisher.java
After the program is compiled, try running SimplePublisher. Before you do, make sure that the JBoss is running. JBoss is required because it provides the JNDI and JBossMQ JMS services that SimplePubisher relies on. In Windows, SimplePublisher can be run with following command:
java -classpath .;%JBOSS_HOME%\client\jbossallclient.
jar;%JBOSS_HOME%\client\log4j.jar SimplePublisher
In Linux, the command is:
java -classpath .:$JBOSS_HOME/client/jbossall
client. jar:$JBOSS_HOME/client/log4j.jar SimplePublisher
Note that we have added the log4j.jar to the classpath because this is used by JBossMQ clients for logging.
The default_properties hashtable is probably something you’ve seen before as a mechanism for passing in various properties that are used to establish context. We provide the settings for JNDI that are used by default in JBoss. We’ve done this here for simplicity’s sake. Amore flexible method is to pass in these settings through the command line rather than keeping them in the code. For example:
java -classpath .;%JBOSS_HOME%\client\jbossallclient.
jar;%JBOSS_HOME%\client\log4j.jar -
Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
-Djava.naming.provider.url=localhost:1099 SimplePublisher.
For the settings to be used, keep in mind that you have to remove the code that passes in the default_properties to the InitialContext constructor.
We also have JNDI lookups for the TopicConnectionFactory and the Topic. The JNDI names that these are bound under vary with different application servers, and even settings.
The SnoopJNDI servlet from Chapter 8 is a great way to browse the JNDI space of an application server.
By now, the program should execute without any errors but how can we tell if the message is really being sent? In order to do that, we need to complete the other side of the messaging chain. Namely, we need to write a receiver counterpart to our sender program. Here’s the code for SimpleSubscriber .java:
import java.util.*;
import javax.jms.*;
import javax.naming.*;
/**
* Simple JMS Subscriber.
*
*@author stng
*@created January 19, 2003
*/
public class SimpleSubscriber
{
/**
* The main program for the SimpleSubscriber class
*
*@param args The command-line arguments
*@since
*/
public static void main( String[] args )
{
TopicConnection connection = null;
TopicSession session = null;
try
{
// --- Create default properties to find JBoss’s JNDI. --- Hashtable default_properties = new Hashtable();
default_properties.put( Context.INITIAL_CONTEXT_FACTORY, “org.jnp.interfaces.NamingContextFactory” )
default_properties.put( Context.PROVIDER_URL, “localhost:1099” );
// --- Create and cache various JMS resources. ---
// -- Note that we are using the transaction aware versions of
// -- everything.
Context context = new InitialContext( default_properties ); TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup( “java:/XAConnectionFactory” ); connection = factory.createTopicConnection(); session = connection.createTopicSession( true, Session.AUTO_ACKNOWLEDGE );
Topic topic = (Topic) context.lookup( “topic/testTopic” );
// -- We use topic/testTopic as the topic to publish to because it
// -- is defined by default by JBossMQ. You can find the queues
// -- and topics available in the jbossmq-destinations-service.xml
// -- file within the %JBOSS_HOME%\server\default directory.
TopicSubscriber subscriber = session.createSubscriber( topic );
subscriber.setMessageListener(
new MessageListener()
{