- Chapter 1 Overview
- Setting Up Your Environment
- Starting and Testing a Message Broker
- To Start a Broker
- To Test a Broker
- Developing a Client Application
- To Produce Messages
- To Consume Messages
- Compiling and Running a Client Application
- To Compile and Run the HelloWorldMessage Application
- Deploying a Client Application
- Example Application Code
Chapter 1 Overview
This chapter provides an overall introduction to Sun Java TM System Message Queue and a quick-start tutorial. It describes the procedures needed to create, compile, and run a simple example application. Before reading this chapter, you should be familiar with the concepts presented in the Sun Java System Message Queue 4.2 Technical Overview
The chapter covers the following topics:
The minimum Java Development Kit (JDK) level required to compile and run Message Queue clients is 1.2. For the purpose of this tutorial it is sufficient to run the Message Queue message broker in a default configuration. For instructions on configuring a message broker, see Chapter 4, Configuring a Broker, in Sun Java System Message Queue 4.2 Administration Guide
Setting Up Your Environment
The Message Queue files that need to be used in conjunction with Message Queue Java clients can be found in the lib directory in the installed location for Message Queue on your platform. Message Queue Java clients need to be able to use several .jar files found in the lib directory when these clients are compiled and run.
You need to set the CLASSPATH environment variable when compiling and running a JMS client. (The IMQ_HOME variable, where used, refers to the directory where Message Queue is installed on Windows platforms and on some Sun Java System Application Server platforms.)
The value of CLASSPATH depends on the following factors:
- The platform on which you compile or run
- The JDK version you are using
- Whether you are compiling or running a JMS application
- Whether your application uses the Simple Object Access Protocol (SOAP)
- Whether your application uses the SOAP/JMS transformer utilities
The following table shows the directories where .jar files are to be found on the various platforms.
Solaris, using the standalone version of Sun Java System Application Server
The table below lists the .jar files you need to compile and run different kinds of code.
Directory containing compiled Java application or ’.’
See discussion of JNDI .jar files, following this table.
Directory containing compiled Java application or ’.’
Sun Java System Application Server already includes these .jar files for SOAP servlet support.
Code using SOAP/JMS transformer utilities
.jar files for JMS and SOAP clients
Also add the appropriate .jar files listed in this table for the kind of code you are writing.
A client application must be able to access the file jndi.jar even if the application does not use the Java Naming and Directory Interface (JNDI) directly to look up Message Queue administered objects. This is because JNDI is referenced by the Destination and ConnectionFactory classes.
JNDI .jar files are bundled with JDK 1.4. Thus, if you are using this JDK, you do not have to add jndi.jar to your CLASSPATH setting. However, if you are using an earlier version of the JDK, you must include jndi.jar in your CLASSPATH.
If you are using JNDI to look up Message Queue administered objects, you must also include the following files in your CLASSPATH setting:
- If you are using the file-system service provider for JNDI (with any JDK version), you must include the file fscontext.jar.
- If you are using the Lightweight Directory Access Protocol (LDAP) context
- with JDK 1.2 or 1.3, include the files ldabbp.jar, and fscontext.jar. ldap.jar,
- with JDK 1.4, all files are already bundled with this JDK.
Starting and Testing a Message Broker
This tutorial assumes that you do not have a Message Queue message broker currently running. (If you run the broker as a UNIX startup process or Windows service, then it is already running and you can skip to Developing a Client Application.)
To Start a Broker
To Test a Broker
One simple way to check the broker startup is by using the Message Queue command utility (imqcmd) to display information about the broker:
Example 1–1 Output From Testing a Broker
% imqcmd query bkr -u admin Querying the broker specified by: ------------------------- Host Primary Port ------------------------- localhost 7676 Version 3.6 Instance Name imqbroker Primary Port 7676 Current Number of Messages in System 0 Current Total Message Bytes in System 0 Max Number of Messages in System unlimited (-1) Max Total Message Bytes in System unlimited (-1) Max Message Size 70m Auto Create Queues true Auto Create Topics true Auto Created Queue Max Number of Active Consumers 1 Auto Created Queue Max Number of Backup Consumers 0 Cluster Broker List (active) Cluster Broker List (configured) Cluster Master Broker Cluster URL Log Level INFO Log Rollover Interval (seconds) 604800 Log Rollover Size (bytes) unlimited (-1) Successfully queried the broker. Current Number of Messages in System 0
Developing a Client Application
This section introduces the general procedures for interacting with the Message Queue API to produce and consume messages. The basic steps shown here are elaborated in greater detail in Chapter 2, Using the Java API The procedures for producing and consuming messages have a number of steps in common, which need not be duplicated if the same client is performing both functions.
To Produce Messages
ConnectionFactory myFctry = new com.sun.messaging.ConnectionFactory();
myFctry.setProperty(ConnectionConfiguration.imqAddressList, "localhost:7676, broker2:5000, broker3:9999"); myFctry.setProperty(ConnectionConfiguration.imqReconnectEnabled, "true");
Connection myConnection = myFactory.createConnection();
Session mySession = myConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination myDest = new com.sun.messaging.Queue("myDest");
MessageProducer myProducer = mySession.createProducer(myDest);
TextMessage outMsg = mySession.createTextMessage();
outMsg.setStringProperty("MagicWord", "Shazam");
To Consume Messages
ConnectionFactory myFctry = new com.sun.messaging.ConnectionFactory();
myFctry.setProperty(ConnectionConfiguration.imqAddressList, "localhost:7676, broker2:5000, broker3:9999"); myFctry.setProperty(ConnectionConfiguration.imqReconnectEnabled,"true");
Connection myConnection = myFactory.createConnection();
Session mySession = myConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination myDest = new com.sun.messaging.Queue("myDest");
MessageConsumer myConsumer = mySession.createConsumer(myDest);
Message inMsg = myConsumer.receive();
TextMessage txtMsg = (TextMessage) inMsg; String msgText = txtMsg.getText();
msgPriority = inMsg.getJMSPriority();
magicWord = inMsg.getStringProperty("MagicWord");
Compiling and Running a Client Application
This section leads you through the steps needed to compile and run a simple example client application, HelloWorldMessage, that sends a message to a destination and then retrieves the same message from the destination. The code shown in Example 1–2 is adapted and simplified from an example program provided with the Message Queue installation: error checking and status reporting have been removed for the sake of conceptual clarity. You can find the complete original program in the helloworld directory in the following locations.
Example 1–2 Simple Message Queue Client Application
// Import the JMS and JNDI API classes import javax.jms.*; import javax.naming.*; import java.util.Hashtable; public class HelloWorldMessage < /** * Main method * * Parameter args not used * */ public static void main (String[] args) < try < // Get a connection factory. // // Create the environment for constructing the initial JNDI // naming context. Hashtable env = new Hashtable(); // Store the environment attributes that tell JNDI which // initial context // factory to use and where to find the provider. // (On Unix, use provider URL "file:///imq_admin_objects" // instead of"file:///C:/imq_admin_objects".) env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); env.put(Context.PROVIDER_URL,"file:///C:/imq_admin_objects"); // Create the initial context. Context ctx = new InitialContext(env); // Look up connection factory object in the JNDI object store. String CF_LOOKUP_NAME = "MyConnectionFactory"; ConnectionFactory myFactory = (ConnectionFactory) ctx.lookup(CF_LOOKUP_NAME); // Create a connection. Connection myConnection = myFactory.createConnection(); // Create a session. Session mySession = myConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Look up the destination object in the JNDI object store. String DEST_LOOKUP_NAME = "MyDest"; Destination myDest = (Destination) ctx.lookup (DEST_LOOKUP_NAME); // Create a message producer. MessageProducer myProducer = mySession.createProducer(myDest); // Create a message consumer. MessageConsumer myConsumer = mySession.createConsumer(myDest); // Create a message. TextMessage outMsg = mySession.createTextMessage ("Hello, World!"); // Send the message to the destination. System.out.println("Sending message: " + outMsg.getText()); myProducer.send(outMsg); // Start the connection. myConnection.start(); // Receive a message from the destination. Message inMsg = myConsumer.receive(); // Retrieve the contents of the message. if (inMsg instanceof TextMessage) < TextMessage txtMsg = (TextMessage) inMsg; System.out.println("Received message: " + txtMsg.getText()); >// Close the session and the connection. mySession.close(); myConnection.close(); > catch (Exception jmse) < System.out.println("Exception occurred: " + jmse.toString() ); jmse.printStackTrace(); >> >
To compile and run Java clients in a Message Queue environment, it is recommended that you use the Java 2 SDK, Standard Edition, version 1.4 or later. You can download the recommended SDK from the following location:
Be sure to set your CLASSPATH environment variable correctly, as described in Setting Up Your Environment, before attempting to compile or run a client application.
If you are using JDK 1.5, you will get compiler errors if you use the unqualified JMS Queue class along with the following import statement.
This is because the packagesjava.util and javax.jms both contain a class named Queue. To avoid the compilation errors, you must eliminate the ambiguity by either fully qualifying references to the JMS Queue class as javax.jms.Queue or correcting your import statements to refer to specific individual java.util classes.
The following steps for compiling and running the HelloWorldMessage application are furnished strictly as an example. The program is shipped precompiled; you do not actually need to compile it yourself (unless, of course, you modify its source code).
To Compile and Run the HelloWorldMessage Application
javac HelloWorldMessage.java
Sending Message: Hello, World! Received Message: Hello, World!
Deploying a Client Application
When you are ready to deploy your client application, you should make sure your Message Queue administrator knows your application’s needs. The checklist shown below summarizes the information required; consult with your administrator for specific details. In some cases, it may be useful to provide a range of values rather than a specific value. See Chapter 9, Managing Administered Objects, in Sun Java System Message Queue 4.2 Administration Guide for details on configuration and on attribute names and default values for administered objects.
- Administered Objects
- Connection Factories
- Type
- JNDI lookup name
- Other attributes
- Type (queue or topic)
- JNDI lookup name
- Physical destination name
- Type
- Name
- Attributes
- Maximum number of messages expected
- Maximum size of messages expected
- Maximum message bytes expected
- Name
- Port
- Properties
- Place dead messages on dead message queue?
- Log placement of messages on dead message queue?
- Discard body of messages placed on the dead message queue?
Example Application Code
The Message Queue installation includes example programs illustrating both JMS and JAXM messaging (see Chapter 5, Working with SOAP Messages). They are located in the following directories:
Each directory (except the JMS directory) contains a README file describing the source files included in that directory. The table below lists the directories of interest to Message Queue Java clients.
Sample programs showing how to create and deploy a JMS client in Message Queue, including the steps required to create administered objects and to look up such objects with JNDI from within client code
Sample programs demonstrating the use of the JMS API with Message Queue
Sample programs demonstrating the use of SOAP messages in conjunction with JMS in Message Queue
Four subdirectories containing source code for the following:
- A GUI application using the JMS API to implement a simple chat application
- A GUI application using the Message Queue JMS monitoring API to obtain a list of queues from a Message Queue broker and browse their contents with a JMS queue browser
- The Message Queue Ping demo program
- The Message Queue Applet demo program
Sample programs demonstrating the use of the JMS API to monitor a message broker
Examples for plugging in a PointBase and an Oracle database
Examples of imqobjmgr command files
- Connection Factories