tomcat 远程调用 websphere remote session bean 一例
今天尝试了一下在TOMCAT下调用WEBSPHERE REMOTE SESSION BEAN的例子,在此共享一下心得。(注意:此处使用的是EJB2.1规范)1.创建REMOTE STATELESS SESSION BEAN
首先构建ejb.jar包,可以使用任何工具,我这里使用了IBM RSA6.0工具来创建,比较简单,主要代码如下。
Remote Interface, 用于远程调用
package test.rmt.ejb;
import java.rmi.RemoteException;
/**
* Remote interface for Enterprise Bean: TestRemote
*/
public interface TestRemote extends javax.ejb.EJBObject {
/**
* 业务接口
*
* @return
* @throws RemoteException
*/
public String returnString() throws RemoteException;
}
Remote session bean 实现
package test.rmt.ejb;
/**
* Bean implementation class for Enterprise Bean: TestRemote
*/
public class TestRemoteBean implements javax.ejb.SessionBean {
static final long serialVersionUID = 3206093459760846163L;
private javax.ejb.SessionContext mySessionCtx;
/**
* getSessionContext
*/
public javax.ejb.SessionContext getSessionContext() {
return mySessionCtx;
}
/**
* setSessionContext
*/
public void setSessionContext(javax.ejb.SessionContext ctx) {
mySessionCtx = ctx;
}
/**
* 业务方法,返回一个字符串
*
* @return
*/
public String returnString(){
return "hello! this is a remote stateless session bean!";
}
/**
* ejbCreate
*/
public void ejbCreate() throws javax.ejb.CreateException {
}
/**
* ejbActivate
*/
public void ejbActivate() {
}
/**
* ejbPassivate
*/
public void ejbPassivate() {
}
/**
* ejbRemove
*/
public void ejbRemove() {
}
}
home接口, 用于远端生成业务实现
package test.rmt.ejb;
/**
* Home interface for Enterprise Bean: TestRemote
*/
public interface TestRemoteHome extends javax.ejb.EJBHome {
/**
* Creates a default instance of Session Bean: TestRemote
*/
public test.rmt.ejb.TestRemote create()
throws javax.ejb.CreateException,
java.rmi.RemoteException;
}
以上代码,除业务相关接口方法外,都是由工具自动生成的,当然也可以自己编写。
下面就是将生成ejb-jar.xml部署描述符, 这里就不详述了。见代码
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>RemoteEJbTestEJB</display-name>
<enterprise-beans>
<session id="TestRemote">
<ejb-name>TestRemote</ejb-name>
<home>test.rmt.ejb.TestRemoteHome</home>
<remote>test.rmt.ejb.TestRemote</remote>
<ejb-class>test.rmt.ejb.TestRemoteBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
然后打包并部署于WEBSPHERE中,并记录下该EJB MODULE的 JNDI。具体过程不详述了。
接下来看TOMCAT端调用EJB的代码。首先有一个很重要的问题要注意,如果要TOMCAT能够调用WEBSPHERE下的资源,则要将以下JAR包放入TOMCAT下的common/lib目录下:
引用
bootstrap.jar
ecutils.jar
emf.jar
ffdc.jar
idl.jar
iwsorb.jar
j2ee.jar
naming.jar
namingclient.jar
namingserver.jar
ras.jar
tx.jar
wsexception.jar
这些jar包位于websphere根目录下的AppServer/lib目录下。
然后创建web project。 这里务必注意,要将之前生成的ejb的jar包引入该web project下。
web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>testCallRemoteEJB</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<ejb-ref>
<description>
</description>
<!-- 此处为EJB JNDI -->
<ejb-ref-name>ejb/test/rmt/ejb/TestRemoteHome</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>test.rmt.ejb.TestRemoteHome</home>
<remote>test.rmt.ejb.TestRemote</remote>
</ejb-ref>
</web-app>
然后创建index.jsp,如下:
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
import="javax.naming.*,
java.util.Properties,
javax.ejb.*,
test.rmt.ejb.*"%>
<%!
publicInitialContext getInitialContext() throws Exception {
//EJB容器的地址, websphere 使用了rmi iiop协议来传输EJB, IP地址和端口请使用您自己的配置
String url = "iiop://localhost:2810";
String user = null;
String password = null;
Properties properties;
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
//com.ibm.websphere.naming.WsnInitialContextFactory 是websphere要求的naming 上下文环境,包含在
//之前放入tomcat下的jar包中
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new javax.naming.InitialContext(properties);
}
%>
<%
Context ctx = getInitialContext();
TestRemoteHome home = (TestRemoteHome)PortableRemoteObject.narrow(ctx.lookup("ejb/test/rmt/ejb/TestRemoteHome"),TestRemoteHome.class);
TestRemote remote = home.create();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%out.println("<h1>" + remote.returnString()+"<h1"); %>
</body>
</html>
之后便可以运行代码了。
注意:此实例是在websphere 6.0.25环境和tomcat 5.5.27环境下测试通过的。其他环境如无法通过,请参照相应文档修改。
页:
[1]