一、环境说明
开发环境:Eclipse Java EE IDE for Web Developers Version: Kepler Service Release 2
JBoss环境:jboss-as-7.1.1.Final
jdk版本:1.7.0
二、文档结构
部分过程截图如下
2.1、在ejbModule目录下右键创建session Bean
2.2、为你的session Bean取一个名字,状态这里选择无状态(stateless)的session,并勾上Remote,它会自动帮助我们生成对应的接口
3、后续部分就开始写代码了
三、详细代码
3.1. HelloWorldBeanRemote接口代码
public interface HelloWorldBeanRemote {
public String sayHello();
}
3.2. 无状态的HelloWorldBean 代码
import comjoe.bussiness.view.HelloWorldBeanRemote;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote(HelloWorldBeanRemote.class)
public class HelloWorldBean implements HelloWorldBeanRemote {
public HelloWorldBean() {
}
@Override
public String sayHello() {
return "Hello world!";
}
}
3.3. 客户端调试代码
import javax.naming.NamingException;
import com.joe.bussiness.view.HelloWorldBeanRemote;
import com.joe.clientutility.ClientUtility;
public class Client {
public static void main(String[] args) {
HelloWorldBeanRemote bean = doLookup();
System.out.println(bean.sayHello());
}
private static HelloWorldBeanRemote doLookup() {
HelloWorldBeanRemote bean = null;
try {
bean = ClientUtility.getInitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
}
3.4. 通过jndi方式查找对应的bean
package com.joe.clientutility;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.joe.bussiness.HelloWorldBean;
import com.joe.bussiness.view.HelloWorldBeanRemote;
public class ClientUtility {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static HelloWorldBeanRemote getInitialContext() throws NamingException {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
final String appName = "";
final String moduleName = "HelloWorldEJB";//工程名,这里写错会出现 IllegalStateException
final String distinctName = "";
final String beanName = HelloWorldBean.class.getSimpleName();
final String viewClassName = HelloWorldBeanRemote.class.getName();
System.out.println(moduleName);
System.out.println(beanName);
System.out.println(viewClassName);
/**
* 要搞清楚这几个name的具体含义,可以参见如下论坛
* @see https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t
*/
return (HelloWorldBeanRemote) context.lookup("ejb:"+appName+"/"+moduleName+"/"+
distinctName+"/"+ beanName + "!"+viewClassName);
}
}
3.5. 编写配置信息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=username
#remote.connection.default.password=password
四、运行结果
直接运行client.java,结果截图如下
五、异常处理
刚开始因为对jndi查找bean中几个参数不了解,导致了如下异常
Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available forhandling[appName:,modulename:HelloWorldEJB,distinctname:] combination for invocationcontextorg.jboss.ejb.client.EJBClientInvocationContext@196c67a
官方网站非常详细的说明了几个参数的含义,参考https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t