本文主要是介绍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 qualified
InterName = 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
这篇关于JBoss中发布EJB 并编写Client的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!