当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

Jboss Ejb3.0 Statefull Bean




这个例子结合了j2se5.0的generis新特性,可以先看下我写的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;











      }











   }











}



 



这里附上log4j.properties 在jboss-EJB-3.0_Preview_5.zip 里面没有这个老是显示缺少appender。有了这个将在该目录下生成个record.log日志文件。



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 



 



运行:参考installing.html











Windows下











打开命令提示符cmd,到  jboss_home/bin











 Run.bat –c all











用ant











先build后run 就行了。



 



讨论:











这里的问题也不大吧。主要是熟悉j2se5.0的generis .在写这篇文章和entity的时候











是学jboss-EJB-3.0_Preview_5.zip之后隔了一段时间写的,可能有些地方会不清楚。有问题可以发我邮箱[email protected],标题写csdn就行了,希望能做些什么。












相关内容
赞助商链接