Excellent EJB (3) stateless session bean foundation

xiaoxiao2021-03-06  62

The stateless session bean is a component that can imitate the business process, which can be performed in a separate method call. The Stateless Session bean is not able to maintain a status of a call customer. In a method call, the Stateless Session bean maintains the status of the calling customer. When the method is executed, the status is not maintained. After the call is completed, the Stateless Session bean is immediately released into the buffer pool, so the Stateless Session bean has good scalability and can support a large number of users.

Features of stateless session beans

No dialog

The stateless session bean can have internal status, and their status cannot be customized for special clients. This means that all stateless beans have no differences for the client, and the client cannot separate them. The client must pass all necessary client data to the parameter of the business logic method to the stateless bean, and the stateless bean can obtain the required data from an external resource (such as a database).

Initialization stateless bean has only one way

We know that the initialization of the session bean calls the ejbcreate () method, because the stateless session bean is not able to reserve the status between the method call, so it cannot be reserved after the client is transferred to ejbcreate (). Call EJBCREATE () or CREATE () without parameters.

Container can gather and reuse stateless session beans

Build "Hello, World!" Remote Interface

Package com.wiley.compbooks.roman.session.helloworld; import javax.ejb. *; import java.rmi.remoteException; import java.rmi.Remote;

/ ** * This is the HelloBean remote interface * * This interface is what clients operate on when * they interact with EJB objects The container * vendor will implement this interface;.. The * implemented object is the EJB object, which * delegates invocations To the actual bean. * /

Public interface hello extends ejbobject {

/ ** * The one method - hello - returns a greeting to the client. * /

Public string hello () throws java.rmi.remoteException;} Source 4.1 Hello.java.

The Hello interface inherits the EJBOBJECT interface, EJBObject inherits the Remote interface, so Hello can throw RMI exceptions.

Establish a bean to achieve business methods: Hello ().

He implements a javax.ejb.sessionBean interface

Package com.wiley.compbooks.roman.session.helloworld; import javax.ejb. *;

/ ** * Demonstration stateless session bean. * /

Public class hellobean implements sessionbean {

// // ejb-required methods // public void ejbcreate () {system.out.println ("ejbcreate ()");} public void ejbremove () {system.out.println ("ejbremove ()");} public void ejbActivate () {System.out.println ( "ejbActivate ()");} public void ejbPassivate () {System.out.println ( "ejbPassivate ()");} public void setSessionContext (SessionContext ctx) {System. Out.println ("SetSessionContext ()");}

/// Business methods //

Public String Hello () {system.out.println ("Hello ()"); return "Hello, World!"

}

Source 4.2 hellobean.java

Note: You don't need to implement your own remote interface, the initialization method does not have parameters. When destroying beans, use a relatively simple EJBREMOVE () method. Ejbactivate () and EJBPassivate () methods do not need to be applied in stateless session beans, so these two methods are empty.

Establish "Hello, World!" HOME interface

The HOME interface inherits Javax.ejb.ejbhome. The HOME interface extends a method that does not have a parameter for the EJB object ?? Create () method.

Package com.wiley.compbooks.roman.session.helloworld; import javax.ejb. *; import java.rmi.remoteException;

/ ** * This is the home interface for HelloBean This interface * is implemented by the EJB Server's glue-code tools -.. The * implemented object is called the Home Object and serves * as a factory for EJB Objects * * One Create () Method is in this home interface, which * corresponds to the ejbcreate () method in hellobean. * /

Public interface hellohome extends ejbhome {

/ * * This method creates the ejb object. * * @Return the newly created ejb object. * /

Hello Create () Throws RemoteException, CreateException;} Creat method throws a java.rmi.RemoteException and aavax.ejb.createException. Exception. Write configuration descriptor

In EJB1.0, the configuration descriptor is a Java object stored on the disk as a file. In EJB1.1, the configuration descriptor is an XML document. The EJB container or IDE environment should provide a tool for generating a configuration descriptor.

Configuration descriptor settings

Bean home

Enterprise bean class name

HOME interface class name

Remote interface class name

RE-Entrant

State or stateless

Session time

Hellobean configuration descriptor

Environmental properties

BEAN is adapted to different special environments by using this information.

EJB-JAR file

We need to pack our needs into the EJB-JAR file.

Enterprise-class bean

Remote interface

HOME interface

Configuration descriptor, including properties

These these must be included in the EJB-JAR file. In EJB1.0, the JAR file has a list of text files. It represents the details of JAR. It is used to identify which business bean is in the EJB-JAR file. In EJB1.1, the XML file contains all necessary information.

Generate EJB-JAR files

JAR CMF ../manifest helloworld.jar * Configure Bean

Finally, we also need to configure Bean in the EJB container. Often perform steps:

EJB-JAR file inspection

Container tools to generate EJB objects and home objects

Container tools to generate stubs and skeletons required for RMI

Customer code written in stateless bean

Package com.wiley.compbooks.roman.session.helloworld; import javax.ejb. *; import javax.naming. *; import java.rmi. *; import java.util.properties;

/ ** * this class is an esample of client code That Invokes * Methods on a Simple Stateless Session bean. * /

Public class helloclient {public static void main (string [] args) {try {

/ * * Get System Properties for JNDI INITIALIZATION * /

Properties PROPS = system.getproperties ();

/ * Form an initial context * /

Context CTX = New InitialContext (PrOPS); / * * Get a reason to the home object * (The factory for ejb objects) * / hellohome home = (HelloHome) CTX.lookup ("HelloHome"); / * * Use the factory To create the ejb object * / hello hello = home.create (); / * * call the hello () method, and print it * / system.out.Println (Hello Hello ()); / * * Done with EJB Object, so remove it * / hello.remove ();} catch (exception e) {E.PrintStackTrace ();}}} client code performs a task: Positioning Home Interface

Estim EJB object using the Home interface

Call Hello () on the EJB object

Remove EJB object

run

First run the application server. For BEA WebLogic, execution

T3Server

Client execution:

java -Djava.naming.factory.initial = weblogic.jndi.TengahInitialContextFactory -Djava.naming.provider.url = t3: // localhost: 7001 com.wiley.compBooks.roman.session.helloworld.HelloClient server output:

SetSessionContext () Ejbcreate () Hello () EJBREMOVE ()

Client output:

Hello, World!

转载请注明原文地址:https://www.9cbs.com/read-117260.html

New Post(0)