国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

精通ejb【四】

2019-11-18 15:00:42
字體:
來源:轉載
供稿:網友

  狀態會話Bean基礎

Stateful session Bean可以一對一的維持某個調用客戶的狀態,并且在不同的方法調用中維持這個狀態, 由于對于每一個并發用戶,必須有一個對應的Stateful Session Bean,為了提高系統的效率,Stateful Session Bean可以在一定的客戶空閑時間后被寫入二級存儲設備(如硬盤),在客戶發出新的調用請求后,再從二級存儲 設備恢復到內存中。但是在多用戶下,Stateless Session Bean運行效率高于Stateful Session Bean。
javax.ejb.EnterPRiseBean接口繼續了java.io.Serializable,用以實現寫入讀出操作。
當EJB容器調用ejbPassivate()方法鈍化了bean之后,就可以把它寫入二級存儲設備,然后容器調用ejbActivate()方法激活bean,把它從二級存儲設備中讀出。

狀態bean的鈍化過程
計數bean的遠程接口
遠程接口定義了一個業務方法count(),它將在企業bean類中實現。


激活狀態bean
package com.wiley.compBooks.roman.session.count;
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* These are CountBean´s business logic methods.
*
* This interface is what clients Operate on when they
* interact with EJB objects. The container vendor will
implement this interface; the implemented object is
* the EJB Object, which delegates invocations to the
* actual bean.
*/
public interface Count extends EJBObject {
/**
* Increments the int stored as conversational state
*/
public int count() throws RemoteException;
}
Source Count.java

package com.wiley.compBooks.roman.session.count;
import javax.ejb.*;
/**
* Demonstration Stateful Session Bean. This bean is
* initialized to some integer value and has a business
* method that increments the value.
*
* This example shows the basics of how to write a stateful
* session bean and how passivation/activation works.
*/
public class CountBean implements SessionBean {
private SessionContext ctx;
// The current counter is our conversational state.
public int val;
//
// Business methods
//
/**
* Counts up
*/
public int count() {
System.out.println("count()");
return ++val;
}
//
// EJB-required methods
//
public void ejbCreate(int val) throws CreateException {
this.val = val;
System.out.println("ejbCreate()");
}
public void ejbRemove() {
System.out.println("ejbRemove()");
}
public void ejbActivate() {
System.out.println("ejbActivate()");
}
public void ejbPassivate() {
System.out.println("ejbPassivate()");
}
public void setSessionContext(SessionContext ctx) {
}
}
Source CountBean.java
Bean實現了javax.ejb.SessionBean。所以,它必須定義所有SessionBean定義的方法。
OjbCreate()初始化帶了val的參數。它將作為counter的初始狀態。在鈍化和激活bean的過程中,val變量被保護。

計數bean的home接口
package com.wiley.compBooks.roman.session.count;
import javax.ejb.*;
import java.rmi.RemoteException;
/**
* This is the home interface for CountBean. This interface
* is implemented by the EJB Server´s glue-code tools - the
* implemented object is called the Home Object and serves
* as a factory for EJB Objects.
*
* One create() method is in this Home Interface, which
* corresponds to the ejbCreate() method in the CountBean file.
*/
public interface CountHome extends EJBHome {
/*
* This method creates the EJB Object.
*
* @param val Value to initialize counter to
*
* @return The newly created EJB Object.
*/
Count create(int val) throws RemoteException, CreateException;
}
Source CountHome.java.
計數bean的配置描述符

