Proficient EJB [Three] (reproduced)

xiaoxiao2021-03-06  45

Reprinted from: http://www.java-cn.com/technology/technology_detail.jsp? Id = 783

The stateless session bean foundation 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. The feature of stateless session beans does not have a dialog stateless session bean can have internal states, 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). Initializing stateless beans have only one way. We know that the initialization of session beans calls the ejbcreate () method because the stateless session bean cannot reserve the state between the method call, so it cannot pass the client to ejbcreate () to transfer data. Reserved. Call EJBCREATE () or CREATE () without parameters. Containers can aggregate and reuse stateless sessions beans build "Hello, World!" Remote Interface package com.wiley.compbooks.roman.session.heloworld; 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. Hello interface inherits the EJBOBJECT interface, EJBObject inherits the Remote interface, so Hello can throw RMI anomalies. Establish a bean to achieve business methods: Hello ().

He realized the 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 method 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 HOME interface inherited 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 descriptors In EJB 1.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's Name Enterprise Bean Class Name HOME Interface Class Name Rear Interface Class Name Re-Entrant Status or Status Session Time Hellobean Configuration Descriptor Environment Properties Bean Adapts to different special environments. EJB-JAR file We need to pack our required files into an EJB-JAR file. Enterprise-level Bean Remote Interface Home Interface Configuration Descriptors, including attributes 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 file JAR CMF ../manifest helloworld.jar * Configure the Bean Finally, we also need to configure Bean in the EJB container. Often enable steps: EJB-JAR files Test container tools to generate EJB objects and Home object container tools to generate the Stubs and Skeletons required by RMI and the Skeletons write-free bean customer code 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 executes tasks: positioning HOM E Interface The HOME interface creates an EJB object to call the Hello () remove the Hello () remove the EJB object on the EJB object, run the application server.

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

New Post(0)