欢迎光临
我们一直在努力

Jboss Ejb3.0 Statefull Bean-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

这个例子结合了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就行了,希望能做些什么。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Jboss Ejb3.0 Statefull Bean-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章