ienki 发表于 2017-1-23 10:41:33

tomcat与jboss集成

  好久没写blog了,最近在考虑openjweb迁移到jboss的问题,经过试验,发现tomcat与jboss结合是挺简单的,tomcat中如何连接jboss,其实只需要把jboss的client目录的jar包复制到tomcat的common\lib中即可,我是复制的jboss开头的jar包,开发的EJB除了在jboss中部署外,ejb的home和接口类还要放到tomcat的web应用的WEB-INF/classes对应路径下,下面是tomcat web应用中一个ejb调用例子(testejb.jsp):
  <%@page import="com.test.*,java.util.*" %>

<%
response.setContentType("text/html; charset=UTF-8");
%>
<%!
com.test.TestEJBHome testHome= null;
com.test.TestEJB test= null;


public void jspInit() {
try{
javax.naming.InitialContext ctx = getInitialContext();
Object objref = null;
if(testHome == null)
objref = ctx.lookup("test111"); //ejb的名称
testHome=(com.test.TestEJBHome)javax.rmi.PortableRemoteObject.narrow(objref, com.test.TestEJBHome.class);
test = testHome.create();
String s = test.getValue("abao");

System.out.println(test.getValue("abao")); //测试成功后输出hekki,abao
  

  }catch(Exception e){ e.printStackTrace(); }
}
  public javax.naming.InitialContext getInitialContext() throws javax.naming.NamingException{
System.out.println("init jndi be called....");
java.util.Properties props = new java.util.Properties();
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.put("java.naming.provider.url","localhost:1099"); //调用本地的jboss
return new javax.naming.InitialContext(props);
}
  


%>
  
<%


out.println("hello!");

%>
  下面是测试ejb的几个类:
  //TestEJB接口
  package com.test;
  public interface TestEJB extends javax.ejb.EJBObject
{

public String getValue(String name) throws java.rmi.RemoteException, Exception;

}

  //session bean
  package com.test;
  import java.rmi.RemoteException;
  import javax.ejb.EJBException;
import javax.ejb.SessionContext;
  public class TestEJBBean implements javax.ejb.SessionBean {
  public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}
  public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}
  public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}
  public void setSessionContext(SessionContext arg0) throws EJBException,
RemoteException {
// TODO Auto-generated method stub

}

public String getValue(String name) throws java.rmi.RemoteException, Exception
{
return "hello,"+name;
}


public void ejbCreate(){
System.out.println("ejb create");
//try{
//ctx = new javax.naming.InitialContext();
//}catch(Exception e){
// e.printStackTrace();
//}
}
  }

  //home接口:
  package com.test;
  public interface TestEJBHome extends javax.ejb.EJBHome{
public TestEJB create() throws javax.ejb.CreateException, java.rmi.RemoteException, Exception;
  }
  //部署文件ejb-jar.xml:
  <?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<small-icon>images/green-cube.gif</small-icon>
<enterprise-beans>
<session>
<small-icon>images/orange-cube.gif</small-icon>
<ejb-name>test111</ejb-name>
<home>com.test.TestEJBHome</home>
<remote>com.test.TestEJB</remote>
<ejb-class>com.test.TestEJBBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>test111</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Supports</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

  //jboss.xml:
  <?xml version="1.0" encoding="euc-kr"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>test111</ejb-name>
<jndi-name>test111</jndi-name>
</session>
</enterprise-beans>
<resource-managers>
<!---->
</resource-managers>
</jboss>
  部署时要打成ejb的jar包放到jboss的server\default\deploy中,jar包中除了有com为根目录的类外,在META-INF目录中还要放置上面两个xml文件.
  其实tomcat的web应用中调用ejb的关键就是tomcat中要放置ejb客户端jar包以及在jndi调用中指定ejb服务器地址.
页: [1]
查看完整版本: tomcat与jboss集成