新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Java, J2SE, J2ME, J2EE, 以及Eclipse, NetBeans, JBuilder等Java开发环境,还有JSP, JavaServlet, JavaBean, EJB以及struts, hibernate, spring, webwork2, Java 3D, JOGL等相关技术。
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Java/Eclipse 』 → 史上最简单的EJB3示例教程[转帖] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4967 个阅读者  浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 史上最简单的EJB3示例教程[转帖] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     hongjunli 帅哥哟,离线,有人找我吗?魔羯座1978-1-20
      
      
      威望:5
      头衔:为振兴论坛而努力!
      等级:研二(中了一篇WWWC Poster)(版主)
      文章:808
      积分:7964
      门派:IEEE.ORG.CN
      注册:2006/3/9

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hongjunli发送一个短消息 把hongjunli加入好友 查看hongjunli的个人资料 搜索hongjunli在『 Java/Eclipse 』的所有贴子 引用回复这个贴子 回复这个贴子 查看hongjunli的博客楼主
    发贴心情 史上最简单的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


       收藏   分享  
    顶(0)
      





    关闭广告显示
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/11/30 9:11:00
     
     GoogleAdSense魔羯座1978-1-20
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Java/Eclipse 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/3/9 2:44:49

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    11,873.050ms