This example illustrates how a Novell exteNd WSSDK Service can interact with a JMS queue. To run this example, you need a JMS implementation such as jBroker MQ. This Web Service simply delegates all incoming requests to a JMS provider.1 Queue Interface
Below is a very simple interface to a JMS queue:Thepackage jms; import java.rmi.Remote; import java.rmi.RemoteException; public interface Queue extends Remote { /** Send a message to a JMS queue. @param queue The name of the queue. @param message The message to send. */ void sendMessage(String queue, String message) throws RemoteException; /** Receive a message from a JMS queue. @param queue The name of the queue. @return A message or null if none available. */ String receiveMessage(String queue) throws RemoteException; }Queue
interface allows clients to send messages to a named queue, or receive messages from a named queue. This sample program assumes that queues have been created prior to running the client, and that users somehow know the names of these queues. A real-world interface to a queue would have appropriate exceptions for various error conditions, including sending or receiving from a non-existent queue.2 The Sender Client
The sender client is a very simple application that takes three parameters from the command line and sends a message to a queue:package jms; import java.rmi.Remote; import java.rmi.RemoteException; import javax.xml.rpc.Stub; import javax.naming.InitialContext; public class Sender { public static void main(String[] args) throws Exception { | if (args.length != 3) { | | System.out.println("usage: Sender <location> <queue> <message>"); | | System.exit(1); | } | | String location = args[0]; | String queueName = args[1]; | String message = args[2]; | | // get the initial context | InitialContext ctx = new InitialContext(); | | // lookup the service | QueueService svc = (QueueService) | ctx.lookup("xmlrpc:soap:jms.QueueService"); | | // get the queue stub | Queue queue = (Queue) svc.getQueuePort(); | | // set the end point address | ((Stub)queue)._setProperty("javax.xml.rpc.service.endpoint.address", | location); | | // send a message to the queue service | queue.sendMessage(queueName, message); } }3 The Receiver Client
The receiver client uses the receiveMessage method from the queue interface. The application takes two parameters from the command line and receives a message from a queue:package jms; import java.rmi.Remote; import java.rmi.RemoteException; import javax.xml.rpc.Stub; import javax.naming.InitialContext; public class Receiver { public static void main(String[] args) throws Exception { | if (args.length != 2) { | | System.out.println("usage: Receiver <location> <queue>"); | | System.exit(1); | } | | String location = args[0]; | String queueName = args[1]; | | // get the initial context | InitialContext ctx = new InitialContext(); | | // lookup the service | QueueService svc = (QueueService) | ctx.lookup("xmlrpc:soap:jms.QueueService"); | | // get the queue stub | Queue queue = (Queue) svc.getQueuePort(); | | // set the end point address | ((Stub)queue)._setProperty("javax.xml.rpc.service.endpoint.address", | location); | | // send a message to the queue service | String message = queue.receiveMessage(queueName); | System.out.println( (message == null) ? "no message" : message); } }4 Queue Web Service Implementation
The Queue implementation delegates incoming requests to a JMS queue. For simplicity, this example create a new queue connection for each request. A more sophisticated version might cache queue connections for better performance.In order to obtain the correct JNDI factory for looking up JMS destinations, the server overrides the init method of the Servlet base class. Note also that the server must be run with the -DORBDefaultInitRef=iioploc://localhost:3506 flag to ensure that client can resolve the JMS server.
As an example, if this server is deployed in the jwebserv Web Server, you have to start the Web Server like this:
The queue service implementation is shown below:jwebserv -verbose -J-DORBDefaultInitRef=iioploc://localhost:3506 services.warPlease refer to the Hello World example for detailed instructions on compiling, deploying and running the JMS example.package jms; import java.rmi.Remote; import java.rmi.RemoteException; import javax.jms.Queue; import javax.jms.Session; import javax.jms.Message; import javax.jms.TextMessage; import javax.jms.JMSException; import javax.jms.QueueSession; import javax.jms.QueueReceiver; import javax.jms.QueueSender; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import com.sssw.jms.api.JMQQueue; import com.sssw.jms.api.JMQQueueConnectionFactory; public class QueueImpl extends Queue_ServiceSkeleton { private QueueConnection _conn; private QueueConnectionFactory _factory; public void init() throws javax.servlet.ServletException { | super.init(); | | // get jBroker MQ connection factory and connection | try { | | _factory = new JMQQueueConnectionFactory(); | | _conn = _factory.createQueueConnection(); | | _conn.start(); | } catch (JMSException jex) { | | throw new javax.servlet.ServletException(jex.getMessage()); | } } public void sendMessage(String q, String m) throws RemoteException { | try { | | Queue queue = JMQQueue.getQueue(q); | | QueueSession session = _conn.createQueueSession(false, | | Session.DUPS_OK_ACKNOWLEDGE); | | QueueSender sender = session.createSender(queue); | | TextMessage msg = session.createTextMessage(m); | | sender.send(msg); | | session.close(); | } catch (Exception ex) { | | ex.printStackTrace(); | | throw new RemoteException("JMS: " + ex); | } } public String receiveMessage(String q) throws RemoteException { | try { | | Queue queue = JMQQueue.getQueue(q); | | QueueSession session = _conn.createQueueSession(false, | | Session.AUTO_ACKNOWLEDGE); | | QueueReceiver receiver = session.createReceiver(queue); | | TextMessage msg = (TextMessage) receiver.receive(1000); | | session.close(); | | return msg != null ? msg.getText() : null; | } catch (Exception ex) { | | ex.printStackTrace(); | | throw new RemoteException("JMS: " + ex); | } } }
Copyright © 2000-2003, Novell, Inc. All rights reserved.