欢迎光临
我们一直在努力

EJB核心技术及其应用(再续)-JSP教程,J2EE/EJB/服务器

建站超值云服务器,限时71元/月

五、ejb的编程环境:

1、 使用jbuilder
  jbuilder与ejb container能够进行无缝连接。jbuilder和inprise的应用服务器包括了所有的开发和配置enterprise beans的工具以及所需要的库:运行和管理enterprise bean的容器、命名服务、 事务服务、java数据库、开发enterprise beans所需要的api、一个增强的java-to-iiop编译器,支持值类型和rmi信号等等。
  jbuilder还提供了一个快速开发应用程序enterprise beans的工具和向导。通过简单而且直观的步骤,向导帮助你建立一个enterprise bean。自己设定某些缺省值,产生了bean的模板,在上面,我们可以增加我们自己的应用逻辑。jbuilder也提供了一个ejb的接口生成向导。向导在enterprise bean的公共方法基础上生成了remote接口和home接口。jbuilder还提供一个配置器的向导帮助我们逐步的建立xml描述器文件。并将生成的stubs集中到一个jar文件中。

2、使用jbuilder之外的集成环境:
  如果你使用其它的除了别的集成环境(ide)。要确定使用了集成环境ide所带的容器工具。也要验证ide是否支持ejb规范的相应的版本,还要确定它是否正确的支持ejb的api。
  要确定jd到所支持的ejb容器的版本。可以通过检查inprise的安装说明来确定ejb容器所支持的支持jdk的版本。
  在配置enterprise bean的时候,你必须使用inprise的应用服务器所提供的工具。这些工具能够编辑和修改第三方的代理商提供的inprise配置描述器。还能够验证配置描述器,能够验证bean的源代码。

六、一个简单的hello例子

1、安装apusic application server
  note:以下以linux为例,来说明apusic application server的安装过程。其他平台的安装,可参考apusic application server安装手册。
  下载jdk1.2,apusic application server必须运行在jdk1.2以上环境中。可从以下站点下载最新jdk。
http://java.sun.com
  下载apusic application server
apusic application server 试用版可从以下网址得到:
  http://www.apusic.com/download/enter.jsp
  在下载完成后,你可以得到一个包裹文件apusic.zip,选定安装目录,假设安装到/usr下,则用以下命令:
cd /usr
jar xvf apusic.zip
/usr下会出现一个目录apusic,apusic application server的所有程序都被解压到/usr/apusic下。
将以下路径加入到classpath中
/usr/apusic/lib/apusic.jar
$java_home/lib/tools.jar
用以下命令运行apusic application server
java -xms64m com.apusic.server.main -root /usr/apusic

2、定义ejb远程接口(remote interface)
  任何一个ejb都是通过remote interface被调用,ejb开发者首先要在remote interface中定义这个ejb可以被外界调用的所有方法。执行remote interface的类由ejb生成工具生成。
  以下是hellobean的remote inteface程序:
package ejb.hello;

import java.rmi.remoteexception;
import java.rmi.remote;
import javax.ejb.*;

public interface hello extends ejbobject, remote {

//this method just get "hello world" from hellobean.
public string gethello() throws remoteexception;
}

3、定义home interface
  ejb容器通过ejb的home interface来创建ejb实例,和remote interface一样,执行home interface的类由ejb生成工具生成。
以下是hellobean 的home interface程序:
package ejb.hello;

import javax.ejb.*;
import java.rmi.remote;
import java.rmi.remoteexception;
import java.util.*;

/**
* this interface is extremely simple it declares only
* one create method.
*/
public interface hellohome extends ejbhome {

public hello create() throws createexception,
remoteexception;

}

4、写ejb类
  在ejb类中,编程者必须给出在remote interface中定义的远程方法的具体实现。ejb类中还包括一些 ejb规范中定义的必须实现的方法,这些方法都有比较统一的实现模版,编程者只需花费精力在具体业务方法的实现上。
以下是hellobean的代码:
package ejb.hello;

import javax.ejb.*;
import java.util.*;
import java.rmi.*;

