以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Java/Eclipse 』  (http://bbs.xml.org.cn/list.asp?boardid=41)
----  史上最简单的EJB3示例教程[转帖]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=41&rootid=&id=56215)


--  作者:hongjunli
--  发布时间:11/30/2007 9:11:00 AM

--  史上最简单的EJB3示例教程[转帖]

虽然不是很完善,但是可以用来了解下EJB3的大致结构以及同EJB2的不同之处。

http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial

I. Stateless Bean

部署在服务器端的Bean
KennyBean.java


[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1 package test.ejb;
   2 import javax.ejb.Stateless;
   3 @Stateless
   4 public class KennyBean implements Kenny {
   5     public String say() {
   6         return "Hello";
   7     }
   8 }

Kenny.java


[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1 package test.ejb;
   2 import javax.ejb.Remote;
   3 @Remote
   4 public interface Kenny {
   5     String say();
   6
   7 }


客户端

[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   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");
  10             props.put("org.omg.CORBA.ORBInitialPort", "37000");
  11
  12             InitialContext context = new InitialContext(props);
  13             Kenny kenny = (Kenny)context.lookup(Kenny.class.getName());
  14             System.out.println("Say something Kenny.");
  15             System.out.println("Kenny: ".concat(kenny.say()));
  16         }catch(Exception e) {
  17             System.err.println("Error: ".concat(e.getMessage()));
  18         }
  19     }
  20 }


运行结果

Say something Kenny.
Kenny: Hello

II. Stateful Bean
将上面的例子稍加改动


部署在服务器端的Bean
KennyBean.java


[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1
   2 package test.ejb;
   3
   4 import javax.ejb.Stateful;
   5 import javax.ejb.Init;
   6 import javax.ejb.Remove;
   7
   8 @Stateful
   9 public class KennyBean implements Kenny {
  10
  11     public String say() {
  12         return "Hello";
  13     }
  14
  15     @Init
  16     public void create() {
  17         System.out.println("Kenny comes.");
  18     }
  19
  20     @Remove
  21     public void remove() {
  22         System.out.println("Kenny leaves.");
  23     }
  24  
  25 }
  26
  27

Kenny.java


[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1
   2 package test.ejb;
   3
   4 import javax.ejb.Remote;
   5
   6 @Remote
   7 public interface Kenny {
   8
   9     String say();
  10
  11     void create();
  12
  13     void remove();
  14     
  15 }
  16
  17


客户端

[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1
   2 package test.ejb;
   3
   4 import javax.naming.InitialContext;
   5 import javax.naming.Context;
   6 import java.util.Properties;
   7
   8 public class Main {
   9
  10     public static void main(String[] args) {
  11
  12         try {
  13             Properties props = new Properties();
  14             
  15             props.put("org.omg.CORBA.ORBInitialHost", "localhost");
  16             props.put("org.omg.CORBA.ORBInitialPort", "3700");
  17             
  18             InitialContext context = new InitialContext(props);
  19             Kenny kenny = (Kenny)context.lookup(Kenny.class.getName());
  20             kenny.create();
  21             System.out.println("Say something Kenny.");
  22             System.out.println("Kenny: ".concat(kenny.say()));
  23             kenny.remove();
  24         }catch(Exception e) {
  25             System.err.println("Error: ".concat(e.getMessage()));
  26         }
  27     }
  28
  29 }
  30
  31


运行结果
客户端处显示


Say something Kenny.
Kenny: Hello


服务器端显示


Kenny comes.
Kenny leaves.

III. Message Driven Bean

部署在服务器端的Bean
KennyInBox.java

[URL=http://linuxfire.com.cn/firewiki/SimplestEJB3Tutorial#]Toggle line numbers[/URL]
   1
   2 package test.message.ejb;
   3
   4 import javax.ejb.MessageDriven;
   5 import javax.jms.Message;
   6 import javax.jms.MessageListener;
   7 import javax.annotation.Resource;
   8 import javax.ejb.MessageDrivenContext;
   9
  10 @MessageDriven(mappedName = "jms/Queue")
  11 public class KennyInBox implements MessageListener {
  12     
  13     @Resource
  14     MessageDrivenContext mdc;
  15     
  16     public KennyInBox() {
  17     }
  18
  19     public void onMessage(Message message) {
  20         System.out.println("Kenny received your message.");
  21     }
  22     
  23 }
  24


客户端

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

客户端处显示


Message sent.


服务器端显示


Kenny received your message.


(待续...)

--------------------------------------------------------------------------------


注: 笔者使用的工作环境

JDK 1.6.0
GlassFish 9.1

Netbeans 6.0 Beta 2


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms