這個例子結合了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
@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
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
{
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
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就行了,希望能做些什麼。