程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Jboss Ejb3.0 Statefull Bean

Jboss Ejb3.0 Statefull Bean

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved