Skip to content

JavaEE_Simplest_EJB3_Tutorial

Iacob edited this page Nov 8, 2010 · 1 revision

史上最简单的EJB3示例教程

  1. Stateless Bean

    • 客户端在每次调用的时候服务器都使用相应的Bean生成一个Object来执行相关的操作,但服务器可能会使用不同的Object来响应同一客户端的调用,在此期间无法保证执行响应某个客户端调用的是同一个Object。这就好比让快递公司送包裹,客户只知道包裹是否送到,并不知道也不必知道是哪个投递员负责自己的包裹。

    • 注: 某些应用服务器会根据用户指定的数量在内存中为某些Stateless Bean预先生成一定数量的Object以提高服务的响应速度。

下面是Stateless Bean的例子:

部署在服务器端的Bean

KennyBean.java (注: 此文件名改为KennyImpl.java比较合适)

Toggle line numbers

1 package test.ejb;
2 import javax.ejb.Stateless;
3 @Stateless // 指出面向客户端的是一个Stateless Bean
4 public class KennyBean implements Kenny {
5     public String say() {
6         return "Hello";
7     }
8 }

Kenny.java

Toggle line numbers

1 package test.ejb;
2 import javax.ejb.Remote;
3 @Remote // 面向远程客户端的接口,相反的是@Local,面对本地
4 public interface Kenny { // 此Bean面向客户端的接口
5     String say();
6 }

客户端

Toggle line numbers

 1 package test.ejb;
 2 import javax.naming.InitialContext;
 3 import javax.naming.Context;
 4 import java.util.Properties;
 5 public class Main {
 6     public static void main(String[] args) {
 7         try {
 8             Properties props = new Properties();
 9             props.put("org.omg.CORBA.ORBInitialHost", "localhost"); // EJB服务器地址
10             props.put("org.omg.CORBA.ORBInitialPort", "37000"); // EJB服务器端口
11             InitialContext context = new InitialContext(props);
12             Kenny kenny = (Kenny)context.lookup(Kenny.class.getName()); // 通过JNDI获取Bean接口
13             System.out.println("Say something Kenny.");
14             System.out.println("Kenny: ".concat(kenny.say()));
15         }catch(Exception e) {
16             System.err.println("Error: ".concat(e.getMessage()));
17         }
18     }
19 }

运行结果

Say something Kenny. Kenny: Hello

II. Stateful Bean

  • 一次会话可以有多次操作,在会话期间总能保证客户端能让服务器上的Bean认出自己,这类Bean就叫做Stateless Bean。 将上面的例子稍加改动:

部署在服务器端的Bean

KennyBean.java (注: 此文件名改为KennyImpl.java比较合适)

Toggle line numbers

 1 package test.ejb;
 2 import javax.ejb.Stateful;
 3 import javax.ejb.Init;
 4 import javax.ejb.Remove;
 5 @Stateful // 指出这是一个Stateful Bean
 6 public class KennyBean implements Kenny {
 7     public String say() {
 8         return "Hello";
 9     }
10     @Init // 初始化Bean的方法
11     public void create() {
12         System.out.println("Kenny comes.");
13     }
14     @Remove // 销毁Bean的方法
15     public void remove() {
16         System.out.println("Kenny leaves.");
17     }
18
19 }

Kenny.java

Toggle line numbers

1 package test.ejb;
2 import javax.ejb.Remote;
3 @Remote // 远端借口
4 public interface Kenny {
5     String say();
6     void create();
7     void remove();
8
9 }

客户端

Toggle line numbers

 1 package test.ejb;
 2 import javax.naming.InitialContext;
 3 import javax.naming.Context;
 4 import java.util.Properties;
 5 public class Main {
 6     public static void main(String[] args) {
 7         try {
 8             Properties props = new Properties();
 9
10             props.put("org.omg.CORBA.ORBInitialHost", "localhost"); // EJB服务器的地址
11             props.put("org.omg.CORBA.ORBInitialPort", "3700"); // EJB服务器的端口
12
13             InitialContext context = new InitialContext(props);
14             Kenny kenny = (Kenny)context.lookup(Kenny.class.getName()); // 获取Bean
15             kenny.create(); // 初始化
16             System.out.println("Say something Kenny.");
17             System.out.println("Kenny: ".concat(kenny.say()));
18             kenny.remove(); // 销毁Bean
19         }catch(Exception e) {
20             System.err.println("Error: ".concat(e.getMessage()));
21         }
22     }
23 }

运行结果

客户端处显示

Say something Kenny. Kenny: Hello

服务器端显示

Kenny comes. Kenny leaves.

III. Message Driven Bean

  • 送过去一个信息,让Bean执行某项操作。

部署在服务器端的Bean

KennyInBox.java

Toggle line numbers

 1 package test.message.ejb;
 2 import javax.ejb.MessageDriven;
 3 import javax.jms.Message;
 4 import javax.jms.MessageListener;
 5 import javax.annotation.Resource;
 6 import javax.ejb.MessageDrivenContext;
 7 @MessageDriven(mappedName = "jms/Queue")
 8 public class KennyInBox implements MessageListener {
 9
10     @Resource
11     MessageDrivenContext mdc;
12
13     public KennyInBox() {
14     }
15     public void onMessage(Message message) {
16         System.out.println("Kenny received your message.");
17     }
18
19 }

客户端

Toggle line numbers

 1 package test.message.ejb.client;
 2 import javax.jms.Connection;
 3 import javax.jms.QueueConnectionFactory;
 4 import javax.jms.MessageProducer;
 5 import javax.jms.Session;
 6 import javax.jms.Queue;
 7 import javax.jms.TextMessage;
 8 import javax.naming.InitialContext;
 9 import java.util.Properties;
10 public class Main {
11     public static void main(String[] args) {
12
13         Connection connection = null;
14         Session session = null;
15         MessageProducer messageProducer = null;
16         TextMessage message = null;
17
18         try {
19             Properties props = new Properties();
20
21             props.put("org.omg.CORBA.ORBInitialHost", "localhost");
22             props.put("org.omg.CORBA.ORBInitialPort", "3700");
23
24             InitialContext context = new InitialContext(props);
25             QueueConnectionFactory connectionFactory = (QueueConnectionFactory)context.lookup("jms/QueueFactory");
26             Queue queue = (Queue)context.lookup("jms/Queue");
27             connection = connectionFactory.createConnection();
28             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
29             messageProducer = session.createProducer(queue);
30             message = session.createTextMessage();
31             message.setText("Hello");
32             messageProducer.send(message);
33             System.out.println("Message sent.");
34         }catch(Exception e) {
35             System.err.println("Error: ".concat(e.getMessage()));
36         }finally {
37             try {
38                 connection.close();
39             }catch (Exception e0) {
40                 System.out.println("Error: ".concat(e0.getMessage()));
41             }
42             System.exit(0);
43         }
44     }
45 }

客户端处显示

Message sent.

服务器端显示

Kenny received your message.

(待续…​)

注: 笔者使用的工作环境

  • JDK 1.6.0 *

      GlassFish 9.1
    * Netbeans 6.0 Beta 2

None: SimplestEJB3Tutorial (last edited 2008-05-13 23:36:54 by Iacob)