目录
- 演示环境
- JBoss 配置
- 创建EJB工程
- 部署
- 创建客户端工程
- 测试
[一]、演示环境
- Eclipse Juno(4.2)
- Java 1.7.0_25
- JBoss AS (5/6/7)
[二]、JBoss 配置
eclipse默认支持到JBoss 5.0,如果需要更高版本JBoss AS,可以去官方下载Eclipse对应版本的压缩包离线安装即可,当然也支持在线安装插件, JBoss Tools 下载地址:http://www.jboss.org/tools/download.html
本文就以自带的JBoss5.0 为例图文演示EJB3.0工程构建过程。
1. 下载JBoss AS
JBoss官网 http://www.jboss.org/jbossas/downloads下载 JBoss AS 5.0.1的版本,下载后解压缩到目录:/Users/micmiu/workspace_eclipse/jboss-5.0.1.GA
2.配置 Server Runtime Environments
eclipse 依次打开 Preference → Server → Runtime Environments → Add… 配置新环境,如下图:
3.创建Server
eclipse 依次打开 File → New → Other → Server → JBoss v5.0
Server runtime environment 选择 上面第二步中配置的环境即可,【Next】配置端口
默认即可。
[三]、创建EJB工程
依次点击 File → New → Other … → EJB → EJB Project 如下图
【Next】→【Next】(选中 Generate ejb-jar.xml deployment descriptor)→【Finish】。
如上图中生成的目录结构中,ejbModule目录是编写JAVA源码的目录(即EJB代码)。
在ejbModule 目录上右键【New】→【Other…】→【EJB】→ Session Bean(EJB 3.x)
→【Next】输入包名:com.micmiu.ejb、EJB类名:HelloWorld ,选中 Remote选项 →【Finish】.
自动生成两个类:HelloWorldRemote.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.micmiu.ejb; import javax.ejb.Remote; /** * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-21 下午1:55:51 * @version 1.0 */ @Remote public interface HelloWorldRemote { } |
HelloWorld.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.micmiu.ejb; import javax.ejb.Stateless; /** * Session Bean implementation class HelloWorld * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-21 下午1:55:40 * @version 1.0 */ @Stateless public class HelloWorld implements HelloWorldRemote { /** * Default constructor. */ public HelloWorld() { // TODO Auto-generated constructor stub } } |
修改接口 HelloWorldRemote.java 添加sayHello(String username) 方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.micmiu.ejb; import javax.ejb.Remote; /** * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-21 下午1:55:51 * @version 1.0 */ @Remote public interface HelloWorldRemote { String sayHello(String username); } |
修改实现类 HelloWorld.java ,实现sayHello(String username)方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.micmiu.ejb; import javax.ejb.Stateless; /** * Session Bean implementation class HelloWorld * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-21 下午1:55:40 * @version 1.0 */ @Stateless public class HelloWorld implements HelloWorldRemote { /** * Default constructor. */ public HelloWorld() { // TODO Auto-generated constructor stub } @Override public String sayHello(String username) { return "Hello," + username + " welcome to EJB."; } } |
[四]、部署
部署的办法有两种:
1.在eclipse中发布
在之前创建好的Server中,右击 Add and Remove … 添加这个项目,然后 Start即可
2.打包后再部署到指定server下
在工程上右键→【Export】→【EJB JAR file】 Destination处:→【Browse…】→ 选择JBoss服务器的部署目录:JBOSS_HOME/server/default/deploy (以JBoss 5.0.1GA为例)→【完成】即可
[五]、创建客户端工程
1. 点击 File → New → Other … → Java Project
创建一个 EJB-HelloWorld-Client 的工程。
2. 把EJB-HelloWorld 工程中的接口类 【Export】 jar包,添加到当前客户端工程classpath中。
3. 添加Jboss Client的Jar包($JBOSS_HOME/clent/jbossall-clent.jar)到classpath中,如下图
4.创建客户端测试类
HelloWorldClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.micmiu.ejb.demo; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; import com.micmiu.ejb.HelloWorldRemote; /** * * @author <a href="http://www.micmiu.com">Michael</a> * @time Create on 2013-9-21 下午2:51:59 * @version 1.0 */ public class HelloWorldClientTest { /** * @param args */ public static void main(String[] args) { 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"); System.out.println(">>>> InitialContext "); InitialContext ctx = new InitialContext(props); System.out.println(">>>> lookup "); String serviceName = "HelloWorld/remote"; HelloWorldRemote helloWorld = (HelloWorldRemote) ctx .lookup(serviceName); System.out.println(">>>> 调用sayHello(\"micmiu.com\")"); String result = helloWorld.sayHello("micmiu.com"); System.out.println(">>>> 结果 = " + result); } catch (NamingException e) { e.printStackTrace(); } } } |
[六]、测试
先运行EJB-HellowWorld 工程,日志如下:
14:46:37,439 INFO [JBossASKernel] Created KernelDeployment for: EJB-HelloWorld.jar
14:46:37,441 INFO [JBossASKernel] installing bean: jboss.j2ee:jar=EJB-HelloWorld.jar,name=HelloWorld,service=EJB3
14:46:37,441 INFO [JBossASKernel] with dependencies:
14:46:37,441 INFO [JBossASKernel] and demands:
14:46:37,442 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
14:46:37,442 INFO [JBossASKernel] and supplies:
14:46:37,442 INFO [JBossASKernel] jndi:HelloWorld/remote-com.micmiu.ejb.HelloWorldRemote
14:46:37,442 INFO [JBossASKernel] jndi:HelloWorld/remote
14:46:37,442 INFO [JBossASKernel] Class:com.micmiu.ejb.HelloWorldRemote
14:46:37,442 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=EJB-HelloWorld.jar,name=HelloWorld,service=EJB3) to KernelDeployment of: EJB-HelloWorld.jar
14:46:37,512 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EJB-HelloWorld.jar,name=HelloWorld,service=EJB3
14:46:37,521 INFO [EJBContainer] STARTED EJB: com.micmiu.ejb.HelloWorld ejbName: HelloWorld
14:46:37,550 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
HelloWorld/remote – EJB3.x Default Remote Business Interface
HelloWorld/remote-com.micmiu.ejb.HelloWorldRemote – EJB3.x Remote Business Interface
14:46:37,602 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8080
14:46:37,631 INFO [AjpProtocol] Starting Coyote AJP/1.3 on ajp-127.0.0.1-8009
14:46:37,643 INFO [ServerImpl] JBoss (Microcontainer) [5.0.1.GA (build: SVNTag=JBoss_5_0_1_GA date=200902231221)] Started in 19s:412ms
再运行客户端的测试类,日志如下:
>>>> InitialContext
>>>> lookup
>>>> 调用sayHello(“micmiu.com”)
>>>> 结果 = Hello,micmiu.com welcome to EJB.
本文介绍到此结束@Michael Sun.
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/j2ee/ejb/eclipse-jboss-ejb-demo/
你用的是哪种IDE编辑器啊?怎么那么好看
报错啊,怎么解决啊,javax.naming.NoInitialContextException:,大家都会遇到的报错啊;
受益匪浅,谢谢。