服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

精通ejb(8)


  精通ejb(8)

例子代码
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);
}
}

(全文完)

扫描关注微信公众号