How to define stateless session type EJB components

xiaoxiao2021-03-06  43

Assign the Home interface of the EJB component

EJB2.0 specification specifies the Home interface of the EJB component in which the EJBHOME interface object is inherited, and the method for defining the client creation, finding, and deleting the EJB component object instance. This interface can only contain a CREATE method without any argument to creating a component object instance. The HOME interface that follows the EJB2.0 specification is shown in the following code:

//Taxratehome.java

Import javax.ejb. *;

Import java.rmi.remoteexception;

Public Interface TaxrateHome Extends EJBHOME

{

Taxrate Create () THROWS RemoteException, CreateException;

}

The CREATE method defined in this interface for creating an EJB component object instance must return the REMOTE interface of the EJB component object and the method can throw RemoteException exception and CreateException exception.

Define the REMOTE interface of the EJB component

The REMOTE interface of the EJB component is used to define a business method that the component can provide, the interface inherits the EJBObject interface object. In the Remote Interface, you need to define the GetTaxRate method that returns the tax rate value in the Remote Interface of the NSTE interface. Defining the Remote interface of the TaxRateejb component object as shown in the following code:

//Taxrate.java

Import javax.ejb. *;

Import java.rmi.remoteexception;

Public Interface Taxrate Extends EJBOBJECT

{

Float gettaxrate () throws remoteException, UseERException;

}

The GetTaxRate method defined in the Remote interface of the TaxRateejb component object is used for client applications or other EJB components to get tax rates for calculating the tax amount. In addition, the component business method defined in the Remote Interface is throws the RemoteException exception, and the user-defined type exception is also thrown. The definition code of this exception is as follows:

//UseRexception.java

Public Class UseRexception Extends Exception

{

Private int erroorn;

Public useXception () {super ();

Public UseRexception (int erroornum)

{

Super ();

THIS.ERRORNUM = ErrorNum;

}

}

It can be seen from the above user anomaly type definition: The user exception defined during the EJB component is the same as the user type created in the usual JDK application.

Define EJB component classes

The component class of the stateless session type EJB component is used to define the creation component object instance method defined in the HOME interface, and the component business method defined in the Remote interface and the component life during the sessionBean interface control method. Implementing the CREATE method defined in the Home interface In order to separate the CREATE method of the creative component object instance defined in the HOME interface and the method named in the component class in the component class, according to the definition of the EJB specification, the Create method defined in the Home interface The method name mapped in the component class is EJBCREATE. The EJBCREATE method is defined as follows:

Public void ejbcreate () {}

As can be seen from the definition of this method: there is no implementation code in the EJBCREATE method. What is the reason? The reason is that when the EJB server is started, the EJB server automatically creates a stateless session type EJB component object instance in the EJB container based on the EJB component type and quantity deployed. The client application calls the Status Session Type EJB Component Object Home Interface Create method created is not a component object instance, which is created in the EJB container to interact with client applications. RMI remote objects. Relatively, client applications create state session type EJB component object instances in addition to creating RMI remote objects in the EJB container in the EJB container in the EJB container. Of course, you can still define the acquisition TCP / IP connection or use JDBC's database connection in the EJBCREATE method of the EJB component class.

Implement business methods defined in the remote interface

The business method GetTaxRate GetTaxRate is defined in the Remote interface of the TaxRateejb in the stateless session type EJB component object. Client applications or other EJB components call this method to obtain tax rates for calculating the tax amount. The implementation code of the GetTaxRate method is as follows:

Float getTaxrate ()

{RETURN 0.10F;}

It can be seen from the implementation code of the method that the method is only returned to the floating point number 0.1 as the tax rate without defining the code in this method.

Implement the method defined in the sessionBean interface

As can be seen from the sessionBean interface definition: the definition of the interface includes a component life-aortic control method and a context method for setting component instance objects. In the component lifetime control method, the EJBRemove method is used for EJB containers to call the method to remove the component object instance created by the EJBCREATE method, and the EJBActivate method and EJBPassivate method are used to convert the EJB component between activation or off.

The SetSessionContext method in the sessionBean interface is used to record the current context status of the EJB container at the session contextual session context (sessionContext) object instance, when the EJB container is called again in the component object instance, the EJB component can be obtained when the method in the component object instance Operating environment.

The state of stateless session type EJB component is only used for client applications or other EJB components to obtain the tax rate of the calculated tax amount, and there is no system resources such as database connections, so it is not necessary to maintain any client application status at the EJB server. Therefore, in the EJB component class, simply write the defined framework of the above method, no need to write any implementation code for these methods, as shown in the following code:

Public void ejbremove () {}

Public void ejbactivate () {}

Public void ejbpassiVate () {}

Public void setsessionContext (sessionContext CTX) {}

EJB component class definition framework

In the component class definition of the TaxRateejb component object, each method defined in Section 3.1-3.3 and a constructor of such a class. The frame code defined by this class is as follows:

//Taxratebean.java

Public Class TaxrateBean Implements SessionBean

{

//3.1 Implement the CREATE method defined in the HOME interface

...

/ 3.2 Implement the business method defined in the remote interface in the section

...

The method defined in the Javax.ejb.SessionBean interface in //3.3

...

//Construction method

Public TaxrateBean ()}

}

By explaining the previous explanation, do you have a more in-depth understanding of the EJB's stateless session components? You can leave the views and opinions on this series of lectures below, or

Developer Alliance and Netizen Discuss. Next, we will start entering the stateful session type EJB component and EJB entity components, please pay attention.

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

New Post(0)