⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 master-ejb-4.htm

📁 写给JSP初级程序员的书
💻 HTM
字号:
<html><!-- #BeginTemplate "/Templates/more.dwt" -->
<head>
<!-- #BeginEditable "doctitle" --> 
<title>csdn_精通ejb【四】</title>
<!-- #EndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<STYLE type=text/css>
A:link {
	COLOR: #000000; FONT-FAMILY:verdana,宋体,新宋体; TEXT-DECORATION: none
}
A:visited {
	COLOR: #333399; FONT-FAMILY:verdana,宋体,新宋体; TEXT-DECORATION: none
}
A:active {
	COLOR: #ff0000; FONT-FAMILY:verdana,宋体,新宋体; TEXT-DECORATION: none
}
A:hover {
	COLOR: black; TEXT-DECORATION: underline
}
BODY {
	 COLOR: #000000; FONT-SIZE:9pt; LETTER-SPACING: normal; LINE-HEIGHT: 150%; WORD-SPACING: 2em
}
TH {
	FONT-SIZE: 9pt
}
TD {
	FONT-SIZE: 9pt
}
TD.page {
	COLOR: #000000; FONT-SIZE:9pt; LETTER-SPACING: normal; LINE-HEIGHT: 150%; WORD-SPACING: 2em
}
TD.title {
	COLOR: #000000; FONT-FAMILY:verdana,宋体,新宋体
}
TD.detail {
	COLOR: #9966ff; FONT-FAMILY:verdana,宋体,新宋体
}
</STYLE>

</head>

