文章出處

一,Remote EJB

服務接口定義:

1 package yjmyzz.ejb.server.helloworld;
2 
3 public interface HelloWorldService {
4     
5     public String sayHello(String name);
6 
7 }

實現:

 1 package yjmyzz.ejb.server.helloworld;
 2 
 3 import javax.annotation.security.RolesAllowed;
 4 import javax.ejb.Remote;
 5 import javax.ejb.Stateless;
 6 
 7 @Stateless
 8 @Remote(HelloWorldService.class)
 9 @RolesAllowed({ "guest" })
10 public class HelloWorldBean implements HelloWorldService {
11 
12     public String sayHello(String name) {
13         return "hello , " + name + " , welcome to EJB's world!";
14     }
15 
16 }

注意: @RoleAllowed({"guest"}) 該注解表示只有guest這個角色的用戶才能調用HelloWorldBean

對應maven的pom.xml內容如下:

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yjmyzz</groupId>
    <artifactId>ejb-server-helloworld</artifactId>
    <version>1.0</version>
    <packaging>ejb</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>
        <version.jboss.spec.javaee.6.0>3.0.2.Final</version.jboss.spec.javaee.6.0>
        <version.ejb.plugin>2.3</version.ejb.plugin>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.source>1.6</maven.compiler.source>
    </properties>

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

    <dependencies>

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

        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>

        <finalName>${project.artifactId}</finalName>
        <plugins>

            <plugin>
                <groupId>org.jboss.as.plugins</groupId>
                <artifactId>jboss-as-maven-plugin</artifactId>
                <version>${version.jboss.maven.plugin}</version>
                <configuration>
                    <filename>${project.build.finalName}.jar</filename>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>${version.ejb.plugin}</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                    <generateClient>true</generateClient>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

 

 

二,EJB Client

 1 package yjmyzz.ejb.client.helloworld;
 2 
 3 import java.util.Hashtable;
 4 
 5 import javax.naming.Context;
 6 import javax.naming.InitialContext;
 7 import javax.naming.NamingException;
 8 
 9 import yjmyzz.ejb.server.helloworld.HelloWorldService;
10 
11 
12 public class EjbClientApp {
13     public static void main(String[] args) throws NamingException {
14         System.out.println(lookupRemoteBean().sayHello("jimmy"));
15     }
16 
17     @SuppressWarnings("unchecked")
18     private static HelloWorldService lookupRemoteBean() throws NamingException {
19         @SuppressWarnings("rawtypes")
20         final Hashtable jndiProperties = new Hashtable();
21         jndiProperties.put(Context.URL_PKG_PREFIXES,
22                 "org.jboss.ejb.client.naming");
23         final Context context = new InitialContext(jndiProperties);
24         return (HelloWorldService) context
25                 .lookup("ejb:/ejb-server-helloworld/HelloWorldBean!"
26                         + HelloWorldService.class.getName());
27     }
28 }

 注: lookupRemoteBean用于查找遠程EJB.

jboss環境中,需要在classpath路徑下放置jboss-ejb-client.properties文件,內容參考下面:

1 remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
2 remote.connections=default
3 remote.connection.default.host=localhost
4 remote.connection.default.port = 4447
5 remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
6 remote.connection.default.username=msgUser
7 remote.connection.default.password=Password1!

注: msgUser為jboss中創建的一個application user,而且屬于guest組(不熟悉jboss下創建用戶的朋友,可參考JMS + jboss EAP 6.2 示例 中的相關內容)

對應maven項目的pom.xml為:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yjmyzz</groupId>
    <artifactId>ejb-client-helloworld</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>ejb-client-helloworld</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>yjmyzz</groupId>
            <artifactId>ejb-server-helloworld</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.1_spec</artifactId>
            <version>1.0.1.Final-redhat-2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.1_spec</artifactId>
            <version>1.0.2.Final-redhat-2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jboss-ejb-client</artifactId>
            <version>1.0.21.Final-redhat-1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.xnio</groupId>
            <artifactId>xnio-nio</artifactId>
            <version>3.0.7.GA-redhat-1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.sasl</groupId>
            <artifactId>jboss-sasl</artifactId>
            <version>1.0.3.Final-redhat-1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.marshalling</groupId>
            <artifactId>jboss-marshalling-river</artifactId>
            <version>1.3.16.GA-redhat-1</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

 

示例源碼: remote-ejb-with-security-sample.zip

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()