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

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

サンプル1


package mypackage; import oracle.jbo.*; import oracle.jbo.client.Configuration; public class sampleClient { public sampleClient() { } public static void main(String arg[]) { String _am = "mypackage.MypackageModule"; //App Module name String _cf = "MypackageModule9iAS"; // EJB Config name String voMemberName = "DeptView"; // Name of the View Object //Use _wcf if you are accessing BC4J 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.job.client.*; /*** ** Sample client code for connecting to an appmdoule deployed ** as an EJB session bean to Oracle9iAS 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 Oracle9iAS). ** 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 Oracle9iAS application ** server is running. **/ String applicationServerHost = "localhost"; /** ** Change the following username and password ** to be used for connecting to the Oracle9iAS 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接続を閉じる必要があります。これ以外の場合は例外が発生します。


関連項目

J2EE EJB JARのスタンドアロンOC4JまたはOracle9iASへのデプロイ
orion-ejb-jar.xmlの編集
BC4JのEJBセッションBeanとしてのOC4Jへのデプロイ