この例ではNovell exteNd WSSDK ServiceがJMSキューと情報をやりとりする方法について説明します。 この例を実行するには、jBroker MQなどのJMS実装が必要です。 WebサービスはJMSプロバイダに対する着信要求をすべて委任します。1 Queueインタフェース
JMS Queueのインタフェースを次に示します。package 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
インタフェースによってクライアントは名前が付けられたキューにメッセージを送信するまたは名前が付けられたキューからメッセージを受信できます。 同様のプログラムではキューがクライアントを実行する前に作成され、ユーザがこのキューの名前を認識することを前提とします。 キューに対する実際のインタフェースには存在しないキューからの送受信といったさまざまなエラー状況に対応する例外があります。2 Senderクライアント
Senderクライアントはコマンドラインから3つのパラメータを取得し、メッセージをキューへ送信するアプリケーションです。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 Receiverクライアント
ReceiverクライアントはQueueインタフェースからreceiveMessageメソッドを使用します。 アプリケーションはコマンドラインからパラメータを2つ取得し、キューからメッセージを受信します。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実装
Queue実装はJMSキューに対する着信要求を委任します。 この例では各要求の新しいキュー接続を作成します。 最新バージョンではパフォーマンスの向上のためにキュー接続をキャッシュする可能性があります。JMSの宛先検索用に正しいJNDCファクトリを取得するため、サーバはServletベースクラスのinitメソッドを上書きします。 またサーバが-DORBDefaultInitRef=iioploc://localhost:3506フラグで実行され、クライアントがJMSサーバを解決できることを確認する必要があります。
たとえば、サーバがjwebserv Webサーバに展開されると、Webサーバを次のように開始する必要があります。
Queueサービス実装を次に示します。jwebserv -verbose -J-DORBDefaultInitRef=iioploc://localhost:3506 services.warJMS例のコンパイル、展開、および実行に関する詳細については、Hello Worldの例を参照してください。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.