Interstage Application Server J2EE ユーザーズガイド
目次 索引 前ページ次ページ

第3部 EJB編> 第28章 Bean共通編> 28.3 トランザクションを使用したアプリケーション

28.3.3 JMSのトランザクション機能を使用する

 JMSのトランザクションを使用した場合の、アプリケーションの開発方法について説明します。

■ トランザクション処理の流れ

 以下に、JMSのトランザクション機能を使用する場合の処理の流れを示します。

image

■ 記述例

 JMSのトランザクションを使用する場合の記述例を示します。JMSのDestinationがTopicの場合の例です。

package pkgSample;

      import javax.ejb.*;
      import java.rmi.*;
      import javax.jms.*;
      import java.util.*;
      import javax.naming.*;

      public class SampleBean implements SessionBean
      {
          SessionContext ctx = null;
          TopicConnection topicConnection = null;
          Topic topic = null;

          // receive SessionContext
          public void setSessionContext(SessionContext ctx) throws EJBException {
                    this.ctx = ctx;
           }

          public void ejbCreate() throws EJBException, CreateException{
                    Context context = null;
                    TopicConnectionFactory topicConnectionFactory = null;

                       /* Contextを作成します。*/
                       try{
                          context=new InitialContext();
                          /*Topicへの接続を行います*/
                          topicConnectionFactory=(TopicConnectionFactory)context.lookup("java:comp/env/jms/MyTopicConnectionFactory");
                          topicConnection = (topicConnectionFactory.createTopicConnection();
                       }
          ...
      }

      public void publishNews(String[] s) throws EJBException{
          TopicSession topicSession = null;
          TopicPublisher topicPublisher = null;
          TextMessage message = null;

          /*メッセージを送信します。*/
          try{
             topicSession = topicConnection.createTopicSession(true,0);
             topicPublisher = topicSession.createPublisher(topic);
             message = topicSession.createTextMessage();
             for (int i=0; i<s, length; i++){
             message.setText(s[i]);
             topicPublisher.publish(message);
          }

          /*トランザクションのコミット*/
          topicSession.commit();
          }
          ...
          }

          public void ejbRemove() throws EJBException{

          /*コネクションを解放します。*/
          try{
          if(topicConnection! = null){
          topicConnection.close();
          }
          }
          }
          ...
          }

目次 索引 前ページ次ページ

All Rights Reserved, Copyright (C) 富士通株式会社 2006