消息驱动Bean(MDB)的开发

来源:互联网 发布:自动开票软件 编辑:程序博客网 时间:2024/04/30 19:43

开发的基本过程包括:

u       创建EJB工程

u       创建消息驱动Bean

u       创建客户端程序

u       运行测试

2、创建EJB工程

选择【FileàNew Project】,在弹出的界面上选择【EnterpriseàEJB Module】,在弹出的界面中输入工程的名字“MDBTest”,完成工程的创建。

3、创建消息驱动Bean

    在工程上面点击右键,选择【NewàMessage-Driven Bean…】,如下图所示。

在弹出的界面上输入消息驱动Bean的名字,并为Bean类指定包。这里包名为“ch20,类名是“TextMDB”。

创建后的消息驱动Bean的代码如下:

/*

 * TextMDB.java

 *

 * Created on 2007514, 下午7:28

 *

 * To change this template, choose Tools | Template Manager

 * and open the template in the editor.

 */

 

package ch20;

 

import javax.ejb.ActivationConfigProperty;

import javax.ejb.MessageDriven;

import javax.jms.Message;

import javax.jms.MessageListener;

 

/**

 * Entity class TextMDB

 *

 * @author Administrator

 */

@MessageDriven(mappedName = "jms/TextMDB", activationConfig =  {

        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),

        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")

    })

public class TextMDB implements MessageListener {

   

    /** Creates a new instance of TextMDB */

    public TextMDB() {

    }

 

    public void onMessage(Message message) {

    }

   

}

在类前面的代码:

@MessageDriven(mappedName = "jms/TextMDB", activationConfig =  {

        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),

        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")

    })

声明这个消息驱动Bean使用的目的是“jms/TextMDB”,默认的生成的连接工厂的名字是“jms/TextMDBFactory”。activationConfig声明消息的确认模式和目的的类型(队列)。

onMessage方法中添加对消息处理的代码,这里假设接收到的消息是TextMessage,接收到消息之后把消息输出到控制台。修改onMessage方法如下:

    public void onMessage(Message message) {

        TextMessage msg = (TextMessage)message;

        try{

            System.out.println(msg.getText());

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

    }

部署该EJB应用,在该工程上点击右键选择【Deploy project】,完成消息驱动Bean的部署。

4、创建客户端程序

选择【FileàNew Project】,在弹出的界面中选择【EnterpriseàEnterprise Application Client】,输入客户端工程的名字“mdbClient”。

客户端的代码如下:

/*

 * Main.java

 *

 * Created on 2007514, 下午7:51

 *

 * To change this template, choose Tools | Template Manager

 * and open the template in the editor.

 */

 

package mdbclient;

 

import javax.naming.*;

import javax.jms.*;

 

 

/**

 *

 * @author Administrator

 */

public class Main {

   

    /** Creates a new instance of Main */

    public Main() {

    }

   

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {                         

        try{

            // 创建上下文环境

            Context ctx = new InitialContext();

           

            // 查找连接工厂

            QueueConnectionFactory qConFactory = (QueueConnectionFactory)

            ctx.lookup("jms/TextMDBFactory");

           

            // 查找队列

            javax.jms.Queue messageQueue = (javax.jms.Queue) ctx.lookup("jms/TextMDB");

           

            // 创建连接

            QueueConnection qCon = qConFactory.createQueueConnection();

 

            // 创建会话Bean

            QueueSession session = qCon.createQueueSession(

                    false, /* not a transacted session */

                    Session.AUTO_ACKNOWLEDGE

                    );

            // 创建消息发送者

            QueueSender sender = session.createSender(messageQueue);

           

            // 创建要发送的消息

            TextMessage msg = session.createTextMessage();

           

            // 为消息赋值

            msg.setText("发送给消息驱动Bean的消息!");

           

            System.out.println("开始发送消息!");

            // 发送消息

            sender.send(msg);

           

            qCon.close();

           

            System.out.println("消息发送成功!");

       

        }catch(Exception e){

            System.out.println(e.getMessage());

        }

    }   

}

注意:这里发送消息的连接工厂和目的应该与消息驱动Bean的相同。

5、运行测试

在客户端程序所在的工程上点击右键选择【Run project】。可以在服务器端的控制台上看到消息驱动Bean的输出信息如下图所示:

 
原创粉丝点击