計數bean的配置描述符
計數bean的環境屬性
生成計數bean的Ejb-jar文件
計數bean的客戶端代碼
package com.wiley.compBooks.roman.session.count;
import javax.ejb.*;
import javax.naming.*;
import java.util.Properties;
/**
* This class is a simple example of client code that invokes
* methods on a simple Stateless Enterprise Bean.
*
* We create 3 EJB Objects in this example, but we allow
* the container to have only 2 in memory. This illustrates how
* beans are passivated to storage.
*/
public class CountClient {
public static void main(String[] args) {
try {
/*
* Get System properties for JNDI initialization
*/
Properties props = System.getProperties();
/*
* Get a reference to the Home Object - the
* factory for EJB Objects
*/
Source CountClient.java
1、需要JNDL初始化上下文
2、使用JNDL定位home接口
3、使用home對象建立3個不同的計數EJB對象,因此也就和三個不同的客戶端建立了會話
4、配置描述符限制了同時只能有兩個bean工作,因此3個bean中一定有鈍化的。在調用ejbPassivate()時,打印一條信息。
5、在每個EJB對象上調用count()方法,調用ejbActivate()方法激活bean,該方法打印一條信息。
6、最后所有的EJB對象被刪除。
package com.wiley.compBooks.roman.session.count;
import javax.ejb.*;
import javax.naming.*;
import java.util.Properties;
/**
* This class is a simple example of client code that invokes
* methods on a simple Stateless Enterprise Bean.
*
* We create 3 EJB Objects in this example, but we allow
* the container to have only 2 in memory. This illustrates how
* beans are passivated to storage.
*/
public class CountClient {
public static void main(String[] args) {
try {
/*
* Get System properties for JNDI initialization
*/
Properties props = System.getProperties();
/*
* Get a reference to the Home Object - the
* factory for EJB Objects
*/
Context ctx = new InitialContext(props);
CountHome home = (CountHome) ctx.lookup("CountHome");
/*
* An array to hold 3 Count EJB Objects
*/
Count count[] = new Count[3];
int countVal = 0;
/*
* Create and count() on each member of array
*/
System.out.println("Instantiating beans...");
for (int i=0; i < 3; i++) {
/*
* Create an EJB Object and initialize
* it to the current count value.
*/
count[i] = home.create(countVal);
/*
* Add 1 and print
*/
countVal = count[i].count();
System.out.println(countVal);
/*
* Sleep for 1/2 second
*/
Thread.sleep(500);
}
/*
* Let´s call count() on each EJB Object to
* make sure the beans were passivated and
* activated properly.
*/
System.out.println("Calling count() on beans...");
for (int i=0; i < 3; i++) {
/*
* Add 1 and print
*/
countVal = count[i].count();
System.out.println(countVal);
/*
* Sleep for 1/2 second
*/
Thread.sleep(500);
}
/*
* Done with EJB Objects, so remove them
*/
for (int i=0; i < 3; i++) {
count[i].remove();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Source CountClient.java
運行客戶端:
對于BEA的WebLogic,執行:
java -Djava.naming.factory.initial=
weblogic.jndi.TengahInitialContextFactory
-Djava.naming.provider.url=
t3://localhost:7001
com.wiley.compBooks.roman.session.count.CountClient
客戶端輸出:
Instantiating beans...
1
2
3
Calling count() on beans...
2
3
4
服務端輸出:
ejbCreate()
count()
ejbCreate()
count()
ejbCreate()
ejbPassivate()
count()
ejbPassivate()
ejbActivate()
count()
ejbPassivate()
ejbActivate()
count()
ejbPassivate()
ejbActivate()
count()
ejbPassivate()
ejbActivate()
ejbRemove()
ejbActivate()
ejbRemove()
ejbRemove()

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南川市| 南丰县| 新绛县| 广德县| 湛江市| 荣成市| 肥城市| 汕头市| 嘉荫县| 绥阳县| 乌拉特前旗| 通城县| 苏尼特左旗| 得荣县| 图片| 德钦县| 留坝县| 常德市| 周口市| 桓台县| 柳河县| 朔州市| 精河县| 德保县| 南乐县| 许昌市| 靖州| 微山县| 通江县| 黄浦区| 瓮安县| 谢通门县| 金塔县| 铜山县| 朔州市| 太白县| 三明市| 汽车| 盘山县| 定边县| 尚志市|