1. 程式人生 > >EJB3.0環境搭建+HelloWorld實現(JBoss7.1.1_Eclipse Juno)

EJB3.0環境搭建+HelloWorld實現(JBoss7.1.1_Eclipse Juno)

I、環境搭建

一、所需環境

1、 JDK1.6 及以上版本)

2、 Eclipse(本人使用 Juno 版)

二、安裝 JBoss

1、 將下載的壓縮檔案解壓到本地目錄,注意:解壓目錄不要包含中文以及空格等字元。

2、 配置 JBoss 環境變數。變數名:JBOSS_HOME,變數值:第一步中的解壓目錄。(此

步驟不是必須的,但是建議進行配置,否則以後開啟 Eclipse 後會出現無法找到 JBoss

環境變數的提示)。

3

三、安裝 JBoss Tools

● Juno  版 : 使用  Eclipse Marketplace  安裝  JBoss Tools 

。 具 體 操 作 : help->Eclipse

image

Marketplace->搜尋 JBoss Tools,單擊 Install,根據提示安裝。

Indigo 版本:

1、下載 JBoss Tools:下載地址:

2、具體操作:help->Install New Software->Add->Archive 選擇剛才下載的.zip 檔案。根

據提示進行安裝。

四、配置 JBoss

1、轉到 Server 選項卡:

image

4、 右鍵點選 New->Server

5、 選擇預設的 JBoss As 7.1 ,點選 finish.

image

6、 右鍵點選新建的 

JBoss 伺服器->Start,啟動伺服器。

image

7、用瀏覽器訪問 http://127.0.0.1:8080/,若出現下圖,則說明配置成功。

IIHello World 實現

1、 新建工程:

image

image

注意:EJB module version 一欄請選擇 3.0 版本。

點選 Next,將 Generate ejb-jar.xml deployment descriptor 一欄勾上,以便由 Eclipse 自動建立

xml 部署檔案。

image

點選完成。完成 EJB 工程的建立。

2、 新增客戶端所需的 Jar 包。配置構建路徑,在 Libraries 選項卡單擊新增外部 Jar,選擇

JBoss 安裝目錄 jboss-as-7.1.1.Final\bin\client 下的 jboss-client.jar 檔案,完成 Jar 檔案的

新增。

image

image

3、 建立 Session Bean。在 ejbModule 根目錄下,建立 session bean 3.x

image

image

包名為 com.jerrylab.business,類名為 HelloWorldBean,型別為 Stateless,選中 Remote

完成 Session Bean 的建立。

此時 IDE 會自動完成兩個 java 檔案的建立。其中 HelloWorldBeanRemote.java 是遠端接

口,HelloWorldBean 是介面的實現。

image

在 HelloWorldBeanRemote.java 中新增以下程式碼:

package com.jerrylab.business;

import javax.ejb.Remote;

@Remote

public interface HelloWorldBeanRemote {

public String sayHello();

}

在 HelloWorldBean.java 中新增以下程式碼:

package com.jerrylab.business;

import javax.ejb.Stateless;

/**

* Session Bean implementation class HelloWorldBean

*/

@Stateless

public class HelloWorldBean implements HelloWorldBeanRemote {

/**

* Default constructor.

*/

public HelloWorldBean() {

// TODO Auto-generated constructor stub

}

@Override

public String sayHello()

{

// TODO Auto-generated method stub

return "Hello World!!!";

}

}

4、 將 EJB 部署到 JBoss 伺服器。選中 JBoss 伺服器,右鍵點選 Add and Remove…

image

image

新增 HelloWorldEJB。點選完成。

image

控制檯中出現下列文字,說明 EJB 部署成功。

5、 建立客戶端工具類。

所有的命名服務都是在 javax.naming.Context 介面的實現上完成的。

新建一個叫做 ClientUtility 的類,包名 com.jerry.clientutility

image

新增下列程式碼:

package com.jerrylab.clientutility;

import java.util.Properties;  import javax.naming.Context;  import javax.naming.InitialContext;

import javax.naming.NamingException;

public class ClientUtility {

private static Context initialContext;

private static final String PKG_INTERFACES =

"org.jboss.ejb.client.naming";

public static Context getInitialContext() throws NamingException {

if (initialContext == null) {

Properties properties = new Properties(); properties.put(Context.URL_PKG_PREFIXESPKG_INTERFACES);

initialContext new InitialContext(properties);

}

return initialContext;

}

}

6、建立客戶端類。

建立一個遠端的 Java 程式(帶有 main()方法)來訪問部署到伺服器中的 EJB

image

類名Client,包名com.jerrylab.client

新增以下程式碼:

package com.jerrylab.client;

import javax.naming.Context;

import javax.naming.NamingException;

import com.jerrylab.business.HelloWorldBean;

import com.jerrylab.business.HelloWorldBeanRemote;

import com.jerrylab.clientutility.ClientUtility;

public class Client {

public static void main(String[] args) { HelloWorldBeanRemote bean = doLookup();

System.out.println(bean.sayHello()); // 4. Call business logic

}

private static HelloWorldBeanRemote doLookup() { Context context = null;

HelloWorldBeanRemote bean = nulltry {

// 1. Obtaining Context

context = ClientUtility.getInitialContext();

// 2. Generate JNDI Lookup name

String lookupName = getLookupName();

// 3. Lookup and cast

bean = (HelloWorldBeanRemote) context.lookup(lookupName);

catch (NamingException e) { e.printStackTrace();

}

return bean;

}

private static String getLookupName() {

/*

The app name is the EAR name of the deployed EJB without .ear suffix. Since we haven't deployed the application as a .ear,

the app name for us will be an empty string

*/

String appName = "";

/* The module name is the JAR name of the deployed EJB without the .jar suffix.

*/

String moduleName = "HelloWorldSessionBean";

/*AS7 allows each deployment to have an (optional) distinct name. This can be an empty string if distinct name is not specified.

*/

String distinctName = "";

// The EJB bean implementation class name

String beanName = HelloWorldBean.class.getSimpleName();

// Fully qualified remote interface name

final String interfaceName = HelloWorldBeanRemote.class.getName();

// Create a look up string name

String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + interfaceName;

return name;

}

}

7、配置 EJB 客戶端上下文屬性。

在 ejbModule 目錄下建立一個 jboss-ejb-client.properties 檔案,並在其中新增以下幾行:

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false remote.connections=default

remote.connection.default.host=localhost remote.connection.default.port = 4447

remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOAN ONYMOUS=false

8、執行 Client.java,型別為 Java Application

控制檯中輸出:“Hello World!!”(如果執行不成功,可以嘗試重啟 JBoss 伺服器)