Research on WebLogic-development and deployment of EJB (1)

Source: Internet
Author: User

We will not discuss the concept of EJB here. We will only discuss how to compile a simple EJB, deploy EJB, and integrate Weblogic with JBuilder, this article describes how to integrate Weblogic and JBuilder to compile the most simple EJB in a text editor, using JBuilder to develop Weblogic ejbs, I believe this will produce good learning results, because this is my learning path. Of course, I will tell you my problems to avoid detours.

The following is the code and XML description required by the simplest EJB. It is troublesome to manually create an ejb jar package. In WIN2000, I created a build. cmd batch processing file in the following example.

Weblogic-ejb-jar.xml
& Lt? Xml version = "1.0 "? & Gt

& Lt! 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 & gt

& Lt weblogic-ejb-jar & gt
& Lt weblogic-enterprise-bean & gt
& Lt ejb-name & gtHelloWorldBean & lt/ejb-name & gt
& Lt caching-descriptor & gt
& Lt max-beans-in-free-pool & gt100 & lt/max-beans-in-free-pool & gt
& Lt/caching-descriptor & gt
& Lt jndi-name & gthello. HelloWorld & lt/jndi-name & gt
& Lt/weblogic-enterprise-bean & gt
& Lt/weblogic-ejb-jar


--------------------------------------------------------------------------------
Ejb-jar.xml
& Lt? Xml version = "1.0" encoding = "GBK "? & Gt
& Lt! DOCTYPE ejb-jar PUBLIC-// Sun Microsystems, Inc. // DTD Enterprise JavaBeans 1.1 // EN http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd & gt

& Lt ejb-jar & gt
& Lt enterprise-beans & gt
& Lt session & gt
& Lt ejb-name & gtHelloWorldBean & lt/ejb-name & gt
& Lt home & gthello. HelloWorldHome & lt/home & gt
& Lt remote & gthello. HelloWorld & lt/remote & gt
& Lt ejb-class & gthello. HelloWorldBean & lt/ejb-class & gt
& Lt session-type & gtStateless & lt/session-type & gt
& Lt transaction-type & gtContainer & lt/transaction-type & gt
& Lt/session & gt
& Lt/enterprise-beans & gt
& Lt assembly-descriptor & gt
& Lt container-transaction & gt
& Lt method & gt
& Lt ejb-name & gtHelloWorldBean & lt/ejb-name & gt
& Lt method-name & gt * & lt/method-name & gt
& Lt/method & gt
& Lt trans-attribute & gtRequired & lt/trans-attribute & gt
& Lt/container-transaction & gt
& Lt/assembly-descriptor & gt
& Lt/ejb-jar & gt

 

--------------------------------------------------------------------------------
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:" + (en

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.