Jing EJB [4] (reproduced)

xiaoxiao2021-03-06  50

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

Status Session Bean Basics STATEFUL Session Bean can maintain a status of a call customer, and maintain this state in different method calls, because for each concired user, there must be a corresponding stateful session bean, in order to improve the system The efficiency, Stateful Session beans can be written to a secondary storage device (such as a hard disk) after a certain customer idle time, and after the customer issues a new call request, returns to the memory from the secondary storage device. But under multi-users, the Stateless Session Bean has higher operating efficiency than Stateful Session Bean. The Javax.ejb.EnterpriseBean interface inherits java.io.serializable to implement write read operations. When the EJB container calls the ejbpassivate () method passivates the bean, you can write it to the secondary storage device, and the container calls the ejbactivate () method to activate the bean, and read it from the secondary storage device. Status Bean's passivation process counting Remote interface remote interface defines a business method count (), which will be implemented in the enterprise bean class.

Active state bean package com.wiley.compBooks.roman.session.count; import javax.ejb *;. Import java.rmi.RemoteException; / ** * These are CountBean's business logic methods * * 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 Count extends EJBObject {/ ** * Increments the int stored as conversational state * / public int count () throws RemoteException;} Source Count.java package com.wiley.compBooks.roman.session.count; import javax.ejb *;. / ** * Demonstration Stateful Session Bean This. bean is * initialized to some integer value and has a business * method that increments the value. * * This example shows the basics of how to write a stateful * session bean and how passivation / activation works. * / public class CountBean implements SessionBean { Private sessionContext CTX; // The Current Counter IS OUR Conversational state. public int val; // // business methods /// / ** * counts up * / public int count () {system.Println ("count ()"); Return Val;} // // ejb-required methods // public void ejbcreate (int vali) throws createException {this.Val = val; 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) {}} Source CountBean.java bean implements javax.ejb.sessionBean. So it must define all methods defined by the sessionBean. Ojbcreate () initializes the parameters of VAL. It will be the initial state of the Counter.

The VAL variable is protected during passivation and activation of the bean. Counting bean home interface package com.wiley.compBooks.roman.session.count; import javax.ejb *;. Import java.rmi.RemoteException; / ** * This is the home interface for CountBean 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 the CountBean file. * / public interface CountHome extends EJBHome {/ * * This method creates the EJB Object. * * @param val Value to initialize counter to * * @return The newly created EJB Object. * / Count create (int VAL) THROWS RemoteException, CreateException;} Source Counthome.java. Counting Bean Configuration Descriptor Count BEAN Configuration Descriptor Counting Bean Environment Property Generates Count BEB-JAR File Count BEAN Client Code Package Com.wiley.compbooks .roman.session.count; import javax.ejb. *; import javax.naming. *; import java.util.properties; / ** * this class is a Simple Example of Client Code That Invokes * Methods On A Simple Statel ess Enterprise Bean. * * We create 3 EJB Objects in this example, but we allow * the container to have only 2 in memory. This illustrates how * beans are passivated to storage. * / public class CountClient {public static void main (String [] args) {try {/ * * Get System properties for JNDI initialization * / Properties props = System.getProperties (); / * * Get a reference to the Home Object - the * factory for EJB Objects * / Source CountClient.java 1. Just JNDL initialization context 2, use the JNDL positioning home interface 3, use the Home object to establish three different count EJB objects, so the session 4 is established with three different clients 4, the configuration descriptor is limited to only There are two beans work, so three beans must be passivated. Print a message when ejbpassivate () is called. 5. Call the count () method on each EJB object, call the ejbactivate () method to activate the bean, which prints a message. 6, the last EJB object is deleted.

Package com.wiley.compbooks.roman.session.count; import javax.ejb. *; import javax.naming. *; import java.util.properties; / ** * this class is a simple esample of client code invokes * methods on a simple Stateless Enterprise Bean. * * We create 3 EJB Objects in this example, but we allow * the container to have only 2 in memory. This illustrates how * beans are passivated to storage. * / public class CountClient {public static void main (String [] args) {try {/ * * Get System properties for JNDI initialization * / Properties props = System.getProperties (); / * * Get a reference to the Home Object - the * factory for EJB Objects * / Context ctx = new initialcontext (props); counthome home = (countHome) CTX.lookup ("countHome"); / * * an array to hold 3 count ejb Objects * / count count [] = new count [3]; int COUNTVAL = 0; / * * Create and count () on each member of array * / system.out.println ("instantiating beans ..."); for (int i = 0; i <3; i ) {/ * * Create an EJB Object and Initialize * IT T o the current count value. * / count [i] = home.create (countval); / * * add 1 and print * / countval = count [i] .count (); system.out.println (countval); / * * Sleep for 1/2 second * / thread.sleep (500);} / * * let's call count () on each ejb Object to * make Sure the beans around passidid and * activated prot ou. * / System.out .println ("Calling Count () on beans ..."); for (int i = 0; i <3; i ) {/ * * add 1 and print * / countval = count [i] .count (); System.out.println (Countval); / * * Sleep for 1/2 second * / thread.sleep (500);} / * * done with ejb objects, so remove the * / for (int i = 0; i < 3; i ) {count [i] .remove ();

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

New Post(0)