JBOSS和EJB学习一

1.使用软件

IDE:Eclipse4.3(开普勒) EE版本

服务器:jboss EAP 6.2

eclipse-jboss plugin:jbosstools-Update-4.1.2.Final_2014-03-18_15-46-19-B706

2.服务端开发步骤

  创建EJB项目——创建EJB sessionBean接口(具体先创建的是stateless sessionBean,接口上添加注解@Remote)——实现此接口(添加注解@Stateless,@LocalBean),LocalBean与RemoteBean的区别,statefulSessionBean与statelessSessionBean的区别今后再学习。部署到Jboss上,服务器端的工作完成了

3.客户端调用EJB

  新建普通Java项目,导入EJB3的库,以及外部jar包C:jboss-eap-6.2inclientjboss-client.jar——拷贝第2步写的远程接口到本项目中,创建 jboss-ejb-client.properties文件,文件内容为

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

文件必须置于classpath根目录下(注意,放子包中不行)——通过JNDI调用,JNDI的name格式为

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

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

注意<module-name>为部署的项目的名字

部分代码:

Hashtable<String, String> jndiProperties = new Hashtable<String, String>();

jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

try {    

Context context = new InitialContext(jndiProperties);

final String appName = "";
final String moduleName = "myejbservertwo"; //部署的EJB项目的名字
final String distinctName = "";

String interfaceName = HelloWorldRemote.class.getName();
String implBeanName = "HelloWorld";
String before_name = "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/";
String name = before_name+implBeanName+"!"+interfaceName;
HelloWorldRemote hwr = (HelloWorldRemote)context.lookup(name);
String say = hwr.sayHello("yhuso");

System.out.println(say);

4.EJB一般分为以下三种:

SessionBean——为客户端服务,通过EntityBean访问DB,可以有状态,也可无状态

EntityBean——用来实现ORM,可以被分配,序列化,可通过网络传输

MessageDrivenBean(消息驱动Bean)——基于JMS消息,可看成异步无状态的SessionBean

5.SLSB(无状态会话Bean是EJB最有价值的部分之一)与门面设计模式Facade

原文地址:https://www.cnblogs.com/enjoy-ourselves/p/3672330.html