EJB中包含有三种bean
会话bean (session bean)
与客户端交互,编写业务逻辑,可以通过jdbc直接操作数据库,但通常用通过实体bean来操作数据库
无状态会话bean 无活保存某一用户的值
有状态会话bean
实体bean (entity bean)
属于java持久化规范里的(jpa)技术,
消息驱动bean (message-driven bean)
专门用于异步处理消息的组件,有处理大量并发消息的能力
开发第一个无状态会话bean,示例代码如下
将web 应用打包
jar cvf 应用名.war
ant打包
===============================================================================
session beans(会话bean)
stateless(无状态的)slsb
示例代码如下
/**
* 包含所有业务的接口
*
*@time 11:29:51 PM
*@author retacn yue
*@Email zhenhuayue@sina.com
*/
public interface HelloWorld {
publicString sayHello(String name);
}
/**
* 无状态会话bean
*
* 就是一个简单的pojo
*
*@time 11:26:54 PM
*@author retacn yue
*@Email zhenhuayue@sina.com
*/
// 声明这是一个无状态会话bean
@Stateless
// 声明这个bean的remote接口
@Remote( { HelloWorld.class })
public class HelloWorldBean implementsHelloWorld {
publicString sayHello(String name) {
returnname + "hello world!";
}
}
开发一个EJB客户端示例代码如下:
需要添加bin\client\jboss-client.jar
/**
*Copyright (C) 2016
*
*FileName:EJBClient.java
*
*Author:<a href="mailto:zhenhuayue@sina.com">Retacn</a>
*
*CreateTime: 2016-7-17
*/
// Package Information
package cn.yue.ejb.test;
import cn.yue.ejb.bean.HelloWorld;
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBClient {
/**
* @param args
* @throws NamingException
*/
public static void main(String[] args) throws NamingException {
/*
* 7.1之前的实现方法 try { Properties props = new Properties();
* props.setProperty("java.naming.factory.initial",
* "org.jnp.interfaces.NamingContextFactory");
* props.setProperty("java.naming.provider.url","localhost:1099"); //
*props.setProperty("java.naming.factory.url.pkgs", //
* "org.jboss.naming"); InitialContext ctx; ctx = new
* InitialContext(props); HelloWorld helloworld = (HelloWorld)
* ctx.lookup("HelloWorldBean/romote");
* System.out.println(helloworld.SayHello("retacn")); } catch
* (NamingException e) { e.printStackTrace(); }
*/
HelloWorld helloWorld = lookupRemoteStatelessEjbBean();
System.out.println(helloWorld);
System.out.println(helloWorld.SayHello("retacn"));
}
/**
* @return
* @throws NamingException
*/
private static HelloWorld lookupRemoteStatelessEjbBean() throwsNamingException {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
// 如果服务器布署的是.ear包,则appname为.ear之后的包名,如果的war或是jar包,appName可以为空,如果是有状态bean,在namespace后面添加?stateful
final String appName = "";
final String moduleName = "HelloWorldEJB";
final String distinctName = "";
final String beanName = "HelloWorldBean";
final String viewClassName = HelloWorld.class.getName();
final String namespace = "ejb:" + appName + "/" +moduleName + "/" + distinctName + "/"
+ beanName + "!" +viewClassName;
System.out.println("namespace:" + namespace);
return (HelloWorld) context.lookup(namespace);
}
}
在客户端测试程序的src目录下添加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=sa
remote.connection.default.password=sa
运行结果如下:
namespace:ejb:/HelloWorldEJB//HelloWorldBean!cn.yue.ejb.bean.HelloWorld
七月 18, 2016 11:16:30 上午org.jboss.ejb.client.EJBClient <clinit>
INFO: JBoss EJB Client version1.0.5.Final
Proxy for remote EJBStatelessEJBLocator{appName='', moduleName='HelloWorldEJB', distinctName='',beanName='HelloWorldBean', view='interface cn.yue.ejb.bean.HelloWorld'}
七月 18, 2016 11:16:30 上午org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
七月 18, 2016 11:16:30 上午org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version3.0.3.GA
七月 18, 2016 11:16:30 上午 org.jboss.remoting3.EndpointImpl<clinit>
INFO: JBoss Remoting version 3.2.3.GA
七月 18, 2016 11:16:31 上午org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: Received server version 1 andmarshalling strategies [river]
七月 18, 2016 11:16:31 上午 org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiverassociate
INFO: Successful version handshakecompleted for receiver contextEJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@514577,receiver=Remoting connection EJB receiver [connection=Remoting connection<278a47>,channel=jboss.ejb,nodename=retacn]} on channel Channel IDc4627bd2 (outbound) of Remoting connection 0198339a to localhost/127.0.0.1:4447
七月 18, 2016 11:16:31 上午org.jboss.ejb.client.remoting.ChannelAssociation$ResponseReceiver handleMessage
WARN: Unsupported message received withheader 0xffffffff
retacn say:hello world! this is myfirst EJB test!
查看ejb的JNDI,可以通过以下方式:
打开http://localhost:9990/,选择profile页签,选择container下的naming,选择页面中的java:global就可以看到我们的应用
把jboss集成到eclipse中,需要下载插件
使用ant打包 ejb项目,示例代码如下:
<?xml version="1.0"encoding="UTF-8"?>
<projectname="HelloWorldEJB"basedir=".">
<!--变量定义-->
<propertyname="src.dir"value="${basedir}\src"/>
<!--环境变量-->
<propertyenvironment="env"/>
<propertyname="jboss.home"value="${env.JBOSS_HOME}"/>
<propertyname="jboss.server.config"value="default"/>
<propertyname="build.dir"value="${basedir}\WebContent\WEB-INF\classes"/>
<propertyname="export.dir"value="${basedir}\WebContent"/>
<!--添加jar包-->
<pathid="build.classpath">
<filesetdir="${jboss.home}\bin\client">
<includename="*.jar"/>
</fileset>
<pathelementlocation="${build.dir}"/>
</path>
<!--初始化工作-->
<targetname="prepare">
<deletedir="${build.dir}"/>
<mkdirdir="${build.dir}"/>
</target>
<!--编 译-->
<targetname="compile"depends="prepare"description="编译">
<javacsrcdir="${src.dir}"destdir="${build.dir}"includeantruntime="false">
<classpathrefid="build.classpath"/>
</javac>
<copyfile="${src.dir}\jboss-ejb-client.properties"todir="${build.dir}"/>
</target>
<!--应用打包-->
<targetname="ejbwar"depends="compile"description="创建EJB发布包">
<wardestfile="${basedir}\${ant.project.name}.war">
<filesetdir="${export.dir}">
<includename="**/*"/>
</fileset>
</war>
</target>
<!--发布应用-->
<targetname="deploy"depends="ejbwar"description="发布EJB">
<copyfile="${basedir}\${ant.project.name}.war"todir="${jboss.home}\standalone\deployments"/>
</target>
<!--卸载应用-->
<targetname="undeploy"description="卸载ejb">
<deletefile="${jboss.home}\standalone\deployments${ant.project.name}.war"/>
</target>
</project>