分享
 
 
 

WebLogic的研究之三--开发、部署EJB(1)

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

WebLogic的研究之三--开发、部署EJB(1)

作者:XYZ

这里不会讨论EJB的概念,只讨论如何编写一个简单EJB,部署EJB,Weblogic与JBuilder的整合,本文先把介绍仅用文本编辑器编写一个最简单的EJB所需要的一切,以让大家一览EJB的概貌,然后才介绍如何把Weblogic与JBuilder整合起来,使用JBuilder开发Weblogic的EJB,我相信这样会得到很好的学习效果,因为这是我学习的路径,当然我会把我遇到的问题告诉大家,以免大家走弯路。

下面是一个最简单的EJB所需要的代码及XML说明,手工制作EJB的JAR包比较麻烦,在WIN2000下,我仿照例子制作了一个 build.cmd 批处理文件

weblogic-ejb-jar.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN' 'http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd'>

<weblogic-ejb-jar>

<weblogic-enterprise-bean>

<ejb-name>HelloWorldBean</ejb-name>

<caching-descriptor>

<max-beans-in-free-pool>100</max-beans-in-free-pool>

</caching-descriptor>

<jndi-name>hello.HelloWorld</jndi-name>

</weblogic-enterprise-bean>

</weblogic-ejb-jar

ejb-jar.xml

<?xml version="1.0" encoding="GBK"?>

<!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>

<enterprise-beans>

<session>

<ejb-name>HelloWorldBean</ejb-name>

<home>hello.HelloWorldHome</home>

<remote>hello.HelloWorld</remote>

<ejb-class>hello.HelloWorldBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>

</session>

</enterprise-beans>

<assembly-descriptor>

<container-transaction>

<method>

<ejb-name>HelloWorldBean</ejb-name>

<method-name>*</method-name>

</method>

<trans-attribute>Required</trans-attribute>

</container-transaction>

</assembly-descriptor>

</ejb-jar>

package hello;

import java.rmi.*;

import javax.ejb.*;