public class hellobean implements sessionbean {
static final boolean verbose = true;

private transient sessioncontext ctx;

// implement the methods in the sessionbean
// interface
public void ejbactivate() {
if (verbose)
system.out.println("ejbactivate called");
}

public void ejbremove() {
if (verbose)
system.out.println("ejbremove called");
}

public void ejbpassivate() {
if (verbose)
system.out.println("ejbpassivate called");
}

/**
* sets the session context.
*
* @param sessioncontext
*/
public void setsessioncontext(sessioncontext ctx) {
if (verbose)
system.out.println("setsessioncontext called");
this.ctx = ctx;
}

/**
* this method corresponds to the create method in
* the home interface hellohome.java.
* the parameter sets of the two methods are
* identical. when the client calls
* hellohome.create(), the container allocates an
* instance of the ejbean and calls ejbcreate().
*/
public void ejbcreate () {
if (verbose)
system.out.println("ejbcreate called");
}
/**
* **** here is the business logic *****
* the gethello just return a "hello world" string.
*/
public string gethello()
throws remoteexception
{
return("hello world");
}
}

5、创建ejb-jar.xml文件
  ejb-jar.xml文件是ejb的部署描述文件,包含ejb的各种配置信息,如是有状态bean(stateful bean) 还是无状态bean(stateless bean),交易类型等。ejb-jar.xml文件的详细信息请参阅ejb规范。以下是hellobean的配置文件:
<?xml version="1.0"?>
<!doctype ejb-jar public "-//sun microsystems inc.//dtd enterprise javabeans 1.2//en" "http://java.sun.com/j2ee/dtds/ejb-jar_1_2.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>hello</ejb-name>
<home>ejb.hello.hellohome</home>
<remote>ejb.hello.hello</remote>
<ejb-class>ejb.hello.hellobean</ejb-class>
<session-type>stateless</session-type>
<transaction-type>container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>hello</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>

6、编译和部署
编译java源文件并将编译后class和ejb-jar.xml打包到hello.jar
mkdir build
mkdir build/meta-inf
cp ejb-jar.xml build/meta-inf
javac -d build *.java
cd build
jar cvf hello.jar meta-inf ejb
cd ..
用ejb工具生成可部署到apusic application server中运行的jar文件:
java com.apusic.ejb.utils.ejbgen -d /usr/apusic/classes/hello.jar build/hello.jar
增加/usr/apusic/classes/hello.jar到classpath中
将hello.jar加入到apusic application server配置文件中。在/usr/apusic/config/server.xml 加入以下几行:
<module>
<ejb>
<ejb-uri>classes/hello.jar</ejb-uri>
<bean>
<ejb-name>hello</ejb-name>
<jndi-name>hellohome</jndi-name>
</bean>
</ejb>
</module>
启动服务器
java -xms64m com.apusic.server.main -root /usr/apusic

7、写客户端调用程序
  您可以从java client,jsp,servlet或别的ejb调用hellobean。
  调用ejb有以下几个步骤:
  通过jndi(java naming directory interface)得到ejb home interface
  通过ejb home interface 创建ejb对象,并得到其remote interface
  通过remote interface调用ejb方法

以下是一个从java client中调用hellobean的例子:
package ejb.hello;

import javax.naming.context;
import javax.naming.initialcontext;
import java.util.hashtable;
import javax.ejb.*;
import java.rmi.remoteexception;

/**
* @author copyright (c) 2000 by apusic, inc. all rights reserved.
*/
public class helloclient{
public static void main(string args[]){
string url = "rmi://localhost:6888";
context initctx = null;
hellohome hellohome = null;
try{
hashtable env = new hashtable();
env.put(context.initial_context_factory,
"com.apusic.jndi.initialcontextfactory");
env.put(context.provider_url, url);
initctx = new initialcontext(env);
}catch(exception e){
system.out.println("cannot get initial context: " + e.getmessage());
system.exit(1);
}
try{
hellohome = (hellohome)initctx.lookup("hellohome");
hello hello = hellohome.create();
string s = hello.gethello();
system.out.println(s);
}catch(exception e){
system.out.println(e.getmessage());
system.exit(1);
}
}

}
运行helloclient,可得到以下输出:
hello world

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » EJB核心技术及其应用(再续)-JSP教程,J2EE/EJB/服务器
分享到: 更多 (0)