注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
让Ghostbusters为电脑保驾护航
走近 WSH(Windows Scripting Hos
预防DDoS攻击的十项安全策略
揭开DDoS攻防的神秘面纱
DDos攻击的原理及对策
DDoS攻防演示 [图]
防火墙防止DDOS分布式拒绝服务攻
应对DoS/DDoS攻击的十条军规
解密DDoS攻击——“缓存溢出”新
Microsoft AntiSpyware微软出品的
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
JBoss技术支持文档
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:
下载例子源程序

  所有例子的源代码可以直接从 www.jboss.org进行下载。下载完后放在一个目录下。下载网址:http://www.jboss.org/docs/manual/files/documentation-example.zip

  1.1 建立 BEAN

  此节主要是建立一个简单的EJB,可以查看代码,这个“Interest”例子,是一个简单无状态的会话EJB。它的目的是根据说明的利息率,来对借的所有钱计算利息。实际上在整个包代码中只有一行功能。
  1.2 回顾EJBs

  在我们查看代码之前,我们先对EJB进行复习一下。在EJB最小类型,也必须有三个类:remote interface, home interface和bean实现类。

  remote interface是会把EJB中方法提供给外边世界,让外边的代码来进行调用,在这个例子中类名称是org.jboss.interest.Interrest。

  home interface是管理remote interface类的类。包括建立、删除等操作。在这个例子中类名称是org.jboss.interest.InterrestHome。

  bean实现类提供home interface和remote interface所有方法的实现。在这个例子中类名称是org.jboss.interest.InterrestBean。

  当然一个Bean可能还包括其他类,甚至其他包。但是必须有此三个类,其他类是在此三个类之上建立的。所有类被打包进一个JAR文件,此文件是用一个目录结构来反映出包的层次关系。在此例子中所有类都打包在org.jboss.interest包中,所以他们需要在目录org/jboss/interest/下。

  在包含所有类的jar文件建立之前,必须有一个META-INF目录。此目录存放了部署描述符(通常叫“ejb-jar.xml”),和可选的其他XML文件。这些文件告诉服务器关于应用明确服务信息。对于JBoss 来讲,文件名必须叫“jboss.xml”。

  创建jar文件后部署到JBoss Server上。在客户端你需要一个jndi.properties文件,此文件告诉你的客户端程序从哪里初始化查找JNDI 命名服务。从这个服务,客户端将查找到Interest bean,并且返回bean的home interface。home interface用来得到bean的一个remote interface。它可以用远程接口来访问bean提供的商业方法。

  1.3 EJB类

  我们需要三个类:remote interface, home interface 和bean实现类。remote interface远程接口类,文件名Interest.java。代码如下:

package org.jboss.docs.interest;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
/**
This interface defines the `Remote' interface for the `Interest' EJB. Its
single method is the only method exposed to the outside world. The class
InterestBean implements the method.
*/
public interface Interest extends EJBObject
{
/** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */

public double calculateCompoundInterest(double principle, double rate, double periods) throws RemoteException;

}
  远程接口只有一个商业方法calculateCompoundInterest。Home interface 文件名InterestHome.java。代码如下:

package org.jboss.docs.interest;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

/**
This interface defines the 'home' interface for the 'Interest' EJB.
*/
public interface InterestHome extends EJBHome
{
/** Creates an instance of the `InterestBean' class on the server, and returns a remote reference to an Interest interface on the client. */
Interest create() throws RemoteException, CreateException;
}
  最后我们给出bean实现类,文件名称:InterestBean.java。代码如下:

package org.jboss.docs.interest;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
This class contains the implementation for the 'calculateCompoundInterest' method exposed by this Bean. It includes empty method bodies for the methods prescribe by the SessionBean interface; these don't need to do anything in this simple example. */public class InterestBean implements SessionBean
{
/** Calulates the compound interest on the sum `principle', with interest rate per period `rate' over `periods' time periods. This method also prints a message to standard output; this is picked up by the EJB server and logged. In this way we can demonstrate that the method is actually being executed on the server, rather than the client. */
public double calculateCompoundInterest(double principle, double rate, double periods)
{
System.out.println("Someone called `calculateCompoundInterest!'");
return principle * Math.pow(1+rate, periods) - principle;
}
/** Empty method body */
public void ejbCreate() {}
/** Every ejbCreate() method ALWAYS needs a corresponding ejbPostCreate() method with exactly the same parameter types. */
public void ejbPostCreate() {}
/** Empty method body */
public void ejbRemove() {} /** Empty method body */
public void ejbActivate() {} /** Empty method body */
public void ejbPassivate() {} /** Empty method body */
public void setSessionContext(SessionContext sc) {}

}
  注意大部分方法是空的。因为这些方法在SessionBean接口中说明,所以必须在InterestBean中存在,但在这个简单例子中,不需要具体内容。

  2、部署描述符

  当你编辑完类文件后,我们来看部署描述符。此文件告诉EJB Server是哪个类应该被唤醒bean、Home Interface 和remote Interface。如果一个包中有不止一个bean,它指明ejb同另外bean如何相和。在这
个简单例子中只有一个ejb,所以我们不用关心这方面问题。

  大部分商业EJB Server都提供图形化部署工具,来构造部署描述符,JBoss没有XML编辑器,但是它能够简单手工构造部署描述符。下面是Interest Bean部署描述符:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>JBoss Interest Sample Application</description>
<display-name>Interest EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<home>org.jboss.docs.interest.InterestHome</home>
<remote>org.jboss.docs.interest.Interest</remote>
<ejb-class>org.jboss.docs.interest.InterestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
  部署描述符文件必须命名为“ejb-jar.xml”,并且必须放在目录META-INF下。一个大家容易犯的错误是目录名字,有的写成“META_INF”, “META~INF”或“meta-inf”所有这些都不能正常的工作。

  我们在server上部署一个应用程序,而不是一个ejb。在这个例子中我们的应用程序只有一个ejb。在部署描述符中<ejb-name>Interest</ejb-name>表明ejb的名称。JBoss缺省是把bean的home interface作为JNDI命名空间放在<ejb-name>中,除非你修改缺省值。实际上客户端应用程序不是必须使用这个名字的。开发者不用为JNDI命名空间指明一个不同的名字而费心。然而一个由完整应用程序的产品包含多个beans,通常用一个和开发者说明不同的名字。所以会发现“[application name]/[bean name]”等,关于这部分我们以后介绍。

  尽管部署描述符格式的文件ejb-jar.xml对所有ejb Server都是共有的。你可以从sun得到一个正确定义的DTD, 但是没有为一个特殊EJB Server指明每个必须要求。也没有说明如何从ejb-name影射到部署的JNDI名字,例如“[application name]/[bean name]”。通过JBoss提供一个缺省行为,来进行工作。这些工作来自于ejb-jar.xml。在高级配置的案例中,你必须定义JBoss说明行为, 此行为用jboss.xml部署描述符。在高级配置中我们会谈到jboss.xml描述符的详细信息的。

  这里我们仅配置jboss.xml描述符的JNDI名字来用来访问Interest EJB home inrterface。通过缺省的JNDI名字用来访问EJB home inrterface, 需要同ejb-jar.xml的ejb-name相同。对于这个简单的例子,Interest bean通过JNDI初始化上下文(作为“Interest”)来定位。 我们想用“interest/Interest”使home interface 可以使用,所以我们必须用jboss.xml描述符来说明。在jboss.xml中,重新覆盖缺省的JNDI命名。为了重新编写缺省的行为,我们用ejb-jar.xml中的ejb-name元素值作为bean home接口的JNDI命名,必须说明jndi命名,来写jboss.xml描述符如下:

<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<jndi-name>interest/Interest</jndi-name>
</session>
</enterprise-beans>
</jboss>
  这个文件说明调用的Interest BEAN被绑定在interest/Interest的JNDI名称下。我们建立的标准ejb-jar.xml部署描述符同JBoss说明jboss.xml共同设置Interest EJB home接口的JNDI名称为“interest/Interest”。我们现在有了EJB类,这些类是建立ejb 文件包必须的文件。


  3、打包和部署BEAN

  jar包是建立一个JAR文件,该文件包含EJB类文件和部署描述符。在下载的例子可能和你设置环
境不同,所以需要修改examples\build\build.xml的内容。关于各个ant方面的内容,我会在Ant文档中介绍给大家。关于如何修改,下面我一步步地来告诉大家。以下是windows平台的。至于Unix/Lunix平台基本相似,这里就不在叙述了。

  1) 在修改之前,比首先设定JBOSS_DIST环境变量,指定到你安装的Jboss的目录。我安装在C:\jboss-3.0.6_tomcat-4.1.18, 所以 JBOSS_DIST设定为C:\jboss-3.0.6_tomcat-4.1.18。

  2) 你是否安装了tomcat或jetty等web服务器。如果你没有安装,建议你安装jboss-3.0.6_tomcat-4.1.18, JBoss和tomcat直接结合在一起啦,不需要再单独安装,当然也可以安装jboss-jetty( JBoss和jetty直接结合在一起)。取决你喜欢什么web服务器。

  3) 完成上面两步后,我们来讨论修改examples\build\build.xml文件。在原有的文件中的部分代码:

