JBoss EAP SessionBean的调用

首先是Server端的开发及设置:
1、增加一个用户:

bin\add-user.bat

用户名密码随便,但要属于guest组

2、启动Server

standalone.bat -server-config=standalone-full.xml 

3、新建java项目
3.1、写SessionBean的接口
ITest.java

package com.neohope.ejb.test;

import javax.ejb.Remote;

/**
 * Created by Hansen
 */

@Remote
public interface ITest{
    /**
     * 返回“Hi XXX"字符串,必须声明抛出RemoteException异常
     * @return 返回“Hi XXX"字符串
     */
    public String sayHiTo(String user);

    /**
     * 加法,必须声明抛出RemoteException异常
     * @param a
     * @parma b
     * @return a+b
     */
    public int add(int a, int b);
}

3.2、写一个无状态的SessionBean
SessionTestBean.java

package com.neohope.ejb.test;

import javax.ejb.Stateless;

/**
 * Created by Hansen
 */
@Stateless(name = "SessionTestEJB")
public class SessionTestBean implements ITest{
    public SessionTestBean() {
    }

    /**
     * 返回“Hi XXX"字符串,必须声明抛出RemoteException异常
     * @return 返回“Hi XXX"字符串
     */
    @Override
    public String sayHiTo(String user) {
        return "Hi " + user;
    }

    /**
     * 加法,必须声明抛出RemoteException异常
     * @param a
     * @parma b
     * @return a+b
     */
    @Override
    public int add(int a, int b) {
        return a+b;
    }
}

3.3、打jar包,或者自己压缩为TestEJBServer.jar

4、新建一个ear项目
4.1增加application.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd"
             version="6">
    <description>EAR Test</description>
    <display-name>EAR Test</display-name>
    <module>
        <ejb>TestEJBServer.jar</ejb>
    </module>
    <library-directory>lib</library-directory>
</application>

4.2打ear包,或自己压缩为TestEar.ear

4.3TestEar的结构为:

│  TestEJBServer.jar
│
├─lib
└─META-INF
        application.xml

5.通过EAP进行部署,部署后,会输出绑定结果

java:global/TestEar/TestEJBServer/SessionTestEJB!com.neohope.ejb.test.ITest
java:app/TestEJBServer/SessionTestEJB!com.neohope.ejb.test.ITest
java:module/SessionTestEJB!com.neohope.ejb.test.ITest
java:jboss/exported/TestEar/TestEJBServer/SessionTestEJB!com.neohope.ejb.test.ITest
java:global/TestEar/TestEJBServer/SessionTestEJB
java:app/TestEJBServer/SessionTestEJB
java:module/SessionTestEJB

到这里服务端已经完成了。

然后是客户端的设置:
1、在scr下新增配置文件jboss-ejb-client.properties

endpoint.name=client-endpoint
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
remote.connection.default.username=user001
remote.connection.default.password=user001#

2、通过ejb方式访问

package com.neohope.ejb.test;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

/**
 * Created by Hansen
 */
public class TestSessionBean{
    public static void main(String[] args) throws NamingException, JMSException {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

        final Context ctx = new InitialContext(jndiProperties);
        final String appName = "TestEar";
        final String moduleName = "TestEJBServer";
        final String distinctName = "";
        final String beanName = "SessionTestEJB";
        final String viewClassName = ITest.class.getName();
        final String namespace = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
        System.out.println(namespace);
        ITest test =  (ITest)ctx.lookup(namespace);

        System.out.println(test.sayHiTo("neohope"));
        System.out.println(test.add(1,2));
    }
}

3、通过JMS方式访问

package com.neohope.ejb.test;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

/**
 * Created by Hansen
 */
public class TestSessionBean{
    public static void main(String[] args) throws NamingException, JMSException {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProperties.put(Context.PROVIDER_URL, "remote://localhost:4447");
        final InitialContext ctx = new InitialContext(jndiProperties);

        ConnectionFactory factory = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
        Connection connection = factory.createConnection("user001", "user001#");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        ITest test = (ITest) ctx.lookup("TestEar/TestEJBServer/SessionTestEJB!com.neohope.ejb.test.ITest");

        session.close();
        connection.close();
        System.out.println(test.sayHiTo("neohope"));
        System.out.println(test.add(1,2));
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*