public class HelloWorldBean implements SessionBean {

private SessionContext sessionContext;

public void ejbCreate() {

}

public void ejbRemove() {

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setSessionContext(SessionContext context) {

sessionContext = context;

}

public String getHelloWorld(){

return "Hello World!";

}

}

HelloWorld.java

package hello;

import java.rmi.*;

import javax.ejb.*;

public interface HelloWorld extends EJBObject {

public java.lang.String getHelloWorld() throws RemoteException;

}

HelloWorldHome.java

package hello;

import java.rmi.*;

import javax.ejb.*;

public interface HelloWorldHome extends EJBHome {

public HelloWorld create() throws RemoteException, CreateException;

}

HelloWorldBean.java

package hello;

import java.rmi.*;

import javax.ejb.*;

public class HelloWorldBean implements SessionBean {

private SessionContext sessionContext;

public void ejbCreate() {

}

public void ejbRemove() {

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setSessionContext(SessionContext context) {

sessionContext = context;

}

public String getHelloWorld(){

return "Hello World!";

}

}

HelloWorldBeanClient1.java

package hello;

import javax.naming.*;

import javax.rmi.PortableRemoteObject;

public class HelloWorldBeanClient1 {

private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";

private static final int MAX_OUTPUT_LINE_LENGTH = 50;

private boolean logging = true;

private HelloWorldHome helloWorldHome = null;

private HelloWorld helloWorld = null;

/**Construct the EJB test client*/

public HelloWorldBeanClient1() {

long startTime = 0;

if (logging) {

log("Initializing bean access.");

startTime = System.currentTimeMillis();

}

try {

//get naming context

Context ctx = new InitialContext();

//look up jndi name

Object ref = ctx.lookup("HelloWorld");

//cast to Home interface

helloWorldHome = (HelloWorldHome) PortableRemoteObject.narrow(ref, HelloWorldHome.class);

if (logging) {

long endTime = System.currentTimeMillis();

log("Succeeded initializing bean access.");

log("Execution time: " + (endTime - startTime) + " ms.");

}

HelloWorld hello=helloWorldHome.create();

String str=hello.getHelloWorld();

System.out.println(str);

}

catch(Exception e) {

if (logging) {

log("Failed initializing bean access.");

}

e.printStackTrace();

}

}

//----------------------------------------------------------------------------

// Methods that use Home interface methods to generate a Remote interface reference

//----------------------------------------------------------------------------

public HelloWorld create() {

long startTime = 0;

if (logging) {

log("Calling create()");

startTime = System.currentTimeMillis();

}

try {

helloWorld = helloWorldHome.create();

if (logging) {

long endTime = System.currentTimeMillis();

log("Succeeded: create()");

log("Execution time: " + (endTime - startTime) + " ms.");

}

}

catch(Exception e) {

if (logging) {

log("Failed: create()");

}

e.printStackTrace();

}

if (logging) {

log("Return value from create(): " + helloWorld + ".");

}

return helloWorld;

}

//----------------------------------------------------------------------------

// Methods that use Remote interface methods to access data through the bean

//----------------------------------------------------------------------------

public String getHelloWorld() {

String returnValue = "";

if (helloWorld == null) {

System.out.println("Error in getHelloWorld(): " + ERROR_NULL_REMOTE);

return returnValue;

}

long startTime = 0;

if (logging) {

log("Calling getHelloWorld()");

startTime = System.currentTimeMillis();

}

try {

returnValue = helloWorld.getHelloWorld();

if (logging) {

long endTime = System.currentTimeMillis();

log("Succeeded: getHelloWorld()");

log("Execution time: " + (endTime - startTime) + " ms.");

}

}

catch(Exception e) {

if (logging) {

log("Failed: getHelloWorld()");

}

e.printStackTrace();

}

if (logging) {

log("Return value from getHelloWorld(): " + returnValue + ".");

}

return returnValue;

}

//----------------------------------------------------------------------------

// Utility Methods

//----------------------------------------------------------------------------

private void log(String message) {

if (message.length() > MAX_OUTPUT_LINE_LENGTH) {

System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");

}

else {

System.out.println("-- " + message);

}

}

/**Main method*/

public static void main(String[] args) {

HelloWorldBeanClient1 client = new HelloWorldBeanClient1();

// Use the client object to call one of the Home interface wrappers

// above, to create a Remote interface reference to the bean.

// If the return value is of the Remote interface type, you can use it

// to access the remote interface methods. You can also just use the

// client object to call the Remote interface wrappers.

}

}

build.cmd

if "" == "%JAVA_HOME%" set JAVA_HOME=\java

if "" == "%WL_HOME%" set WL_HOME=\weblogic

set MYSERVER=%WL_HOME%\myserver

set MYCLASSPATH=%JAVA_HOME%\lib\classes.zip;%WL_HOME%\classes;%WL_HOME%\lib\weblogicaux.jar;%MYSERVER%\clientclasses

@REM Create the build directory, and copy the deployment descriptors into it

mkdir build build\META-INF

copy *.xml build\META-INF

@REM Compile ejb classes into the build directory (jar preparation)

javac -d build -classpath %MYCLASSPATH% HelloWorld.java HelloWorldHome.java HelloWorldBean.java

@REM Make a standard ejb jar file, including XML deployment descriptors

cd build

jar cv0f std_ejb_HelloWorld.jar META-INF hello

cd ..

@REM Run ejbc to create the deployable jar file

java -classpath %MYCLASSPATH% -Dweblogic.home=%WL_HOME% weblogic.ejbc -compiler javac build\std_ejb_HelloWorld.jar %MYSERVER%\ejb_Hello.jar

@REM Compile ejb interfaces & client app into the clientclasses directory

javac -d %MYSERVER%\clientclasses -classpath %MYCLASSPATH% HelloWorld.java HelloWorldHome.java HelloWorldBeanClient1.java

作者:XYZ※版权所有

转载请注明来源

【CGI编程设计室】

http://zjxyz.yeah.net

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有