<target name="validate-servlet">
<!-- Override with your web server servlet jar location.
The default assumes that JBOSS_DIST points to a
JBoss/Tomcat bundle distribution
-->
<available property="servlet.jar" value="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar" file="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar"/>
<available property="servlet.jar" value="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar" file="${env.JBOSS_DIST}/../jetty/lib/javax.servlet.jar"/>
<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar"/>
<available property="servlet.jar" value="${env.JBOSS_DIST}/../catalina/common/lib/servlet.jar" file="${env.TOMCAT_HOME}/lib/servlet.jar"/>
<property name="servlet.jar" value="COULD_NOT_FIND_SERVLET_JAR"/>
<path id="base.path_22">
<pathelement location="${jboss.dist}/client/ejb.jar"/>
<pathelement location="${jboss.dist}/client/jaas.jar"/>
<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>
<pathelement location="${jboss.dist}/client/jboss-client.jar"/>
<pathelement location="${jboss.dist}/client/jnp-client.jar"/>
<pathelement location="${servlet.jar}"/>
</path>
<path id="base.path_24">
<pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/>
<pathelement location="${jboss.dist}/client/jaas.jar"/>
<pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>
<pathelement location="${jboss.dist}/client/jboss-client.jar"/>
<pathelement location="${jboss.dist}/client/jnp-client.jar"/>
<pathelement location="${servlet.jar}"/>
</path>
</target>
<target name="validate-jboss" depends="validate-servlet">
<available property="classpath_id" value="base.path_22" file="${jboss.dist}/client/ejb.jar" />
<available property="classpath_id" value="base.path_24" file="${jboss.dist}/client/jboss-j2ee.jar" />
</target>

  由于此代码例子是jboss2.2中的所以对于我们不能适应,在此基础上我们来进行改动。在改动之前,来说明一下为什么改动,让大家清楚改动原因,不是一团雾水。

<available property="servlet.jar" value="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar" file="${env.JBOSS_DIST}/../tomcat/lib/servlet.jar"/>….

  此部分说明你运用的web服务器是什么,根据你使用的web服务器来设置servlet.jar。 由于我使用的是jboss-tomcat.。

<path id="base.path_24"> … </path>说明客户端要的CLASSPATH包。

  修改以后的部分文件:

<target name="validate-servlet">
<!-- Override with your web server servlet jar location.
The default assumes that JBOSS_DIST points to a
JBoss/Tomcat bundle distribution

[1] [2] 下一页






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务