My EJB learning course (5)

zhaozj2021-02-12  138

Haha, after a relaxed weekend, I have to start writing something.

I was also written on the weekend, but I still feel interesting, aunt, a few days.

The last talk is the stateless session bean.

It seems that it is still a bit. I don't understand why I have two kinds of beans that are united and state session beans, but don't matter, I can understand the program of the state session bean. Yes, there is something to say. I made a day of Eclipse3 on Friday. I heard that he made a new version, so he quickly downloaded the test test. He added a function of the function in it called EJB Editor. This thing is good, there are many new things. But the only thing I have understood is why it always reports when using his Create Client test programm! So or more irritated! I don't know if Eclipse's problem is still the problem of my machine, I will try again and tell me!

My status session bean is still pulling with Eclipse2.1.

I will write the program to everyone, then analyze it, I seem to be the kind of person who likes to look at the program first and then look at the theory. I don't know if someone is like me.

This is the client's implementation program:

Package com.test.session;

Import javax.ejb. *;

/ **

* @ Ejb.bean name = "stateful"

* JNDI-Name = "Statefulbean"

* Type = "stateful"

*

* -

* This is needed for jonas.

* If you are not usning Jonas You can Safely Remove The Tags Below.

* @ Jonas.bean EJB-Name = "Stateful"

* JNDI-Name = "Statefulbean"

*

* -

** /

Public Abstract Class StatefulBean Implements SessionBean {

/ **

* @ ejb.create-method

* View-type = "remote"

** /

Public void ejbcreate (double amout) throws createException {

THIS.AMOUT = Amout;

}

/ **

* @ ejb.interface-method

* View-type = "remote"

** /

Public void addfunds (double amout) {

THIS.AMOUT = Amout;

}

/ **

* @ ejb.interface-method

* View-type = "remote"

** /

Public void withdrawfunds (double amout) {

THIS.AMOUT- = Amout;

}

/ **

* @ ejb.interface-method

* View-type = "remote"

** /

Public Double getBalance () {

Return this.amout;

}

Double amout;

}

Yes, the EJBCREATE method is to call the EJBCREATE (Double Amout) method during the Bean generation process.

It is like a method of initialization.

And you must make sure there is a CREATE (Double Amout) method in the primary interface and Ejbcreate (double amout)! This is a must, if you don't understand, find information. You should choose Create when you are new. Give you the test program of the client:

Package com.testing.client;

Import java.rmi.remoteexception;

Import java.util.hashtable;

Import javax.ejb.createException;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import javax.swing.jframe;

Import javax.swing. *;

Import java.awt. *;

Import java.awt.event. *;

Import java.text. *;

Import com.test.session. *;

/ **

* @Author Sy

*

* Change the template for the generated type annotation

* Window> Preferences> Java> Code Generation> Code and Notes

* /

Public Class TestStateful Extends Jframe IMPLEments ActionListener {

Public testStateful () {

Super ("Fund Manger");

}

Private com.test.session.StateFulhome gethome () throws namingexception {

return (com.test.session.statefulhome) getContext (). Lookup (

com.test.session.StateFulHome.jndi_name);

}

Private InitialContext getContext () throws namingexception {

Hashtable props = new hashtable ();

Props.put

InitialContext.Initial_Context_Factory,

"Org.jnp.interfaces.namingContextFactory");

Props.PUT (InitialContext.Provider_URL, "JNP: //127.0.0.1: 1099");

// this Establishes the security for authorization / authentication

// Props.Put (InitialContext.security_principal, "username");

// Props.Put (InitialContext.security_credentials, "password");

InitialContext InitialContext = New InitialContext (PROPS);

Return INIALCONTEXT;

}

Public void createfund () {

Try {

MyBean = gethome (). CREATE (1000);

// --------------------------------------

// this is the place you make your caver

//System.out.println (MyBean.callyourthod ());

} Catch (RemoteException E) {E.PrintStackTrace ();

} catch (createException e) {

E.PrintStackTrace ();

} catch (namingexception e) {

E.PrintStackTrace ();

}

}

Public void testbean () {

BUILDGUI ();

AddWindowListener (New windowadapter () {public void windowclosing (windowevent evt) {system.exit (0);}});

CreateFund ();

AddFund.addActionListener (this);

WITHDRAW.ADDACTIONLISTENER (this);

Currencyfomat = Numberformat.getCurrencyInstance ();

Try {

String currency = currencyfomat.format (myBean.getBalance ());

Status.setText (MSG Currency);

} catch (exception e) {}

Pack ();

SHOW ();

}

Public Void ActionPerformed (ActionEvent E) {

String str = Amout.getText ();

Try {

IF (E.GetSource () == addfund) {

MyBean.addfunds (Double.Parsedouble (STR));

Currencyfomat = Numberformat.getCurrencyInstance ();

strbar = currencyfomat.format (MyBean.getBalance ());

Status.Settext (MSG strbar);

}

IF (E.getsource () == withdraw) {

MyBean.withdrawfunds (Double.Parsedouble (STR));

Currencyfomat = Numberformat.getCurrencyInstance ();

strbar = currencyfomat.format (MyBean.getBalance ());

Status.Settext (MSG strbar);

}

} catch (exception ex) {}

}

Public void buildinggui () {

GridbagLayout GL = New gridbaglayout ();

Gridbagconstraints gc = new gridbagconstraints ();

Container Container = getContentPane ();

Container.setLayout (GL);

gc.fill = gridbagconstraints.both;

Jlabel label = new Jlabel ("Enter Amout");

Gl.SetConstraints (Label, GC);

Container.Add (label);

gc.gridwidth = gridbagconstraints.remaInder;

Gl.SetConstraints (Amout, GC);

Container.Add (Amout);

Gl.addlayoutComponent (Addfund, GC);

Container.Add (addfund);

Gl.addlayoutComponent (Withdraw, GC);

Container.Add (withdraw);

Status = New Jlabel (MSG);

Gl.addLayoutComponent (Status, GC); Container.Add (status);

}

Double balance = 100;

Jtextfield Amout = New JtextField (10);

JButton AddFund = New JButton ("Add Funds");

JButton withdraw = New JButton ("withdraw funds");

String msg = "Current Funds IS:";

String strbar = "0";

Jlabel Status;

STATEFUL MYBEAN;

Numberformat currencyfomat;

Public static void main (String [] args) {

TestStateful test = new testStateful ();

Test.TestBean ();

}

}

Did you see the door? I finally know why this is called: Status session bean. Because of because. . . . . Haha, he has an Amout variable, this is completely internal variables, saved in the EJB container! And where the stateless session bean is not the same, the stateless session bean is saved in the client. Very big difference! This is the key to the problem! I don't know if you understand! Anyway, I think it is really obvious.

Below this is a pair of pictures I found in the IBM article library, I feel good, I fully explain what the state bean is going to pull:

These are some explanations, and the book is almost the same, save me typing.

State Session Bean Lifecycle:

BEAN is reclaimed in the following cases:

Timeout (whether activated or passivated)

Call remove ()

Passivation is based on the Least-Recently-useed algorithm

Activation is generated by the method of bacan

Afterbegin () beforecompletion () instercompletion () method is only valid for Beans that implements the sessionsynchronization interface.

In a transaction environment, the method of calling must have the same transaction type.

OK, status session bean is such a thing. It should be very simple, it can be understood.

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

New Post(0)