精通ejb【八】
2008-01-05 08:51:38 来源:WEB开发网核心提示:例子代码 rmi-iiop ejb客户端例子 A java RMI-IIOP client with a PRoprietary EJB server. package com.wiley.compBooks.roman.corba.helloworld; import javax.ejb.*; import 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", <..ORB class..>);
* 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);
}
}
- ››精通Photoshop之通道详解
- ››EJB2.0有状态SessionBean
- ››EJB 3.0 在 WAS V7 上开发部署的最佳实践
- ››精通 Grails: 使用 Grails 进行单元测试(单元测试...
- ››精通 Grails: 创建自定义插件
- ››精通 Grails: 在企业中使用 Grails
- ››精通 Grails: Grails 与移动 Web
- ››精通 Grails: Grails 与遗留数据库
- ››精通 Grails: RESTful Grails
- ››精通 Grails: 用 JSON 和 Ajax 实现异步 Grails
- ››精通 Grails: 用定制 URI 和 codec 优化 Grails 中...
- ››精通 Grails: 身份验证和授权
更多精彩
赞助商链接