websphere ejb 远程/本地调用总结
来源:互联网 发布:excel数据透视表的作用 编辑:程序博客网 时间:2024/05/22 13:00
1:本地调用
前提:
(1)被调用者实现了LOCAL接口
(2)调用者和被调用者应该在同一EJB模块打包文件(ear)內,由于是本地调用,也就是说调用者和被调用者应运行于同一个ejb容器内,所以,想用类似main函数调用的企图都是不能成功的(因为main函数不可能运行于ejb容器)。
(3)调用者的部署描述(ejb-jar.xml)中有关于被调用者的Local ref的描述。
示例如下:
比如我有一个无状态sessionBean(被访问者):MapSessionBean,一个访问用的sessionBean:AccessBean
其中AccessBean的ejb-jar.xml应有被调用者的Local ref描述,否则,不能进行本地调用:
<!--sp-->DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar id="ejb-jar_ID">
<display-name>TestEJBdisplay-name>
<enterprise-beans>
<session id="MapSession">
<ejb-name>MapSessionejb-name>
<home>co.test.bean.MapSessionHomehome>
<remote>co.test.bean.MapSessionremote>
<local-home>co.test.bean.MapSessionLocalHomelocal-home>
<local>co.test.bean.MapSessionLocallocal>
<ejb-class>co.test.bean.MapSessionBeanejb-class>
<session-type>Statelesssession-type>
<transaction-type>Containertransaction-type>
<ejb-local-ref id="EJBLocalRef_1165387097531">
<ejb-ref-name>ejb/MapSessionejb-ref-name>
<ejb-ref-type>Sessionejb-ref-type>
<local-home>co.test.bean.MapSessionLocalHomelocal-home>
<local>co.test.bean.MapSessionLocallocal>
<ejb-link>MapSessionejb-link>
ejb-local-ref>
session>
<session id="AccessBean">
<ejb-name>AccessBeanejb-name>
<home>co.test.bean.AccessBeanHomehome>
<remote>co.test.bean.AccessBeanremote>
<local-home>co.test.bean.AccessBeanLocalHomelocal-home>
<local>co.test.bean.AccessBeanLocallocal>
<ejb-class>co.test.bean.AccessBeanBeanejb-class>
<session-type>Statelesssession-type>
<transaction-type>Containertransaction-type>
<ejb-local-ref id="EJBLocalRef_1165393609046">
<ejb-ref-name>ejb/MapSessionejb-ref-name>
<ejb-ref-type>Sessionejb-ref-type>
<local-home>co.test.bean.MapSessionLocalHomelocal-home>
<local>co.test.bean.MapSessionLocallocal>
<ejb-link>MapSessionejb-link>
ejb-local-ref>
session>
enterprise-beans>
ejb-jar>
本地调用代码如下:
...{
MapSessionLocalHome mapSessionLocalHome = null;
MapSessionLocal mapSessionLocal = null;
InitialContext initContext = null;
final String JNDIName = "java:comp/env/ejb/MapSession";
try ...{
System.out.println("in invoke()!!!!!!!");
initContext = new InitialContext();
Object obj = initContext.lookup(JNDIName);
mapSessionLocalHome = (MapSessionLocalHome)obj;
mapSessionLocal = mapSessionLocalHome.create();
Person person = new Person("lcl",555);
mapSessionLocal.setMapValue("key1", person);
Person tempPerson = (Person)mapSessionLocal.getMapValue("key1");
tempPerson.setName("wangwu");
tempPerson .setAge(88);
Person changedPerson = (Person)mapSessionLocal.getMapValue("key1");
System.out.println("after changed: " + changedPerson.getName() + "---" + changedPerson.getAge());
}
catch(Exception e)
...{
e.printStackTrace();
}
}
值得一提的是,在本地调用中,对一个object的操作,是在同一内存块中进行的。具体到上面的代码,tempPerson的改变,已经影响到了changedPerson的值。
2:远程方法调用:
前提:被调用者实现了REMOTE接口,适用于不在同一模块中的ejb,servlet.
...{
MapSessionHome mapSessionHome = null;
MapSession mapSession = null;
InitialContext initContext = null;
final String JNDIName = "ejb/co/test/bean/MapSessionHome";
try ...{
System.out.println("in MapSessionClient!!!!!!!");
initContext = new InitialContext();
Object obj = initContext.lookup(JNDIName);
mapSessionHome =
(MapSessionHome) PortableRemoteObject.narrow(
obj,
MapSessionHome.class);
mapSession = mapSessionHome.create();
Person person1 = new Person("zhangsan", 100);
mapSession.setMapValue("key1",person1);
Person tempPerson = (Person)mapSession.getMapValue("key1");
tempPerson.setName("lisi");
tempPerson.setAge(500);
System.out.println("before changed: " + tempPerson.getName() + "---" + tempPerson.getAge());
Person changedPerson = (Person)mapSession.getMapValue("key1");
System.out.println("after changed: " + changedPerson.getName() + "---" + changedPerson.getAge());
} catch (Exception e) ...{
e.printStackTrace();
System.exit(0);
}
}
值得一提的是,在远程调用中,对一个object的操作,经过了corba处理,是不在同一内存块中进行的。具体到上面的代码,tempPerson的改变,不影响changedPerson的值(其实理所当然,一个是远程的对象,你个是本地内存对象)。
3:远程调用:适用于不在同一机器的远程调用:
对于websphere:
Provider URL: (PROVIDER_URL): iiop://server ip:2809/
其中:server ip为ejb容器ip地址.必须注意:在websphere服务器的配置中,有一项orb bootstrap setting的配置,它的默认配置如下:
Port:2809
hostname:localhost
其中,hostname必须改为server的ip地址,
hostname:192.168.0.81
否则,远程调用不能成功.
调用代码如下:
...{
System.out.println("in MapSessionRemoteTest");
MapSessionHome mapSessionHome = null;
MapSession mapSession = null;
String JNDIName = "ejb/co/test/bean/MapSessionHome";
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
p.put(Context.PROVIDER_URL,"iiop://192.168.0.81:2809/");
InitialContext initContext;
try
...{
initContext = new InitialContext(p);
Object obj = initContext.lookup(JNDIName);
mapSessionHome = (MapSessionHome) PortableRemoteObject.narrow(
obj,
MapSessionHome.class);
mapSession = mapSessionHome.create();
Person person1 = new Person("zhangsan", 100);
mapSession.setMapValue("key1",person1);
Person tempPerson = (Person)mapSession.getMapValue("key1");
tempPerson.setName("lisi");
tempPerson.setAge(500);
System.out.println("before changed: " + tempPerson.getName() + "---" + tempPerson.getAge());
Person changedPerson = (Person)mapSession.getMapValue("key1");
System.out.println("after changed: " + changedPerson.getName() + "---" + changedPerson.getAge());
}
catch (Exception e)
...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- websphere ejb 远程/本地调用总结
- websphere ejb 远程/本地调用总结
- ejb本地调用总结(websphere)
- EJB远程调用和本地调用
- 【EJB基础】远程调用和本地调用
- 【EJB学习笔记】——远程调用和本地调用
- EJB例子:既支持远程又支持本地调用
- 【EJB系列】(二)——JBOSS7中EJB的远程调用和本地调用
- 一步一步远程调用EJB
- ejb的远程调用
- EJB 远程调用问题
- weblogic——远程/近程调用EJB的方法总结
- (自结)EJB远程调用自己总结的例子
- 调用部署在websphere上的EJB
- WebSphere集群中调用EJB的方法
- EJB的远程调用与本地调用http://fay19860916.blog.163.com/blog/static/1186491192009819220664/
- ejb远程调用 之 weblogic
- Jboss6 远程调用ejb问题
- CoreJavaVI类和对象笔记
- 优美的琴声
- 一个悲凉的故事``一个伟大的母亲```
- Windows中的消息详细列表
- datalist 分页
- websphere ejb 远程/本地调用总结
- 多线程工作请求的同步
- SQL关于联合的基本概念和示例
- VelocityTemplateLanguage
- my_memmove
- Spring心得
- 挖潜无极限—数据挖掘技术与应用热点扫描
- 各种自定义消息通讯
- 有用的链接