程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 教您如何成為 EJB 專家詳解系列連載之四

教您如何成為 EJB 專家詳解系列連載之四

編輯:關於JAVA

Stateful Session Beans可以一對一的維持某個調用客戶的狀態,並且在不同的方法調用中維持這個狀態,由於對於每一個並發用戶,必須有一個對應的Stateful Session Beans,為了提高系統的效率,Stateful Session Beans可以在一定的客戶空閒時間後被寫入二級存儲設備(如硬盤),在客戶發出新的調用請求後,再從二級存儲 設備恢復到內存中。

但是在多用戶下,Stateless Session Beans運行效率高於Stateful Session Beans。Javax.ejb.EnterpriseBeans接口繼承了Java.io.Serializable,用以實現寫入讀出操作。當EJB容器調用ejbPassivate()方法鈍化了Beans之後,就可以把它寫入二級存儲設備,然後容器調用ejbActivate()方法激活Beans,把它從二級存儲設備中讀出。

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

激活狀態Beans:

package com.wiley.compBooks.roman.session.count;

import Javax.ejb.*;

import Java.rmi.RemoteException;

/**

* These are CountBeans′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 Beans.

*/

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 Beans. This Beans 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 Beans and how passivation/activation works.

*/

public class CountBeans implements SessionBeans {

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 CountBeans.Java

Beans實現了Javax.ejb.SessionBeans。

所以,它必須定義所有SessionBeans定義的方法。

OjbCreate()初始化帶了val的參數。

它將作為counter的初始狀態。

在鈍化和激活Beans的過程中,val變量被保護。

計數Beans的home接口

package com.wiley.compBooks.roman.session.count;

import Javax.ejb.*;

import Java.rmi.RemoteException;

/**

* This is the home interface for CountBeans. 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 CountBeans 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.

計數Beans的配置描述符

計數Beans的配置描述符

計數Beans的環境屬性

生成計數Beans的Ejb-jar文件

計數Beans的客戶端代碼

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 Beans.

*

* We create 3 EJB Objects in this example, but we allow

* the container to have only 2 in memory. This illustrates how

* Beanss 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、配置描述符限制了同時只能有兩個Beans工作,

因此3個Beans中一定有鈍化的。在調用ejbPassivate()時,打印一條信息。

5、在每個EJB對象上調用count()方法,

調用ejbActivate()方法激活Beans,該方法打印一條信息。

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 Beans.

*

* We create 3 EJB Objects in this example, but we allow

* the container to have only 2 in memory. This illustrates how

* Beanss 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 Beanss...");

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 Beanss were passivated and

* activated properly.

*/

System.out.println("Calling count() on Beanss...");

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 Beanss...

1

2

3

Calling count() on Beanss...

2

3

4

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