My EJB learning course (four)

zhaozj2021-02-16  63

The front program is easy to use, I feel that the troubles written more than the book is much more, so I still have tools.

Finally, there is a point of AJB achievements, hot iron, now you will look at the third chapter.

Chapter 1 Development Session Bean

Everyone knows that the session bean is divided into state session beans and stateless sessions beans.

The book summed up their common point:

1. Both can realize the Javax.ejb.SessionBean interface, so there is the same container callback. Container callback? Now China's translation!

2. Both represent dedicated resources for generating BEAN customers.

3. Both can construct a process or a task model

4. Both can update the shared data, but the shared data is displayed as in the entity bean.

NND, I said so many common points, tell the truth, I don't know what he meant at all. Why is it so professional? ? ()

But different points but very clear:

Status Session Bean can save data between customer access, without status!

Take him, from the example, find the entry point.

A simple example of the stateless session bean is calculating the number, the number can be arbitrarily complex, but only stores in the client program. Oh! This is the case, it is actually quite free, but it seems that I can't say this, just so understand.

So, this time I decided to adopt the example of the book, the example of the book is equivalent to a Bank system! Exaggerate, it is actually adding minimalism.

In this way, we are still deploying him with the method inside (three):

Remember is an example of a stateless session bean.

Create a new implementation class, I wrote this:

Package com.testing.fund;

Import javax.ejb.sessionbean;

/ **

* @ ejb.bean name = "fundmanger"

* JNDI-NAME = "Fundmangerbean"

* Type = "stateless"

*

* -

* This is needed for jonas.

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

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

* JNDI-NAME = "Fundmangerbean"

*

* -

** /

Public Abstract Class FundmangerBean Implements SessionBean {

/ **

* @ ejb.interface-method

* View-type = "remote"

** /

Public Double Add (Double B, Double A) {

B = B a;

Return B;

}

/ **

* @ ejb.interface-method

* View-type = "remote"

** /

Public Double Withdraw (Double B, Double A) {

B = B-A;

Return B;

}

}

I have to remember the ADD and WITHDRAW methods in this, it is not manual plus, it is Wizard. If you won't, look at me (3)! ! ! ! Then deploy it on the JBoss on the side of the (3)!

If everything is successful, or write one implementation in accordance with (3)! I wrote this, and the book is a bit different, but the implementation method is exactly the same!

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. *;

Import java.awt. *;

Import java.awt.event. *;

Import java.text. *;

Import com.testing.fund.fundman;

Public Class Fundtest Extends Jframe Implements ActionListener {

Public Fundtest () {

Super ("Fund Manger");

}

Private com.testing.fund.fundmangerhome gethome () throws namingexception {

return (com.testing.fund.fundmanGerhome) getContext (). Lookup

com.testing.fund.fundmangerHome.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 testbean () {

BUILDGUI ();

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

CreateFund ();

AddFund.addActionListener (this);

WITHDRAW.ADDACTIONLISTENER (this);

Currencyfomat = Numberformat.getcurrencyinstance (); string currency = currencyfomat.format (0);

Status.setText (MSG Currency);

Pack ();

SHOW ();

}

Public Void ActionPerformed (ActionEvent E) {

String str = Amout.getText ();

Try {

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

Balance = (Double) MyBean.add (Balance, Double.Parsedouble);

Currencyfomat = Numberformat.getCurrencyInstance ();

Strbar = currencyfomat.format (balance);

Status.Settext (MSG strbar);

}

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

Balance = (double) MyBean.withdraw (Balance, Double.Parsedouble (STR));

Currencyfomat = Numberformat.getCurrencyInstance ();

Strbar = currencyfomat.format (balance);

Status.Settext (MSG strbar);

}

} catch (exception ex) {}

}

Public void createfund () {

Try {

MyBean = gethome (). CREATE ();

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

// 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 static void main (String [] args) {

Fundtest Test = New FundTest ();

Test.TestBean ();

}

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;

Fundmanger mybean;

Numberformat currencyfomat;

}

Run as Java app! Success! The interface is as follows:

Now I think about why there is a stateless session bean. In fact, this EJB is equivalent to a simple application background data processor! All your data record information is all in front desk, that is, on the side of this interface.

OK, the next class is the status session bean.

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

New Post(0)