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服务器地址.

 

转载于:https://www.cnblogs.com/ajuanabc/archive/2008/08/07/2462972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值