好久沒寫blog了,最近在考慮openjweb遷移到j(luò)boss的問題,經(jīng)過試驗,發(fā)現(xiàn)tomcat與jboss結(jié)合是挺簡單的,tomcat中如何連接jboss,其實只需要把jboss的client目錄的jar包復(fù)制到tomcat的common/lib中即可,我是復(fù)制的jboss開頭的jar包,開發(fā)的EJB除了在jboss中部署外,ejb的home和接口類還要放到tomcat的web應(yīng)用的WEB-INF/classes對應(yīng)路徑下,下面是tomcat web應(yīng)用中一個ejb調(diào)用例子(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"); //調(diào)用本地的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包放到j(luò)boss的server/default/deploy中,jar包中除了有com為根目錄的類外,在META-INF目錄中還要放置上面兩個xml文件.
?
其實tomcat的web應(yīng)用中調(diào)用ejb的關(guān)鍵就是tomcat中要放置ejb客戶端jar包以及在jndi調(diào)用中指定ejb服務(wù)器地址.
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
