OC4JにデプロイされるEJBクライアントのサンプル・クライアント・コード

Business Component Browserのかわりに次の2つのサンプル・クライアント・コードを使用し、ビジネス・コンポーネント・プロジェクトについて、EJBセッションBeanとしてOC4JインスタンスにデプロイされたAppModuleへの接続をテストすることもできます。

注意: クライアントの実行には、適切なライブラリを含める必要があります。 場合によって、その情報をこれらのサンプルに追加する必要があります。

サンプル1

package mypackage;

import oracle.jbo.*;
import oracle.jbo.client.Configuration;
import java.util.Hashtable;

public class sampleClient
{
  public sampleClient()
  {
  }
  public static void main(String arg[]) {

  String _am = "mypackage.MypackageModule"; //App Module name
  String _cf = "MypackageModuleOracleAS"; // EJB Config name
  String voMemberName = "DeptView"; // Name of the View Object

 //Use _wcf if you are accessing Business Components application deployed as
 //as EJB session bean in weblogic

  ApplicationModule myam =
    (ApplicationModule)Configuration.createRootApplicationModule(_am,_cf);

  // Find the viewobject included in the appmodule
    ViewObject vo = myam.findViewObject(voMemberName);

  // Iterate over the viewobject to get the rows

        Row r = vo.first();
         while (r != null)
         {
            // Iterate over the current row and get
            // all the attributes
            for (int i = 0; i  vo.getAttributeCount(); i++)
            {
               String attrName = vo.getAttributeDef(i).getName();
               String attrVal  = r.getAttribute(i).toString();
               System.out.println(attrName + " = " + attrVal);
            }
            r = vo.next();
         }


  Configuration.releaseRootApplicationModule(myam, true);

  }
}

    

サンプル2

import javax.naming.*;
import oracle.jbo.*;
import oracle.jbo.client.*;

/***
 ** Sample client code for connecting to an appmdoule deployed
 ** as an EJB session bean to Oracle Application Server.
 ***/
public class SampleEJBClient
{
   public static void main(String[] args)
   {
      /**
       ** Change the following String's to match your appmodule name and the
       ** name of the viewobject included in that appmodule.
       **
       */
      String amDefName = "package1.Package1Module";
      String voMemberName = "DeptView";

      /**
       ** Change the following to point to the datasource name
       ** (defined in Oracle Application Server).
       ** to your database.
       **/
      String dataSourceName = "jdbc/OracleCoreDS";

      /**
       ** Change the following to point to the hostname where the
       ** appmodule is deployed i.e. the host where Oracle Application Server is running.
       **/
      String applicationServerHost = "localhost";

      /**
       ** Change the following username and password
       ** to be used for connecting to the Oracle Application Server.
       **/
      String iasUserName = "admin";
      String iasUserPasswd = "welcome";

      /**
       ** Change the following to match the name of the J2EE application
       ** containing the deployed appmodule session bean.
       **/
      String applicationName = "Workspace1_Project1_ejb";

      ApplicationModule am = null;
      try
      {
         // Setup JNDI environment for looking up
         // the appmodule
         Hashtable env = new Hashtable();
         env.put(Context.INITIAL_CONTEXT_FACTORY,JboContext.JBO_CONTEXT_FACTORY);
         env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_EJB_IAS);
         env.put(JboContext.HOST_NAME, applicationServerHost);
         env.put(JboContext.SECURITY_PRINCIPAL, iasUserName);
         env.put(JboContext.SECURITY_CREDENTIALS, iasUserPasswd);
         env.put(JboContext.APPLICATION_PATH, applicationName);
         Context ctx = new InitialContext(env);

         // Lookup appmodule home
         ApplicationModuleHome amHome = (ApplicationModuleHome)ctx.lookup(amDefName);

         // Create an appmodule instance
         am = amHome.create();

         // Connect to the database using the datasource
         am.getTransaction().connectToDataSource(null,
                                                 dataSourceName,
                                                 false);

         // Find the viewobject included in the appmodule
         ViewObject vo = am.findViewObject(voMemberName);

         // Iterate over the viewobject to get the rows
         Row r = vo.first();
         while (r != null)
         {
            // Iterate over the current row and get
            // all the attributes
            for (int i = 0; i  vo.getAttributeCount(); i++)
            {
               String attrName = vo.getAttributeDef(i).getName();
               String attrVal  = r.getAttribute(i).toString();
               System.out.println(attrName + " = " + attrVal);
            }
            r = vo.next();
         }
      }
      catch (NamingException ex)
      {
         System.out.println("NamingException " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (ApplicationModuleCreateException ex)
      {
         System.out.println("Unable to create application module: " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (JboException ex)
      {
         System.out.println("JboException: " + ex.getMessage());
         ex.printStackTrace();
      }
      catch (Exception ex)
      {
         System.out.println("Exception: " + ex.getMessage());
         ex.printStackTrace();

      }
      finally
      {
         if (am != null)
         {
            am.getTransaction().disconnect();
            am.remove();
         }
      }
   }

}    

注意: クライアントで使用できないEJBセッションFacade Bean(BMT)を再デプロイするときは、クライアント・コードでappmoudle.remove()またはappmodule.getTransaction().disconnect()をコールしてJDBC接続を閉じる必要があります。 これ以外の場合は例外が発生します。


関連項目

OC4Jアプリケーション・サーバーの使用

 

Copyright © 1997, 2004, Oracle. All rights reserved.