JMS およびメッセージングを直接使用するためのプログラミング

エンタープライズ・アプリケーションは、Java™ Message Service (JMS) プログラミング・インターフェースを直接使用して、メッセージング・サービスと、ビジネス・ロジックを実装するメソッドを提供できます。

このタスクについて

WebSphere® Application Server は、JMS プログラミング・インターフェースに基づく通信方法として非同期メッセージングをサポートします。 JMS を使用すると、エンタープライズ・アプリケーションは、JMS 宛先 (キューまたはトピック) を使用して、その他の JMS クライアントとメッセージを非同期に交換することができます。 エンタープライズ・アプリケーションは、宛先上で明示的にメッセージのポーリングを行うことができます。

JNDI を使用して、接続ファクトリーや宛先など、メッセージング・プロバイダーの構成情報を取得しないことを選択した場合、代わりにメッセージング・プロバイダーによって提供される API を使用して、その構成情報をプログラマチックに指定できます。

JMS アプリケーションと従来の IBM MQ アプリケーションの間でメッセージを送信したい場合、JMS メッセージ構造が IBM MQ メッセージにどのようにマップされるかを考慮する必要があります。これには、IBM MQ を使用して、2 つの JMS アプリケーション間でのメッセージの送信を取り扱うというシナリオが含まれます。例えば、メッセージ・ブローカーとしての IBM MQ の使用です。

デフォルトでは、IBM MQ キューに保持される JMS メッセージは、JMS メッセージ・ヘッダー情報の一部を保持するために MQRFH2 ヘッダーを使用します。IBM MQ の従来のアプリケーションの多くは、このヘッダーを含むメッセージを処理することができないため、IBM MQ Workflow アプリケーションの場合の MQWIH などの、独自の特性ヘッダーを必要とします。 JMS メッセージ構造が IBM MQ メッセージにどのようにマップされるのかについて詳しくは、IBM MQ インフォメーション・センターの『JMS メッセージのマッピング』セクションを参照してください。

手順

以下の例では、デフォルトのメッセージング・プロバイダーのリソースをプログラマチックに構成する方法が示されています。

この例では、サービス統合バスへの JMS 接続を com.ibm.websphere.sib パッケージ中の API を使用して作成します。 これは、管理上構成した接続ファクトリーを検索するために JNDI を使用する方法に代わるものです。接続が確立された後に、このサンプル・プログラムではコンソールからの入力行が読み取られて、指定された宛先に JMS テキスト・メッセージとして送信されます。

この例はシン・クライアント・アプリケーションとして、またはスタンドアロン・クライアント・アプリケーションとして実行できます。

/* 
 * Sample program 
 * © COPYRIGHT International Business Machines Corp. 2009
 * All Rights Reserved * Licensed Materials - Property of IBM
 *
 * This sample program is provided AS IS and may be used, executed,
 * copied and modified without royalty payment by customer
 *
 * (a) for its own instruction and study,
 * (b) in order to develop applications designed to run with an IBM
 *     WebSphere product for the customer's own internal use.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import com.ibm.websphere.sib.api.jms.JmsConnectionFactory;
import com.ibm.websphere.sib.api.jms.JmsFactoryFactory;
import com.ibm.websphere.sib.api.jms.JmsQueue;
import com.ibm.websphere.sib.api.jms.JmsTopic;

/**
 * Sample code to programmatically create a connection to a bus and
 * send a text message.
 * 
 * Example command lines:
 *   SIBusSender topic://my/topic?topicSpace=Default.Topic.Space MyBus localhost:7276
 *   SIBusSender queue://myQueue MyBus localhost:7286:BootstrapSecureMessaging InboundSecureMessaging 
 */
public class SIBusSender {
  
  /**
   * @param args DEST_URL,BUS_NAME,PROVIDER_ENDPOINTS,[TRANSPORT_CHAIN]
   */
  public static void main(String[] args) throws JMSException, IOException {
    
    // Parse the arguments
    if (args.length < 3) {
      throw new IllegalArgumentException(
          "Usage: SIBusSender <DEST_URL> <BUS_NAME> <PROVIDER_ENDPOINTS> [TARGET_TRANSPORT_CHAIN]");
    }    
    String destUrl = args[0];
    String busName = args[1];
    String providerEndpoints = args[2];    
    String targetTransportChain = "InboundBasicMessaging";
    if (args.length >= 4) targetTransportChain = args[3];
    
    // Obtain the factory factory
    JmsFactoryFactory jmsFact = JmsFactoryFactory.getInstance();

    // Create a JMS destination
    Destination dest;
    if (destUrl.startsWith("topic://")) {
      JmsTopic topic = jmsFact.createTopic(destUrl);
      // Setter methods could be called here to configure the topic
      dest = topic ;
    }
    else {
      JmsQueue queue = jmsFact.createQueue(destUrl);
      // Setter methods could be called here to configure the queue
      dest = queue;
    }
        
    // Create a unified JMS connection factory
    JmsConnectionFactory connFact = jmsFact.createConnectionFactory();
    
    // Configure the connection factory
    connFact.setBusName(busName);
    connFact.setProviderEndpoints(providerEndpoints);
    connFact.setTargetTransportChain(targetTransportChain);
    
    // Create the connection
    Connection conn = connFact.createConnection();
    
    Session session = null;
    MessageProducer producer = null;
    try {
      
      // Create a session
      session = conn.createSession(false, // Not transactional 
                                   Session.AUTO_ACKNOWLEDGE);
      
      // Create a message producer
      producer = session.createProducer(dest);
      
      // Loop reading lines of text from the console to send
      System.out.println("Ready to send to " + dest + " on bus " + busName);
      BufferedReader lineInput = new BufferedReader(new InputStreamReader(System.in));      
      String line = lineInput.readLine();
      while (line != null && line.length() > 0) {
        
        // Create a text message containing the line
        TextMessage message = session.createTextMessage();
        message.setText(line);
        
        // Send the message
        producer.send(message,
                      Message.DEFAULT_DELIVERY_MODE,
                      Message.DEFAULT_PRIORITY,
                      Message.DEFAULT_TIME_TO_LIVE);        
        
        // Read the next line
        line = lineInput.readLine();
      }
      
    }
    // Finally block to ensure we close our JMS objects 
    finally {
      
      // Close the message producer
      try {
        if (producer != null) producer.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close message producer: " + e);
      }
      
      // Close the session
      try {
        if (session != null) session.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close session: " + e);
      }
      
      // Close the connection
      try {
        conn.close();
      }
      catch (JMSException e) {
        System.err.println("Failed to close connection: " + e);
      }
      
    }
  }

}

トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tmj_pgmng
ファイル名:tmj_pgmng.html