1.jboss简介
jboss是一个运行ejb的j2ee应用服务器。它是开放源代码的项目,遵循最新的j2ee规范。从jboss项目开始至今,它已经从一个ejb容器发展成为一个基于的j2ee的一个web 操作系统(operating system for web),它体现了j2ee规范中最新的技术,并且它还在the javaworld editors' choice 2002评选中获得“最佳java应用服务器”大奖。无论是学习还是应用,jboss为我们提供了一个非常优秀的平台。有关jboss的详细信息请参阅其主页http://www.jboss.org。
刚开始使用jboss进行ejb开发时,由于可供参考的资源不是很多,所以有一个比较困难的起步阶段。jboss的配置和使用没有提供图形向导界面,所以开发部署ejb相对比较复杂。本文通过尽量具体的演示来对jboss3.0下的ejb开发和部署的进行一个简单的介绍,从而使刚开始使用jboss的用户可以很快地进入到真正的j2ee应用开发中。
由于本文主要介绍jboss3.0中不同类型ejb的配置和部署,不对基本的ejb开发做太多的描述,所以希望读者具有j2ee和ejb的经验。想要了解有关信息请查阅参考资料1。
2.jboss3.0中基本的ejb配置和部署
根据j2ee规范的要求,一个基本的ejb jar包使用的描述文件是ejb-jar.xml。web应用的war包使用的是web.xml。企业应用的ear包使用的是application.xml。这些配置文件都是中性的和平台无关的。同时应用服务器可以使用一些其他的配置文件用于描述特定服务器的相关信息。在jboss中这样的文件有jboss.xml,jboss-web.xml等。jboss容器中这些文件不是必须的,如果提供了那么jboss.xml和ejb-jar.xml放在同一目录下,jboss-web.xml和web.xml放在同一目录下。关于jboss.xml和jboss-web.xml的规范请参考jboss安装目录下docs/dtd/目录下的对应的dtd文件。
2.1 jboss中关于ejb客户端的配置:
调用ejb的客户端可以是jsp、servlet或客户端应用程序。如果客户端和服务器不在同一个java vm上,那么在客户端必须提供一个jndi.properties文件告诉客户端进行有关jndi命名服务的信息,并且把这个文件所在目录设定到环境变量classpath中。
以下是一个jndi.properties的样例:
java.naming.factory.initial=org.jnp.interfaces.namingcontextfactory
java.naming.provider.url=localhost:1099 (服务器地址和端口号)
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
通过这个文件和jboss的一些客户端类库,就可以使用jboss提供的ejb对象服务了。
2.2 jboss中部署ejb:
首先介绍jboss容器中部署各类ejb对象的两种方法。下面的演示以session bean为例,客户端是一个本机上的web应用。这里不对演示程序进行具体介绍。
第一种方式:
把开发好的ejb jar包和web应用的war包公共放在deploy目录下。
演示1:不需要jboss.xml文件和其他任何特殊的设置。
ejb-jar.xml:
<ejb-jar>
······
<ejb-name>ejbtest</ejb-name>
······
</ejb-jar>
······
initialcontext ctx = new initialcontext();
object objref = ctx.lookup("ejbtest"); //使用<ejb-name> beanhome=(zcxejb1home)portableremoteobject.narrow(objref,ejbtesthome.class);
······
<ejb-jar>
······
<ejb-name>ejbtest</ejb-name>
······
</ejb-jar>
<jboss>
<ejb-name>ejbtest</ejb-name>
<jndi-name>example/ejbtest</jndi-name>
</jboss>
······
initialcontext ctx = new initialcontext();
object objref = ctx.lookup("example/ejbtest"); //使用<jndi-name> beanhome=(zcxejb1home)portableremoteobject.narrow(objref,ejbtesthome.class);
······
······
<ejb-ref>
<ejb-ref-name>ejb/ejbtest</ejb-ref-name> <!--采用sun推荐的命名方式-->
<ejb-ref-type>session</ejb-ref-type>
<home>org.zcx.test.zcxejb1home</home>
<remote>org.zcx.test.zcxejb1</remote>
<ejb-link>ejbtest</ejb-link> <!--必须和被应用的ejb-name匹配-->
</ejb-ref>
······
······
initialcontext ctx = new initialcontext();
object objref = ctx.lookup("java:comp/env/ ejb/ejbtest "); //使用java:comp/env命名空间 beanhome=(zcxejb1home)portableremoteobject.narrow(objref,ejbtesthome.class);
······
······
<ejb-ref>
<ejb-ref-name>ejb/ejbtest</ejb-ref-name> <!--采用sun推荐的命名方式-->
<ejb-ref-type>session</ejb-ref-type>
<home>org.zcx.test.zcxejb1home</home>
<remote>org.zcx.test.zcxejb1</remote>
</ejb-ref>
······
<ejb-ref>
<ejb-ref-name> ejb/ejbtest </ejb-ref-name>
<jndi-name> example/ejbtest </jndi-name> <!-- 这里对应ejb对象的jndi名-->
</ejb-ref>
nitialcontext ctx = new initialcontext();
object objref = ctx.lookup("java:comp/env/ ejb/ejbtest "); beanhome=(zcxejb1home)portableremoteobject.narrow(objref,ejbtesthome.class);
<message-driven>
<ejb-name>hellotopicmdb</ejb-name>
<ejb-class>org.zcx.test.hellomdb</ejb-class>
<message-selector></message-selector>
<transaction-type>container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.topic</destination-type>
<subscription-durability>nondurable</subscription-durability>
</message-driven-destination>
</message-driven>
<message-driven>
<ejb-name>hellotopicmdb</ejb-name>
<configuration-name>standard message driven bean</configuration-name>
<destination-jndi-name>topic/myapptesttopic</destination-jndi-name>
</message-driven>
······
context context = new initialcontext();
// get the connection factory
// create the connection
// create the session
······
// look up the destination
topic = (topic)context.lookup("topic/mytestapptopic");
// create a publisher
// publish the message
······
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器