Jboss EJB实现实例

使用eclipse进行开发,创建一个普通的java工程,导入java-client.jar,这个jar包在bin/client目录下

1. 定义一个接口

public interface HelloWorldRemote {
   String sayHelloRemote();
}
2.定义一个ejb,实现这个接口,并且指定这个接口为remote接口
import javax.ejb.Remote;
import javax.ejb.Stateless;


@Stateless
@Remote(HelloWorldRemote.class)
public class HelloWorldRemoteBean implements HelloWorldRemote {


    @Override
    public String sayHelloRemote() {
        // TODO Auto-generated method stub
        
          return "say remote";


    }

}


如果指定HelloWorldRemote 为local接口,那么在remote client就无法访问这个ejb

Local接口和remote接口也不可以是同一个接口



3.导出这些class为jar包,jar包名为helloworld1,部署到jboss中
4.客户端访问这个ejb
        Properties props = new Properties();
        props.put("java.naming.factory.initial","org.jboss.naming.remote.client.InitialContextFactory");
        props.put("java.naming.provider.url", "remote://localhost:4447");
        props.put("jboss.naming.client.ejb.context",true);
        try{
            InitialContext ctx = new InitialContext(props);
            HelloWorldRemote helloworld =  (HelloWorldRemote)(ctx.lookup("helloworld1//HelloWorldRemoteBean!com.alex.wang.HelloWorldRemote"));
            System.out.println(helloworld.sayHelloRemote());
        }catch(NamingException e){
            System.out.println("get error");
            System.out.println(e);

        }

这里不能使用ejb:helloworld1//HelloWorldRemoteBean!com.alex.wang.HelloWorldRemote来访问

否则报Exception in thread "main" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:helloworld1, moduleName:, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@1af691b

原因应该是jndi前缀名为ejb:,但是我们没有指定其对应的interceptor



5.服务端访问这个ejb
在部署ejb的时候,server.log可以看出在server里每个ejb绑定的jndi,例如部署了一个helloworldbean,实现了helloworld接口
在jboss的server.log中,可以看出其绑定的jndi为
java:global/helloworld/HelloWorldBean!com.alex.wang.HelloWorld
java:app/helloworld/HelloWorldBean!com.alex.wang.HelloWorld
java:module/HelloWorldBean!com.alex.wang.HelloWorld
java:global/helloworld/HelloWorldBean
java:app/helloworld/HelloWorldBean
java:module/HelloWorldBean
那么在server中调用这个ejb,可以使用这样的方式
    InitialContext ctx = new InitialContext();

    HelloWorld world =(HelloWorld)ctx.lookup("java:global/helloworld1/HelloWorldBean!com.alex.wang.HelloWorld");

根据调用ejb与被调用ejb位置的关系,我们可以采用不同的jndi来进行调用(java:global,java:app,java:module)

上述的试验也说明在ejb3中,无需定义ejb-jar.xml

为什么通过4447端口可以获得ejb呢

原因是在jboss server的standalone.xml中定义了remoting模块

 <extension module="org.jboss.as.remoting"/>

其相关的配置为

  <subsystem xmlns="urn:jboss:domain:remoting:1.1">
            <connector name="remoting-connector" socket-binding="remoting" security-realm="ApplicationRealm"/>
  </subsystem>

其指定绑定端口为remoting

在<socket-binding-group中了定义其对应4447端口

<socket-binding name="remoting" port="4447"/>

而在  <subsystem xmlns="urn:jboss:domain:ejb3:1.3">,定义了其remote 访问使用的是前面定义的remoting-connector

 <remote connector-ref="remoting-connector" thread-pool-name="default"/>

所以通过4447端口,remote 协议可以访问到ejb

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值