EJB 3:Session beans

EJB 3 : Session beans

Session beans type

Session beans 有兩種: stateful and stateless.

  • stateful session bean 能自動儲存 bean 的狀態(state).也就是說 container 讓每一個client擁有自己的 session bean. 典型的範例為 shopping cart.
  • stateless session bean 故名思義為將不會管理狀態, container 不保證 client 會於 session bean pool 中取到上次所使用的 bean. 典型的範例為 checking customer credit history.

A session bean can be invoked either locally or remotely using Java RMI. Astateless session bean can be exposed as a web service.

 

The programming rules

以下為所有的 session beans 所需依照的規則:

  • A session bean 至少要有一個 business interface.
  • The session bean class 不能為 final or abstract 否則 container 無法使用它.
  • A session bean class 必須要有一個 no-argument constructor. 因為 container 利用 no-argument constructor 來 create a bean instance.( Note that the compiler inserts a default noargument constructor if there is no constructor in a Java class.)
  • A session bean class 可以繼承其他 session bean or POJO.

For example, a stateless session bean named BidManager can extend
another session bean PlaceBidBean in the following way:
@Stateless
public BidManagerBean extends PlaceBidBean implements BidManager {

}

  • A session bean class 可以繼承其他 session bean 所定義的 lifecycle callback methods 的 annotations 及 resource injections.
  • Business method 的名稱不能開頭為“ejb”.例如 "ejbCreate" or "ejbAddBid".因為可能會影響EJB內基礎操作
  • 所有的 business methods 必須為 public 且不能為 final or static.
  • 如果預期 method 是一個 remote business interface of the EJB ,則 arguments 及 return type 是為實作 the java.io.Serializable interface.

 

Coding the Enterprise Bean

The enterprise bean in this example needs the following code:
■ Remote business interface
■ Enterprise bean class

撰寫 Business Interface
Business interface 用來定義給 Client 端呼叫的 business methods. The business methods are implemented in the enterprise bean class. The source code for the Converter remote
business interface follows.

package com.starbooks.model;

import javax.ejb.Remote;

/* Note the @Remote annotation decorating the interface definition. This lets the container know that ConverterBean will be accessed by remote clients.*/

@Remote
public interface IDChecker {
     public boolean check(String id);
}

撰寫 Enterprise Bean Class
The enterprise bean class for this example is called
IDCheckerBean . This class implements the business methods ( check ) that the IDChecker remote business interface defines. The source code for the IDCheckerBean class follows.

package com.starbooks.model;

import javax.ejb.Stateless;
import com.starbooks.model.IDChecker;

/*
Note the @Stateless annotation decorating the enterprise bean class. This lets the container know that ConverterBean is a stateless session bean.
*/

@Stateless
public class IDCheckerBean implements IDChecker {

    @Override
    public boolean check(String id) {
        if(id.length()==10){
            return true;
        }
        return false;
    }

}

 

Testing the Enterprise Bean

開發測試環境:

  1. Java SE Development Kid 6 Update 11 : Java Development Kid
  2. Eclipse-jee-ganymede(Eclipse Platform Version: 3.4.2) : Java IDE Tool
  3. JBOSS AS 5.0.1GA : JAVA EE5 Application Server

在 Eclipse 建立 EJB 3.0 Service Project

 在 Eclipse 功能表裡的 File 中選擇 New ,Project,EJB,選擇 EJB Project,

image

點選Next,輸入 Project Name,Target RunTime 請選擇 JBoss 5.0 Runtime,EJB Model version 請選擇 3.0,Configuration 請選擇 Default Configuaration for JBoss 5.0 Runtime.

image

其餘設定使用預設值即可,點選 Finish 即完成 EJB 3.0 Project 的建立.

 

在 EJB 3.0 Service Project 中建立 Session Bean

1.在 EJBSservice Project 中選取 New > Other >  EJB3 Session Bean,點選 Next 鍵

image

2. Session Bean Type 我們選擇 Stateless ,Package 輸入 com.starbooks.model ,Bean Name 輸入 IDChecker ,工具會自動幫我們建立 Bean Class Name 為 IDCheckerBean 而 Remote Interface Name 為 IDChecker,點選 Finish 即完成. image 

3.在 EJBSservice Project 中選取 Run As > Run As Server  ,選擇執行的 Server ,點選 Finish 即可完成部署啟動.

image

image 

4.當 Consol 顯示如下Log即完成於 JBoss Server 的部署及啟動

image

 

在 Eclipse 建立 EJB 3.0 App Client Project

 在 Eclipse 功能表裡的 File 中選擇 New ,Project,Java EE,選擇 Application Client Project,

image

點選Next,輸入 Project Name,Target RunTime 請選擇 JBoss 5.0 Runtime,EJB Model version 請選擇 3.0,Configuration 請選擇 Default Configuaration for JBoss 5.0 Runtime.點選 Next

image 

Source Folder 選擇預設值即可.

image

 

在 EJB 3.0 App Client Project 中建立 Session Bean Client Class

1.將 EJBAppClient Project 與 EJBService Project 能建立關聯,在 EJBAppClient Project 的節點下點選右鍵,選取 Properties ,選擇 Java Bulid Path ,將 EJBService Project 加入即可.

image 

2.在 EJBAppClient Project中選取 New > Class ,Package 輸入 com.starbooks.client ,Name 輸入 IDCheckerClient,點選 Finish 即完成. 

image

程式碼如下: 

image

3.建立 jndi.properties 檔於 EJBAppClient Project 內,內容如下:

image

image

4. 在 IDCheckerClient 節點上選取 Run As > Java Application 結果將會如下:

image

Client Console Log 如下:

image

Server Console Log 如下:

12:08:10,689 WARN  [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
12:08:10,689 WARN  [InterceptorsFactory] EJBTHREE-1246: Do not use InterceptorsFactory with a ManagedObjectAdvisor, InterceptorRegistry should be used via the bean container
12:08:10,689 WARN  [InterceptorRegistry] applicable interceptors is non-existent for public boolean com.starbooks.model.IDCheckerBean.check(java.lang.String)
12:08:10,709 WARN  [InterceptorRegistry] applicable interceptors is non-existent for public boolean com.starbooks.model.IDCheckerBean.check(java.lang.String)

即為成功完成第一個Sample.由此可知,EJB3.0的開發與以往EJB的開發方便不少,這對 Java EE 的開發人員則是一大幫助.

本篇發表於 未分類。將永久鏈結加入書籤。

1 Responses to EJB 3:Session beans

  1. Dragos 說道:

    Instead of using JNDI lookup try using the @EJB annotation in the client. For me it doesn’t work. Does it work for you?

發表留言