使用 JBoss AS 7.1 設定 EJB

1.概述

在本文中,我們將討論如何開始使用 Enterprise JavaBeans(EJB)。我們將使用 JBoss AS 7.1.1.Final,但你可以自由使用你選擇的任何伺服器。

2. Bean 的 Maven 依賴項

為了使用 EJB,請確保將其最新版本新增到 pom.xml 檔案的 dependencies 部分:

<dependency>
    <groupId>org.jboss.spec.javax.ejb</groupId>
    <artifactId>jboss-ejb-api_3.2_spec</artifactId>
    <version>1.0.0.Final</version>
</dependency>

確保正確新增 JBoss 依賴項,因為我們將在本教程中將 JBoss 用作應用程式伺服器。在本教程的後半部分,我們將詳細討論如何為專案設定 maven 構建。

3. EJB Remote

讓我們首先建立名為 HelloWorldRemote 的 Bean 介面。

public interface HelloWorldRemote {
    public String getHelloWorld();
}

現在我們將實現上面的介面並將其命名為 HelloWorldBean

@Stateless
public class HelloWorldBean implements HelloWorldRemote {

    public HelloWorldBean() {
    
    }
    
    @Override
    public String getHelloWorld(){
        return ("Hello World");
    }
}

請注意類宣告頂部的 @Stateless 表示法。它表示無狀態會話 bean。

4.遠端 Bean 的 Maven 設定

在本節中,我們將討論如何設定 maven 以在伺服器上構建和執行應用程式。

讓我們一個一個地看一下外掛。

4.1。編譯器外掛

maven-compiler-plugin 用於編譯專案的原始碼。

這裡我們使用了外掛的 2.3.1 版本,其中源和目標 JDK 在配置下設定為 1.7。

我們已將這些設定定義為標記內的屬性,並通過$ {property}引用它。

<version.compiler.plugin>2.3.1</version.compiler.plugin>
<!-- maven-compiler-plugin -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>

4.2 EJB 外掛

這個外掛生成 Bean 檔案以及相關的客戶端 jar。

我們已將 ejb 版本指定為 3.2,並將 generateClient 屬性設定為 true 以生成客戶端。

4.3 在 JBoss 中部署

jboss-as-maven-plugin 用於在 JBoss AS 7 中部署,重新部署,取消部署或執行​​應用程式。

在此配置中,我們指定構建檔名與專案構建檔名相同,預設情況下,在我們的案例 ejb-remote-1.0-SNAPSHOT 中使用 artifactid-version 格式。

4.4 EJB 的必需 Maven 依賴項

jboss-javaee-6.0 定義了我們想要使用的 JBoss Java EE 6 API 的版本。

JBoss 分發了一整套 Java EE 6 API,包括物料清單(BOM)。

BOM 指定工件的堆疊(或集合)的版本。我們在標籤中指定它,以便我們始終獲得正確的工件版本。此依賴項本身的型別是包含所需依賴項的 pom。

<dependency>
    <groupId>org.jboss.spec</groupId>
    <artifactId>jboss-javaee-6.0</artifactId>
    <version>${version.org.jboss.spec.jboss.javaee.6.0}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

4.5 註釋

以下將獲得註釋依賴項:

<dependency>
    <groupId>org.jboss.spec.javax.annotation</groupId>
    <artifactId>jboss-annotations-api_1.1_spec</artifactId>
    <scope>provided</scope>
</dependency>

4.6 EJB 版本 3.2

在下面的程式碼中,我們獲得了最新版本的規範:

<dependency>
    <groupId>org.jboss.spec.javax.ejb</groupId>
    <artifactId>jboss-ejb-api_3.2_spec</artifactId>
    <version>1.0.0.Final</version>
</dependency>

要在 JBoss 伺服器上執行上述專案,我們需要先執行:

mvn clean install

然後我們需要通過執行以下 maven 命令將它部署到正在執行的 JBoss 伺服器:

jboss-as:deploy

現在你應該看到在 jboss 伺服器中部署了 jar 檔案。

或者,你可以從專案中的目標資料夾中複製可用的 jar,並將其貼上到伺服器的 webapp 資料夾中。

5.設定客戶端專案

在建立遠端 bean 之後,我們應該通過建立客戶端來測試已部署的 bean。

首先讓我們討論專案的 maven 設定。

使用了 5.1 Maven 外掛

maven-compiler-plugin 用於編譯專案的原始碼。

我們為源類和目標類指定了 jdk 1.7 版本。

我們的客戶端是一個 Java 程式,執行它我們使用 exec-maven-plugin 來幫助執行系統和 Java 程式。我們需要指定可執行檔案(即 java),類路徑和 java 類(com.baeldung.ejb.client.Client)。

類路徑保持為空,因為外掛包含基於所提供的依賴項所必需的類路徑引數。

5.2 EJB3 客戶端的 Maven 依賴關係

為了執行 EJB3 客戶端,我們需要包含以下依賴項。

我們依賴於此應用程式的 EJB 遠端業務介面來執行客戶端。所以我們需要指定 ejb 客戶端 jar 依賴項。值為“ejb-client”的標記用於指定此專案對 EJB 客戶端 jar 的依賴性。

<dependency>
    <groupId>com.theopentutorials.ejb3</groupId>
    <artifactId>ejbmavendemo</artifactId>
    <type>ejb-client</type>
    <version>${project.version}</version>
</dependency>

依賴關係 jboss-transaction-api_1.1_specjboss-ejb-api_3.1_specjboss-ejb-clientxnio-apixnio-niojboss-remotingjboss-sasljboss-marshalling-river 具有作為執行時的範圍,因為這些是執行時所需而不是在編譯期間。

dependencyManagement 下的依賴關係 jboss-javaee-6.0jboss-as-ejb-client-bom 的範圍為 import。這用於將來自遠端 POM 的依賴關係管理資訊包括到當前專案中。這些遠端 POM 由 JBoss 提供,其中包含執行客戶端所必需的依賴項。

5.3 JBoss EJB Client 屬性

在“src / main / resources”下建立一個檔案,並將其命名為 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_NOANONYMOUS=false

6.建立客戶端類

首先,我們建立一個 ClientUtility 類:

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_PREFIXES, PKG_INTERFACES);
            initialContext = new InitialContext(properties);
         }
        return initialContext;
    }
}

現在讓我們建立一個實際的 Client 類,它將使用我們在伺服器中部署的 bean:

public class Client {
    
    //The lookup method to get the EJB name
    private static HelloWorldRemote doLookup() {
        Context context = null;
        HelloWorldRemote bean = null;
        try {
            // 1. Obtaining Context
            context = ClientUtility.getInitialContext();
            // 2. Generate JNDI Lookup name
            String lookupName = getLookupName();
            // 3. Lookup and cast
            bean = (HelloWorldRemote) 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 = "ejb-remote-0.0.1-SNAPSHOT";
 
        
        // 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";
 
        // Fully qualified remote interface name
        final String interfaceName = "com.baeldung.ejb.tutorial.HelloWorldRemote";
 
        // Create a look up string name
        String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
                + "/" + beanName + "!" + interfaceName;
        
        return name;
    }
}

Client 類使用 bean 並輸出結果。

7.結論

所以我們建立了一個 EJB 伺服器和一個使用該服務的客戶端。該專案可以在任何 Application Server 上執行。