Posted on 2008-07-08 16:53
sailor 阅读(538)
评论(0) 编辑 收藏
开发环境:Glassfish2.0 + Myeclipse6.5
1、新建EJB3工程sessionEjbDemo(图略)
2、建立Caculate类、CaculateLocal接口、CaculateRemote接口
如下:
CaculateRemote
1package com.stateless;
2
3import javax.ejb.Remote;
4/** *//**
5 * 远程接口
6 * @author sailor
7 * Jul 8, 2008 4:44:18 PM
8 */
9@Remote
10public interface CaculateRemote {
11 public int caculateNumber(int x, int y);
12}
CaculateLocal
1package com.stateless;
2
3import javax.ejb.Local;
4/** *//**
5 * 本地接口
6 * @author sailor
7 * Jul 8, 2008 4:43:35 PM
8 */
9@Local
10public interface CaculateLocal {
11 public int caculateNumber(int x, int y);
12}
Caculate
1package com.stateless;
2
3import java.io.Serializable;
4
5import javax.ejb.Stateless;
6
7/** *//**
8 * 业务bean:计算客户端传来的值
9 * @author sailor
10 * Jul 8, 2008 4:42:22 PM
11 */
12
13@Stateless
14public class Caculate implements Serializable, CaculateLocal, CaculateRemote {
15
16 private static final long serialVersionUID = 1L;
17
18 public int caculateNumber(int x, int y){
19
20 int z = x + y;
21
22 System.out.println("caculate result: " + z);
23
24 return z;
25 }
26}
3、部署到Glassfish。
部署后,有两种途径查看JNDI:
一是在控制台查看:
1信息: **RemoteBusinessJndiName: com.stateless.CaculateRemote; remoteBusIntf: com.stateless.CaculateRemote
二是到glassfish管理界面查看
路径:common tasks - Application Server - General - JNDI Brower
4、在glassfish的lib下引入两个包:appserv-rt和javaee.jar
5、编写客户端验证
Client
1/** *//**
2 *
3 */
4package com.stateless;
5
6import javax.naming.InitialContext;
7
8/** *//**
9 * @author sailor
10 * Jul 8, 2008 4:06:02 PM
11 */
12public class Client {
13
14 /** *//**
15 * @param args
16 */
17 public static void main(String[] args) {
18
19 try{
20 InitialContext ic = new InitialContext();
21 CaculateRemote cr = (CaculateRemote)ic.lookup("com.stateless.CaculateRemote");
22 //CaculateRemote cr = (CaculateRemote)ic.lookup("com.stateless.CaculateRemote#com.stateless.CaculateRemote");
23 //两种方式都可以
24 int result = cr.caculateNumber(1, 2);
25 System.out.println("result: " + result);
26 }catch(Exception e){
27 e.printStackTrace();
28 }
29 }
30
31}
32
自此 部署statelse ejb3到glassfish上结束,stateful ejb3也是同样的部署方式。
查考资料:http://www.myeclipseide.com/documentation/quickstarts/ejb3/