转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> JAVA开发 >> 正文
JBoss技术支持文档(二)         ★★★★

JBoss技术支持文档(二)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:653 更新时间:2009/4/22 23:27:50


  1、下载例子源程序

  所有例子的源代码可以直接从 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。代码如下:


  远程接口只有一个商业方法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中存在,但在这个简单例子中,不需要具体内容。


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台