Stateful EJB objects and stateless EJB objects

Source: Internet
Author: User



One, define stateful beans and stateless beans


Stateful Bean:


@Stateful @remotepublic class Statefulejbbean implements statefulejb{private int state; @Overridepublic void Compute (int i) {state=state+i;} @Overridepublic int GetResult () {return state;}}


Stateless Bean:


@Stateless @remotepublic class Statelessejbbean implements STATELESSEJB {private int state; @Overridepublic void Compute ( int i) {state = state + i;} @Overridepublic int GetResult () {return state;}}


two, client test and results


1, test the stateful EJB object:


public class Statefulejbclient {public static void main (string[] args) throws Exception {InitialContext context=new Initia Lcontext ();//First session STATEFULEJB ejb1= (STATEFULEJB) context.lookup ("Statefulejbbean/remote"); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); System.out.println ("First session end---------");//second session STATEFULEJB ejb2= (STATEFULEJB) context.lookup ("statefulejbbean/ Remote "); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); System.out.println ("Second session end---------");}}

Results:




2, test the stateless EJB object:



public class Statelessejbclient {public static void main (string[] args) throws Namingexception {InitialContext Context=ne W InitialContext ();//First session STATELESSEJB ejb1= (STATELESSEJB) context.lookup ("Statelessejbbean/remote"); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); Ejb1.compute (1); System.out.println (Ejb1.getresult ()); System.out.println ("First session end---------");//second session STATELESSEJB ejb2= (STATELESSEJB) context.lookup ("statelessejbbean/ Remote "); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); Ejb2.compute (1); System.out.println (Ejb2.getresult ()); System.out.println ("The second session ends---------");//Determine if each object found is the sameSystem.out.println (EJB1==EJB2);//false}} 

Results:





Third, the results contrast


Through multiple executions, it is found that for stateful EJB objects, each object obtained through a lookup is a new object, whereas for stateless EJB objects, each Find object has a singleton effect, and executes the method of testing the stateless EJB object multiple times. You will find that the server looks like it is always working on an object.







Stateful EJB objects and stateless EJB objects

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.