Jboss Ejb3.0 Statefull Bean

來源:互聯網
上載者:User



這個例子結合了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之後隔了一段時間寫的,可能有些地方會不清楚。有問題可以發我郵箱rosonsandy@yahoo.com.cn,標題寫csdn就行了,希望能做些什麼。














相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。