程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Beans入門必讀之經典EJB例子代碼

Beans入門必讀之經典EJB例子代碼

編輯:關於JAVA

rmi-iiop ejb客戶端例子

A Java RMI-IIOP client with a proprIEtary EJB server.

package com.wiley.compBooks.roman.corba.helloworld;

import Javax.ejb.*;

import Javax.naming.*;

import Javax.rmi.*;

import Java.util.PropertIEs;

import Javax.transaction.UserTransaction;

/**

* This class is an example of clIEnt code that invokes

* methods on a simple stateless session bean.

*/

public class RMIClIEnt {

public static void main(String[] args) {

try {

/*

* Get System propertIEs for JNDI initialization

*/

Properties props = System.getPropertIEs();

/*

* Use JNDI to look up the home object

*/

Context ctx = new InitialContext(props);

HelloHome home = (HelloHome)

Javax.rmi.PortableRemoteObject.narrow(

ctx.lookup("HelloHome"),

HelloHome.class);

/*

* Use JNDI to look up the JTA

* UserTransaction interface

*/

UserTransaction userTran = (UserTransaction)

ctx.lookup("Javax.transaction.UserTransaction");

/*

* Start the transaction

*/

userTran.begin();

/*

* Use the home object to create the Hello EJB Object

*/

Hello hello = home.create();

/*

* Call the hello() method, and print it

*/

System.out.println(hello.hello());

/*

* Done with EJB Object, so remove it

*/

hello.remove();

/*

* Commit the transaction

*/

userTran.commit();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Example RMI-IIOP EJB clIEnt

A CORBA clIEnt with a CORBA-based EJB server.

package com.wiley.compBooks.roman.corba.helloworld;

import Java.util.*;

import org.omg.CosNaming.*;

import org.omg.CosTransactions.*;

public class CORBAClIEnt {

public static void main(String[] args) throws Exception {

/*

* Initialize the ORB.

*

* A more portable way to do this is:

*

* Properties p = new PropertIEs();

* p.put("org.omg.CORBA.ORBClass", );

* org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, p);

*/

org.omg.CORBA.ORB orb = com.inprise.ejb.Global.orb();

/*

* Get a reference to a naming context

*/

NamingContext context = NamingContextHelper.narrow

(orb.resolve_initial_references("NameService"));

/*

* Look up the home object using COS Naming

*/

NameComponent[] names = { new NameComponent("HelloHome", "") };

HelloHome helloHome = HelloHomeHelper.narrow

(context.resolve(names));

/*

* Get the CORBA OTS Current interface for

* controlling transactions

*/

Current currentTX = CurrentHelper.narrow

(orb.resolve_initial_references("TransactionCurrent"));

/*

* Begin the transaction

*/

currentTX.begin();

/*

* Use the home object to create an EJB object

*/

Hello hello = helloHome.create();

/*

* Call a business method

*/

System.out.println(hello.hello());

/*

* Remove the EJB object

*/

hello.remove();

/*

* Commit the transaction

*/

currentTX.commit(true);

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved