Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > WebLogic的研究之开发、部署EJB(1)
【标  题】:WebLogic的研究之开发、部署EJB(1)
【关键字】:c,Web,EJB,WebLogic,Log,WebLogic,EJB
【来  源】:网络

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

Your Ad Here WebLogic的研究之开发、部署EJB(1)

这里不会讨论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

(未完待续)
WebLogic的研究之开发、部署EJB(2):【上一篇】
控制bean定制器的技巧(2):【下一篇】
【相关文章】
  • EJB(Enterprise JavaBeans)入门(8)
  • EJB(Enterprise JavaBeans)入门(9)
  • EJB(Enterprise JavaBeans)入门(10)
  • 全面研读EJB 2.0(1)
  • 全面研读EJB 2.0(2)
  • 全面研读EJB 2.0(3)
  • javabean与ejb的区别
  • J2EE 组件开发:会话EJB
  • EJB 异常处理的最佳做法(1)
  • EJB 异常处理的最佳做法(2)
  • 【随机文章】
  • SCO UNIX 5.0.7
  • ASP.NET技巧:为Blog打造个性日历
  • 为国产瘦型计算机叫好
  • Myfaces 系列大发布
  • 然后可以用前面介绍的方法
  • SQL Server XML 和 Web 应用体系结构(二)
  • 求VMware最新版和注册码!
  • 第十五章 高级进程间通信
  • shawl.qiu Javascript 语法高亮函数 v1.0
  • 惠普收购安全公司Trustgenix
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.