快捷搜索:

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

HelloWorldBean

100

hello.HelloWorld

HelloWorldBean

hello.HelloWorldHome

hello.HelloWorld

hello.HelloWorldBean

Stateless

Container

HelloWorldBean

*

Required

--------------------------------------------------------------------------------

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%libclasses.zip;%WL_HOME%classes;%WL_HOME%libweblogicaux.jar;%MYSERVER%clientclasses

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

mkdir build buildMETA-INF

copy *.xml buildMETA-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 buildstd_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

您可能还会对下面的文章感兴趣: