Cosminexus V9 アプリケーションサーバ 機能解説 基本・開発編(コンテナ共通機能)

[目次][用語][索引][前へ][次へ]

4.10.4 onMessageメソッドでの業務ロジックの実装

onMessageメソッドに,次に示す業務ロジックを実装します。

public TP1OutMessage onMessage(TP1InMessage in)
{
  //入力パラメタ取得
  byte[] inputdata = in.getInputData();
  ・・・
  //出力データオブジェクトの生成
  TP1OutMessage out = in.createOutMessage();
  ・・・
  ・・・
  //出力データ格納領域の長さを取得
  int outLen = out.getMaxOutputLength();
  ・・・
  try {
    ・・・
    //出力データ格納領域の取得
    byte[] outputdata = out.getOutputData(outLen);
    ・・・
    //業務処理
    //outputdata中に出力データを格納
    ・・・
  } catch(Exception e) {
    //errorLenにエラー処理用の出力データ長を設定
    //エラー処理用の出力データ格納領域の取得
    byte[] outputdata = out.getOutputData(errorLen);
    ・・・
    //outputdata中にエラー情報を格納
    ・・・
  }
  return out;
}

 

Message-driven Bean(サービス)のコーディング例は,トランザクション連携機能を使用しない場合と,トランザクション連携機能を使用する場合の2種類があります。Message-driven Bean(サービス)のコーディング例を次に示します。

●トランザクション連携機能を使用しない場合のコーディング例
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
 
import com.hitachi.software.ejb.adapter.tp1.TP1InMessage;
import com.hitachi.software.ejb.adapter.tp1.TP1MessageListener;
import com.hitachi.software.ejb.adapter.tp1.TP1OutMessage;
 
public class SampleMDB
implements TP1MessageListener, MessageDrivenBean {
 
    private MessageDrivenContext context;
    public TP1OutMessage onMessage(TP1InMessage in) {
        //入力パラメタ取得
        byte[] inputdata = in.getInputData();
 
        //出力データオブジェクトの生成
        TP1OutMessage out = in.createOutMessage();
 
        //出力データ格納領域の長さを取得
        int outLen = out.getMaxOutputLength();
 
        UserTransaction ut = null;
        Connection con = null;
        PreparedStatement prepStmt = null;
        try {
            //DB更新する業務処理を実装
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource)ic.lookup(
                                "java:comp/env/jdbc/SampleDB");
            ut = context.getUserTransaction();
            ut.begin();
            con = ds.getConnection();
            prepStmt = con.prepareStatement(
                              "update sample set name = ? where id = 1");
            prepStmt.setString(1, new String(inputdata));
            prepStmt.executeUpdate();
            ut.commit();
            byte[] result ="update complete.".getBytes();
            //出力データ格納領域の取得
            byte[] outputdata = out.getOutputData(outLen);
 
            //業務処理の結果を出力データにコピー
            System.arraycopy(result,0,outputdata,0,result.length);
        } catch(Exception e) {
            //エラー処理
            try {
                if(ut != null) {
                    ut.rollback();
                }
            } catch(Exception ex) {
            }
            byte[] wk = "An error occurred.".getBytes();
 
            //エラー処理用の出力データ格納領域の取得
            byte[] outputdata = out.getOutputData(wk.length);
            //エラー処理の内容を出力データにコピー
            System.arraycopy(wk,0,outputdata,0,wk.length);
        }
        finally {
            if(prepStmt != null) {
                try {
                    prepStmt.close();
                } catch(Exception exp) {
                }
            }
            if(con != null) {
                try {
                    con.close();
                } catch(Exception exp) {
                }
            }
        }
        return out;
    }
 
    public void ejbCreate() {
    }
    public void ejbRemove() throws EJBException {
    }
    public void setMessageDrivenContext(MessageDrivenContext ctx)
            throws EJBException {
        this.context = ctx;
    }
}
 
●トランザクション連携機能を使用する場合のコーディング例
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
import com.hitachi.software.ejb.adapter.tp1.TP1InMessage;
import com.hitachi.software.ejb.adapter.tp1.TP1MessageListener;
import com.hitachi.software.ejb.adapter.tp1.TP1OutMessage;
public class SampleMDB
implements TP1MessageListener, MessageDrivenBean {
    public TP1OutMessage onMessage(TP1InMessage in) {
        //入力パラメタ取得
        byte[] inputdata = in.getInputData();
 
        //出力データオブジェクトの生成
        TP1OutMessage out = in.createOutMessage();
 
        //出力データ格納領域の長さを取得
        int outLen = out.getMaxOutputLength();
        Connection con = null;
        PreparedStatement prepStmt = null;
        try {
            //DB更新する業務処理を実装
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource)ic.lookup(
                                "java:comp/env/jdbc/SampleDB");
            con = ds.getConnection();
            prepStmt = con.prepareStatement(
                              "update sample set name = ? where id = 1");
            prepStmt.setString(1, new String(inputdata));
            prepStmt.executeUpdate();
            byte[] result ="update complete.".getBytes();
 
            //出力データ格納領域の取得
            byte[] outputdata = out.getOutputData(outLen);
            //業務処理の結果を出力データにコピー
            System.arraycopy(result,0,outputdata,0,result.length);
        } catch(Exception e) {
            //エラー処理
            byte[] wk = "An error occurred.".getBytes();
 
            //エラー処理用の出力データ格納領域の取得
            byte[] outputdata = out.getOutputData(wk.length);
 
            //エラー処理の内容を出力データにコピー
            System.arraycopy(wk,0,outputdata,0,wk.length);
        }
        finally {
            if(prepStmt != null) {
                try {
                    prepStmt.close();
                } catch(Exception exp) {
                }
            }
            if(con != null) {
                try {
                    con.close();
                } catch(Exception exp) {
                }
            }
        }
        return out;
    }
 
    public void ejbCreate() {
    }
    public void ejbRemove() throws EJBException {
    }
    public void setMessageDrivenContext(MessageDrivenContext ctx)
            throws EJBException {
    }
}