JBoss EJB3.0 STATEFULL Bean

xiaoxiao2021-03-05  28

This example combines the new feature of J2se5.0, you can first look at the new features I wrote J2se5.0 generis.

ShoppingCart.java

Package org.jboss.tutorial.stateful.bean;

Import javax.ejb.remote;

Import javax.ejb.remove;

Import java.util.hashmap;

@Remote

Public Interface ShoppingCart

{

Void Buy (String Product, Int Quantity);

Hashmap getCartContents ();

@Remove void checkout ();

}

ShoppingCartBean.java

Package org.jboss.tutorial.stateful.bean;

Import javax.ejb.remove;

Import javax.ejb.stateful;

Import java.util.hashmap;

Import java.io.serializable;

@Stateful

Public Class ShoppingCartBean Implements ShoppingCart, Serializable

{

Private Hashmap Cart = New Hashmap ();

Public void Buy (String Product, Int Quantity)

{

IF (Cart.Containskey (Product))

{

INT CURRQ = Cart.get (Product);

Currq = quantity;

Cart.Put (Product, Currq);

}

Else

{

Cart.Put (Product, Quantity);

}

}

Public HashMap getCartContents ()

{

Return cart;

}

@Remove

Public void checkout ()

{

System.out.println ("To Be Implement");

}

}

Client.java

Package Org.jboss.tutorial.Stateful.Client;

Import org.jboss.tutorial.stateful.bean.shoppingcart;

Import javax.ejb.ejbexception;

Import javax.naming.initialcontext;

Import java.rmi.nosuchobjectException;

Import java.util.hashmap;

Public Class Client

{

Public static void main (string [] args) Throws Exception

{

InitialContext CTX = New InitialContext ();

ShoppingCart Cart = (ShoppingCart) ctx.lookup (shoppingcart.class.getname ());

System.out.println ("Buying 1 Memory Stick");

Cart.buy ("Memory Stick", 1);

System.out.println ("Buying ANOTHER MEMORY STICK);

Cart.Buy ("Memory Stick", 1); System.out.Println ("Buying a Laptop");

Cart.buy ("Laptop", 1);

System.out.Println ("Print Cart:");

Hashmap fullcart = cart.getCartContents ();

FOR (String Product: fullcart.keyset ())

{

System.out.println (Fullcart.get (Product) " Product);

}

System.out.println ("Checkout");

Cart.checkout ();

System.out.println ("Should Throw An Object Not Found Exception By Invoking On Cart After @Remove Method");

Try

{

Cart.getcartContents ();

}

Catch (EJBEXCEPTION E)

{

IF (E.GETCAUSEDBYEXCEPTION () InstanceOf NosuchobjectException

System.out.println ("Successful Caught No Such Object Exception.");

Else

Throw e;

}

}

}

There is no log4j.properties in jboss-ejb-3.0_preview_5.zip, there is no such thing as the lack of appender. With this will generate a replard.log log file in this directory.

Log4j.properties

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = record.log

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.Appender.r.Layout.conversionPattern =% p% d {hh: mm: ss}% T% c {1} -% M% N

Log4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.maxfilesize = 100kb

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.rootlogger = stdout, r

Run: Refer to Installing.html

Under Windows

Open the command prompt cmd to jboss_home / bin

Run.bat -c all

Use ANT

After Build, run.

discuss:

The problem here is not big. Mainly familiar with J2SE5.0 generis. When writing this article and Entity

It is written by learning jboss-ejb-3.0_preview_5.zip, maybe some places will be unclear. If you have any questions, you can send my email rosonsandy@yahoo.com.cn, the title is 9CBS, I hope to do something.

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

New Post(0)