Jboss Ejb3.0 Statefull Bean

Source: Internet
Author: User
Tags object integer log new features string zip jboss log4j



This example combines the new features of j2se5.0 's generis to 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<string, integer> 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<string, integer> cart = new hashmap<string, integer> ();



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<string, integer> getcartcontents ()











{











return cart;











}



@Remove











public void Checkout ()











{











System.out.println ("to be implemented");











}











}















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<string, integer> 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 ("Successfully caught no such object exception.");











Else











Throw e;











}











}











}







Here attached log4j.properties in Jboss-ejb-3.0_preview_5.zip without this always shows a lack of appender. With this, a record.log log file will be generated 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







Running: Reference installing.html











Under Windows











Open command Prompt cmd, to Jboss_home/bin











Run.bat–c All











With Ant











Just build and run.







Discuss:











It's not a big problem here either. is mainly familiar with the generis of j2se5.0. While writing this article and entity,











After learning Jboss-ejb-3.0_preview_5.zip after a period of time to write, may be unclear in some places. Have the question can send my mailbox rosonsandy@yahoo.com.cn, the title writes CSDN on the line, hoped can do something.














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.