JBoss中发布EJB 并编写Client

第一步:编写EJB服务端 项目名 appName= ejbserver

public interface Handler {
    public String say(String name);
}

@Remote(Handler.class)
@Stateless //无状态bean
public class HandlerBean implements Handler {
    @Override
    public String say(String name) {
        return "你好,"+name;
    }
}

将此项目部署到 JBoss中

第二步:编写Client 调用接口中的 say()方法 创建项目 ejbclient

1、将Handler.java 接口导出为一个jar文件添加到 ejbclient项目中,

2、因为编写的是另外一个项目(和EJB程序不在同一个JVM中)远程调用EJB,需要将%JBOSS_HOME%/bin/client/jboss-client.jar添加到类路径下

否则会出javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter 例外

3、

public class Client {

   public static void main(String[] args) {

    String lookupName = getLookupName();

    try {
            Context context = ClientUtility.getInitialContext();
            Handler handler  = (Handler) context.lookup(lookupName);
            System.out.println(handler.say("张三"));
        } catch (Exception e) {
                e.printStackTrace();
        }

  }

 

/**

For stateless beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>

For stateful beans:

ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>?stateful

*/

   private static String getLookupName() {

        String appName = "ejbserver";  //app名

       String moduleName = "";

        String distinctName = "";

        String beanName = "HandlerBean";//实现类名 

      

        String  qualifiedInterName =  Handler.class.getName();//完整接口名

        String jndi = "ejb:/" + appName+"/"+distinctName +"/" + beanName + "!" + viewClassName;

       //ejb:/ejbserver//HandlerBean!com.ejb.inter.Handler

//注意如果这里jndi name错了,

//会No EJB receiver available for handling [appName:,modulename:ejb01,distinctname:]

/ /javax.ejb.NoSuchEJBException: No such EJB

        return jndi;

   }

}


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;
    }
}

4、配置客户端上下文属性

在类路径下下 创建 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=root//安装Jboss的密码 root 1234
remote.connection.default.password=1234

至此即可 运行成功

参考1百度文库:http://wenku.baidu.com/link?url=phjFT-9Y03RtAFoqDMgKOvqdk3XdrGghYQBge4ZzC9W3t0fjkVkzJLQ0d92_KaJ5MS9mNa2yIDFNG65XmKfvoHgVAY-TyLrTLunGUIaZWve

参考2:https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值