Stateless Session Bean 示例和注意点

(1)定义远程接口

package examples.ejb20.basic.statelessSession;

import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface Trader extends EJBObject {

  public  TradeResult buy (String stockSymbol, int shares)
    throws RemoteException;

  public TradeResult sell (String stockSymbol, int shares)
    throws RemoteException;
}

 

(2) 定义Home接口

package examples.ejb20.basic.statelessSession;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface TraderHome extends EJBHome {
  Trader create() throws CreateException, RemoteException;
}

 

(3)定义EJB的实现类

 

package examples.ejb20.basic.statelessSession;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TraderBean implements SessionBean {

  private static final boolean VERBOSE = false;
  private SessionContext ctx;
  private int tradeLimit;

  // You might also consider using WebLogic's log service
  private void log(String s) {
    if (VERBOSE) System.out.println(s);
  }

  public void ejbActivate() {
    log("ejbActivate called");
  }

  public void ejbRemove() {
    log("ejbRemove called");
  }

  public void ejbPassivate() {
    log("ejbPassivate called");
  }

  public void setSessionContext(SessionContext ctx) {
    log("setSessionContext called");
    this.ctx = ctx;
  }

  public void ejbCreate () throws CreateException {
    log("ejbCreate called");

    try {
      InitialContext ic = new InitialContext();

      Integer tl = (Integer) ic.lookup("java:/comp/env/tradeLimit");

      tradeLimit = tl.intValue();
    } catch (NamingException ne) {
      throw new CreateException("Failed to find environment value "+ne);
    }
  }

  public TradeResult buy(String stockSymbol, int shares) {

    if (shares > tradeLimit) {
      log("Attempt to buy "+shares+" is greater than limit of "+tradeLimit);
      shares = tradeLimit;
    }

    log("Buying "+shares+" shares of "+stockSymbol);

    return new TradeResult(shares, stockSymbol);
  }

  public TradeResult sell(String stockSymbol, int shares) {

    if (shares > tradeLimit) {
      log("Attempt to sell "+shares+" is greater than limit of "+tradeLimit);
      shares = tradeLimit;
    }

    log("Selling "+shares+" shares of "+stockSymbol);

    return new TradeResult(shares, stockSymbol);
  }

}

 (4) 配置文件

ejb-jar.xml

 

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC 
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>statelessSession</ejb-name>
      <home>examples.ejb20.basic.statelessSession.TraderHome</home>
      <remote>examples.ejb20.basic.statelessSession.Trader</remote>
      <ejb-class>examples.ejb20.basic.statelessSession.TraderBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
      <env-entry>
        <env-entry-name>WEBL</env-entry-name>
	<env-entry-type>java.lang.Double </env-entry-type>
	<env-entry-value>10.0</env-entry-value>
      </env-entry>
      <env-entry>
        <env-entry-name>INTL</env-entry-name>
	<env-entry-type>java.lang.Double </env-entry-type>
	<env-entry-value>15.0</env-entry-value>
      </env-entry>
      <env-entry>
        <env-entry-name>tradeLimit</env-entry-name>
	<env-entry-type>java.lang.Integer </env-entry-type>
	<env-entry-value>500</env-entry-value>
      </env-entry>
    </session>
  </enterprise-beans>
  <assembly-descriptor>
    <container-transaction>
      <method>
        <ejb-name>statelessSession</ejb-name>
	<method-name>*</method-name>
      </method>
      <trans-attribute>Required</trans-attribute>
    </container-transaction>
  </assembly-descriptor>
  <ejb-client-jar>ejb20_basic_statelessSession_client.jar</ejb-client-jar>
</ejb-jar>

 

weblogic-ejb-jar.xml

<?xml version="1.0"?>

<!DOCTYPE weblogic-ejb-jar PUBLIC 
'-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN'
'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>

<weblogic-ejb-jar>
  <weblogic-enterprise-bean>
    <ejb-name>statelessSession</ejb-name>
    <enable-call-by-reference>True</enable-call-by-reference>
    <jndi-name>ejb20-statelessSession-TraderHome</jndi-name>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>

 

(5)EJB client

package examples.ejb20.basic.statelessSession;

import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Client
{
  private static final String JNDI_NAME = "ejb20-statelessSession-TraderHome";
  private String url;
  private TraderHome home;

  public Client(String url) throws NamingException {
    this.url = url;
    home = lookupHome();
  }

   public static void main(String[] args) throws Exception {
    log("\nBeginning statelessSession.Client...\n");
    String url       = "t3://localhost:7001";
    Client client = null;

    // Parse the argument list
     if (args.length != 1) {
      log("Usage: java examples.ejb20.basic.statelessSession.Client t3://hostname:port");
      return;
    } else {
      url = args[0];
    }

    try {
      client = new Client(url);
      client.example();
    } catch (NamingException ne) {
      log("Unable to look up the beans home: " + ne.getMessage());
      throw ne;
    } catch (Exception e) {
      log("There was an exception while creating and using the Trader.");
      log("This indicates that there was a problem communicating with the server: "+e);
      throw e;
    }

    log("\nEnd statelessSession.Client...\n");
  }

  /**
   * Runs this example.
   */
  public void example()
    throws CreateException, RemoteException, RemoveException
  {
    // create a Trader
    log("Creating a trader");
    Trader trader = (Trader) narrow(home.create(), Trader.class);

    String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };

      // execute some buys
    for (int i=0; i<stocks.length; i++) {
      int shares = (i+1) * 100;
      log("Buying "+shares+" shares of "+stocks[i]+".");
      trader.buy(stocks[i], shares);
    }

    // execute some sells
    for (int i=0; i<stocks.length; i++) {
      int shares = (i+1) * 100;
      log("Selling "+shares+" shares of "+stocks[i]+".");
      trader.sell(stocks[i], shares);
    }

    // remove the Trader
    log("Removing the trader");
    trader.remove();
  }

  /**
   * RMI/IIOP clients should use this narrow function
   */
  private Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
  }

  /**
   * Lookup the EJBs home in the JNDI tree
   */
  private TraderHome lookupHome() throws NamingException {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();
    try {
      Object home = ctx.lookup(JNDI_NAME);
      return (TraderHome) narrow(home, TraderHome.class);
    } catch (NamingException ne) {
      log("The client was unable to lookup the EJBHome.  Please make sure ");
      log("that you have deployed the ejb with the JNDI name "+
        JNDI_NAME+" on the WebLogic server at "+url);
      throw ne;
    }
  }

  /**
   * Using a Properties object will work on JDK 1.1.x and Java2
   * clients
   */
  private Context getInitialContext() throws NamingException {
    
    try {
      // Get an InitialContext
      Hashtable h = new Hashtable();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
    } catch (NamingException ne) {
      log("We were unable to get a connection to the WebLogic server at "+url);
      log("Please make sure that the server is running.");
      throw ne;
    }
  }

  private static void log(String s) { System.out.println(s); }
  
}

 

 

 c:> java weblogic.ejbc  ...  编译ejb.jar  现在都自动完成了

 c:> java weblogic.deploy ...

 

打包成ear的配置文件application.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>

<application>
  <display-name>Basic StatelessSession ejb20</display-name>
  <description>Basic StatelessSession ejb20</description>

  <module>
    <ejb>ejb20_basic_statelessSession.jar</ejb>
  </module>

</application>

 

 使用ebj对象缓冲池来保存 无状态会话bean的对象。bean的实例都是相同的。缓冲池的设置取决于

(1) weblogic的线程数

(2) 数据库的连接数

 

生命周期的两种状态:

(1) 不存在

(2) 方法就绪池

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值