1 OpenEJB概述
Tomcat本不支持部署EJB,通过向其安装OpenEjb,可使其支持。
2 安装
2.1 下载
http://www.apache.org/dyn/closer.cgi/openejb/3.1.3/openejb.war
2.2 安装
1、将下载的openejb.war 放在Tomcat的安装目录 webapps下。
2、启动Tomcat。
3、在IE中输入:http://localhost:8080/openejb 回车后显示如下信息:
Welcome to the OpenEJB/Tomcat integration!
Now that OpenEJB has been installed, click on the "Testing your setup" link below to verify it. When everything is setup well, feel free to play around with the tools provided below!
OK!安装成功,就这么简单!
4、测试一下:http://localhost:8080/openejb/viewjndi.jsp
3 部署
像往常一样,开发一个Ejb工程。
接口:
@Remote
public interface GreeterRemote
{
public String greet(String message);
public List<Greeting> getAllGreetings();
}
实现类:
@Stateless
public class GreeterBean implements GreeterRemote,GreeterLocal{
public List<Greeting> getAllGreetings(){
List<Greeting> greetings = new ArrayList<Greeting>();
Greeting greeting = new Greeting();
greeting.setId(12);
greeting.setName("bill gates");
greetings.add(greeting);
greeting = new Greeting();
greeting.setId(334);
greeting.setName("李宁");
greetings.add(greeting);
return greetings;
}
public String greet(String message){
return "您好"+ message;
}
}
将此EJB工程打成jar包,比如放在D:\Tomcat\ejb下。注:“D:\Tomcat\ejb”是我自己建的。
然后打开:Tomcat\conf\openejb.xml,将</openejb>前的内容改为:
<!--
#
# The <Deployments> element can be used to configure file
# paths where OpenEJB should look for ejb jars or ear files.
#
# See http://openejb.apache.org/deployments.html
#
# The below entry is simply a default and can be changed or deleted
<Deployments dir="apps/" />原来是这句,我们用不到,可以用下面的语句直接加载指定的ejb jar包。
-->
<Deployments jar="D:/Tomcat/ejb/OpenEjbTest.jar" />
重启Tomcat,在浏览器输入:http://127.0.0.1:8089/openejb/invokeobj.jsp
点击其中的”Browse for an EJB”,转到如下界面:
如果在其中能看到自己的EJB Bean,那就成功了。
4 客户端调用
按照官方给出的说明:http://openejb.apache.org/3.0/clients.html,此处使用“Remote Client with HTTP (in tomcat)”方式。对上面部署的EJB调用的客户端代码如下:
public class GreeterBeanTest{
public static void main(String[] args) throws NamingException {
Properties p = new Properties();
p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://localhost:8089/openejb/ejb");
InitialContext initialContext = new InitialContext(p);
GreeterRemote greeterRemote =(GreeterRemote) initialContext.lookup("GreeterBeanRemote");
String str="屈剑峰";
System.out.println(greeterRemote.greet(str));
}
}