<body bgcolor="#FFFFFF" text="#000000" >
<div align="center"></div>
<table width="700" border="0" align="center">
  <tr> 
    <table width="700" border="1" cellpadding="1" cellspacing="0" bordercolorlight="#9898ba" bordercolordark="#000000">
        
      </table>
      <table width="700" cellspacing="0" cellpadding="0" bgcolor="9898ba" border="0">
        <tr valign="middle"></tr>
      </table>
      <div align="center"><b></div>
      
      <br>
      <table width="700" border="0">
        <tr> 
          <td width="20">&nbsp;</td>
          <td colspan="2"> 
            <div align="center">
              <h3><b><!-- #BeginEditable "5" -->
              <h3><font face="Verdana, Arial, Helvetica, sans-serif" ><b>精通ejb【四】</b></font></h3>
              <!-- #EndEditable --></b></h3>
            </div>
          </td>
          <td width="20">&nbsp;</td>
        </tr>
        <tr> 
          <td width="20">&nbsp;</td>
          <td  colspan="2"><!-- #BeginEditable "6" -->
            <p>状态会话Bean基础 </p>
            <p>Stateful Session Bean可以一对一的维持某个调用客户的状态,并且在不同的方法调用中维持这个状态, 由于对于每一个并发用户,必须有一个对应的Stateful 
              Session Bean,为了提高系统的效率,Stateful Session Bean可以在一定的客户空闲时间后被写入二级存储设备(如硬盘),在客户发出新的调用请求后,再从二级存储 
              设备恢复到内存中。但是在多用户下,Stateless Session Bean运行效率高于Stateful Session Bean。 
              <br>
              javax.ejb.EnterpriseBean接口继承了java.io.Serializable,用以实现写入读出操作。 <br>
              当EJB容器调用ejbPassivate()方法钝化了bean之后,就可以把它写入二级存储设备,然后容器调用ejbActivate()方法激活bean,把它从二级存储设备中读出。 
            </p>
            <p>状态bean的钝化过程 <br>
              计数bean的远程接口 <br>
              远程接口定义了一个业务方法count(),它将在企业bean类中实现。 </p>
            <p><br>
              激活状态bean <br>
              package com.wiley.compBooks.roman.session.count; <br>
              import javax.ejb.*; <br>
              import java.rmi.RemoteException; <br>
              /** <br>
              * These are CountBean's business logic methods. <br>
              * <br>
              * This interface is what clients operate on when they <br>
              * interact with EJB objects. The container vendor will <br>
              implement this interface; the implemented object is <br>
              * the EJB Object, which delegates invocations to the <br>
              * actual bean. <br>
              */ <br>
              public interface Count extends EJBObject { <br>
              /** <br>
              * Increments the int stored as conversational state <br>
              */ <br>
              public int count() throws RemoteException; <br>
              } <br>
              Source Count.java </p>
            <p>package com.wiley.compBooks.roman.session.count; <br>
              import javax.ejb.*; <br>
              /** <br>
              * Demonstration Stateful Session Bean. This bean is <br>
              * initialized to some integer value and has a business <br>
              * method that increments the value. <br>
              * <br>
              * This example shows the basics of how to write a stateful <br>
              * session bean and how passivation/activation works. <br>
              */ <br>
              public class CountBean implements SessionBean { <br>
              private SessionContext ctx; <br>
              // The current counter is our conversational state. <br>
              public int val; <br>
              // <br>
              // Business methods <br>
              // <br>
              /** <br>
              * Counts up <br>
              */ <br>
              public int count() { <br>
              System.out.println(&quot;count()&quot;); <br>
              return ++val; <br>
              } <br>
              // <br>
              // EJB-required methods <br>
              // <br>
              public void ejbCreate(int val) throws CreateException { <br>
              this.val = val; <br>
              System.out.println(&quot;ejbCreate()&quot;); <br>
              } <br>
              public void ejbRemove() { <br>
              System.out.println(&quot;ejbRemove()&quot;); <br>
              } <br>
              public void ejbActivate() { <br>
              System.out.println(&quot;ejbActivate()&quot;); <br>
              } <br>
              public void ejbPassivate() { <br>
              System.out.println(&quot;ejbPassivate()&quot;); <br>
              } <br>
              public void setSessionContext(SessionContext ctx) { <br>
              } <br>
              } <br>
              Source CountBean.java <br>
              Bean实现了javax.ejb.SessionBean。所以,它必须定义所有SessionBean定义的方法。 <br>
              OjbCreate()初始化带了val的参数。它将作为counter的初始状态。在钝化和激活bean的过程中,val变量被保护。 
            </p>
            <p>计数bean的home接口 <br>
              package com.wiley.compBooks.roman.session.count; <br>
              import javax.ejb.*; <br>
              import java.rmi.RemoteException; <br>
              /** <br>
              * This is the home interface for CountBean. This interface <br>
              * is implemented by the EJB Server's glue-code tools - the <br>
              * implemented object is called the Home Object and serves <br>
              * as a factory for EJB Objects. <br>
              * <br>
              * One create() method is in this Home Interface, which <br>
              * corresponds to the ejbCreate() method in the CountBean file. <br>
              */ <br>
              public interface CountHome extends EJBHome { <br>
              /* <br>
              * This method creates the EJB Object. <br>
              * <br>
              * @param val Value to initialize counter to <br>
              * <br>
              * @return The newly created EJB Object. <br>
              */ <br>
              Count create(int val) throws RemoteException, CreateException; <br>
              } <br>
              Source CountHome.java. <br>
              计数bean的配置描述符 </p>
            <p>计数bean的配置描述符 <br>
              计数bean的环境属性 <br>
              生成计数bean的Ejb-jar文件 <br>
              计数bean的客户端代码 <br>
              package com.wiley.compBooks.roman.session.count; <br>
              import javax.ejb.*; <br>
              import javax.naming.*; <br>
              import java.util.Properties; <br>
              /** <br>
              * This class is a simple example of client code that invokes <br>
              * methods on a simple Stateless Enterprise Bean. <br>
              * <br>
              * We create 3 EJB Objects in this example, but we allow <br>
              * the container to have only 2 in memory. This illustrates how <br>
              * beans are passivated to storage. <br>
              */ <br>
              public class CountClient { <br>
              public static void main(String[] args) { <br>
              try { <br>
              /* <br>
              * Get System properties for JNDI initialization <br>
              */ <br>
              Properties props = System.getProperties(); <br>
              /* <br>
              * Get a reference to the Home Object - the <br>
              * factory for EJB Objects <br>
              */ <br>
              Source CountClient.java <br>
              1、需要JNDL初始化上下文 <br>
              2、使用JNDL定位home接口 <br>
              3、使用home对象建立3个不同的计数EJB对象,因此也就和三个不同的客户端建立了会话 <br>
              4、配置描述符限制了同时只能有两个bean工作,因此3个bean中一定有钝化的。在调用ejbPassivate()时,打印一条信息。 
              <br>
              5、在每个EJB对象上调用count()方法,调用ejbActivate()方法激活bean,该方法打印一条信息。 <br>
              6、最后所有的EJB对象被删除。 <br>
              package com.wiley.compBooks.roman.session.count; <br>
              import javax.ejb.*; <br>
              import javax.naming.*; <br>
              import java.util.Properties; <br>
              /** <br>
              * This class is a simple example of client code that invokes <br>
              * methods on a simple Stateless Enterprise Bean. <br>
              * <br>
              * We create 3 EJB Objects in this example, but we allow <br>
              * the container to have only 2 in memory. This illustrates how <br>
              * beans are passivated to storage. <br>
              */ <br>
              public class CountClient { <br>
              public static void main(String[] args) { <br>
              try { <br>
              /* <br>
              * Get System properties for JNDI initialization <br>
              */ <br>
              Properties props = System.getProperties(); <br>
              /* <br>
              * Get a reference to the Home Object - the <br>
              * factory for EJB Objects <br>
              */ <br>
              Context ctx = new InitialContext(props); <br>
              CountHome home = (CountHome) ctx.lookup(&quot;CountHome&quot;); 
              <br>
              /* <br>
              * An array to hold 3 Count EJB Objects <br>
              */ <br>
              Count count[] = new Count[3]; <br>
              int countVal = 0; <br>
              /* <br>
              * Create and count() on each member of array <br>
              */ <br>
              System.out.println(&quot;Instantiating beans...&quot;); <br>
              for (int i=0; i &lt; 3; i++) { <br>
              /* <br>
              * Create an EJB Object and initialize <br>
              * it to the current count value. <br>
              */ <br>
              count[i] = home.create(countVal); <br>
              /* <br>
              * Add 1 and print <br>
              */ <br>
              countVal = count[i].count(); <br>
              System.out.println(countVal); <br>
              /* <br>
              * Sleep for 1/2 second <br>
              */ <br>
              Thread.sleep(500); <br>
              } <br>
              /* <br>
              * Let's call count() on each EJB Object to <br>
              * make sure the beans were passivated and <br>
              * activated properly. <br>
              */ <br>
              System.out.println(&quot;Calling count() on beans...&quot;); <br>
              for (int i=0; i &lt; 3; i++) { <br>
              /* <br>
              * Add 1 and print <br>
              */ <br>
              countVal = count[i].count(); <br>
              System.out.println(countVal); <br>
              /* <br>
              * Sleep for 1/2 second <br>
              */ <br>
              Thread.sleep(500); <br>
              } <br>
              /* <br>
              * Done with EJB Objects, so remove them <br>
              */ <br>
              for (int i=0; i &lt; 3; i++) { <br>
              count[i].remove(); <br>
              } <br>
              } catch (Exception e) { <br>
              e.printStackTrace(); <br>
              } <br>
              } <br>
              } <br>
              Source CountClient.java <br>
              运行客户端: <br>
              对于BEA的WebLogic,执行: <br>
              java -Djava.naming.factory.initial= <br>
              weblogic.jndi.TengahInitialContextFactory <br>
              -Djava.naming.provider.url= <br>
              t3://localhost:7001 <br>
              com.wiley.compBooks.roman.session.count.CountClient <br>
              客户端输出: <br>
              Instantiating beans... <br>
              1 <br>
              2 <br>
              3 <br>
              Calling count() on beans... <br>
              2 <br>
              3 <br>
              4 <br>
              服务端输出: <br>
              ejbCreate() <br>
              count() <br>
              ejbCreate() <br>
              count() <br>
              ejbCreate() <br>
              ejbPassivate() <br>
              count() <br>
              ejbPassivate() <br>
              ejbActivate() <br>
              count() <br>
              ejbPassivate() <br>
              ejbActivate() <br>
              count() <br>
              ejbPassivate() <br>
              ejbActivate() <br>
              count() <br>
              ejbPassivate() <br>
              ejbActivate() <br>
              ejbRemove() <br>
              ejbActivate() <br>
              ejbRemove() <br>
              ejbRemove() <br>
            </p>
            <!-- #EndEditable --></td>
         
  </tr>
</table>
<div align="center"> 
  
  <br>
</div>
</body>
<!-- #EndTemplate --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -