JMS和Message driven bean【转】

这边对初学者理解JMS和MDB比较有帮助。

JMS(Java Message Service)即消息服务。是由sun公司提出,定义java程序访问企业消息传递系统的接口,是J2EE平台上实现面向消息的中间件的规范。面向消息的中间件系统是一种异步通讯系统。非java平台的面向消息的中间件系统也有很多,比较常见的有IBM的MQSeries,Microsoft的MSMQ。
Message driven bean是EJB2.0规范新增加的一种bean,这样就使EJB框架具备了异步消息通讯机制。Message driven bean在JMS系统中扮演消息的消费者(consumer),也就是消息的监听者,而一个客户端着扮演消息的生产者(productor)即消息的发送者。当一个客户端向一个队列或主题发送一个消息后,EJB容器将调用相应的Message driven bean,对收到的消息进行处理,对bean的调用是自动进行的,所要做的只是把消息发送到队列或主题即可。
在此不多说JMS和Message driven bean的详细情况,详细知识请到sun的java网站上取得,或在网上搜索。在这里说明一个简单例子的实现过程。本例子用到的工具软件application server:weblogic 6.1,jdk:jdk1.3。
要想运行一个Message driven bean,先要在weblogic控制台做一些设定。
1.Connection factory的设定
打开weblogic控制台,在左边的Form中选择mydomain/Service//JMS/Connection factory,在右边将会显示Connection factory的配置画面,然后在该画面中选择new JMS Connection Factory configuration。
名字:TestConnFactory
JNDI名:jmstest.JMSConnFactory
其他选项取默认值,点击做成按钮。
  2.JMS的服务器设置
    (1)在左边的Form中选择mydomain/Service//JMS/server,在右边将显示的配置画面,点击new JMSServer configuration。


名字:MyJMSServer
点击作成按钮即作成了server。
(2)然后在左边的form中就可以看到MyJMSServer这一项,下面有destination子项,点击打开,看到destination的设定画面。点击new JMSQueue configuration或者new JMSTopic configuration,这次我们选new JMSTopic configuration。

名字:MyJMSTopic
JNDI名:jmstest.JMSTopic
点击作成按钮。

到此,JMS的服务器端就配置好了。
    先说一下用JMS API做的一个例子。在JMS系统中,消息传递模型分两种,点对点模型(point-to-point model,PTP)和发布/订阅模型(publish and subscribe,pub/sub)。关于这两种模型的详细资料,请在网上查询。下面的例子用的是发布/订阅模型,对于点对点模型处理几乎一模一样,只是用到的有些方法名称不一样。
1. 消息发布程序
package jmstest;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/

import java.io.*;
import javax.jms.*;
import javax.naming.*;

public class TPublisher {

    public static void main(String[] args) {

        new TPublisher().publish();
    }

    public void publish() {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        try {
            //Prompt for JNDI names
            System.out.println("Enter TopicConnectionFactory name:");
            String factoryName = reader.readLine();
            System.out.println("Enter Topic name:");
            String topicName = reader.readLine();

            //Look up administered objects
            Context initContext = Util.getInitialContext();
            TopicConnectionFactory factory =
                (TopicConnectionFactory) initContext.lookup(factoryName);
            Topic topic = (Topic) initContext.lookup(topicName);
            initContext.close();

            //Create JMS objects
            TopicConnection connection = factory.createTopicConnection();
            TopicSession session =
                connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            TopicPublisher publisher = session.createPublisher(topic);
            // set persistant
            publisher.setDeliveryMode(DeliveryMode.PERSISTENT);

            //Send messages
            String messageText = null;
            while (true) {
                System.out.println("Enter message to send or 'quit':");
                messageText = reader.readLine();
                if ("quit".equals(messageText))
                    break;
                TextMessage message = session.createTextMessage(messageText);
                publisher.publish(message);
            }
            System.out.println("Exiting...");
            reader.close();
            connection.close();
            System.out.println("Goodbye!");

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

2.消息订阅程序
package jmstest;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/

import java.io.*;
import javax.jms.*;
import javax.naming.*;

public class TSubscriber implements MessageListener {

    private boolean stop = false;

    public static void main(String[] args) {

        new TSubscriber().subscribe();
    }

    public void subscribe() {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        try {
            //Prompt for JNDI names
            System.out.println("Enter TopicConnectionFactory name:");
            String factoryName = reader.readLine();
            System.out.println("Enter Topic name:");
            String topicName = reader.readLine();
            reader.close();

            //Look up administered objects
            Context initContext = Util.getInitialContext();
            TopicConnectionFactory factory =
                (TopicConnectionFactory) initContext.lookup(factoryName);
            Topic topic = (Topic) initContext.lookup(topicName);
            initContext.close();

            //Create JMS objects
            TopicConnection connection = factory.createTopicConnection();
            TopicSession session =
                connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            TopicSubscriber subscriber = session.createSubscriber(topic);
            subscriber.setMessageListener(this);
            connection.start();

            //Wait for stop
            while (!stop) {
                Thread.sleep(1000);
            }
            System.out.println("Exiting...");
            connection.close();
            System.out.println("Goodbye!");

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void onMessage(Message message) {

        try {
            String msgText = ((TextMessage) message).getText();
            System.out.println(msgText);
            if ("stop".equals(msgText))
                stop = true;
        } catch (JMSException e) {
            e.printStackTrace();
            stop = true;
        }
    }
}
打开消息订阅程序和消息发布程序,输入我们在上面配置好的connection factory名和topic名,即可通过消息发布程序发送字符消息,在消息订阅程序程序中可以接收到。

    下面再说一下message driven bean的例子。message driven bean在JMS系统中扮演的是消息消费者的角色,即一个message driven bean相当于我们上面例子中的Tsubscriber,只不过它不需要手工调用,由服务器自动调用,也就是说我们只要向某个主题发送消息就可以了,消息将由服务器调用相应的message driven bean去处理。

1.MsgDrvTest.java

package msgdrvbeantest;

/**
* Title:
* Description:
* Copyright:    Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/

import weblogic.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import javax.naming.InitialContext;
import javax.naming.NamingException;

public class MsgDrvTest implements MessageDrivenBean, MessageListener{
    private MessageDrivenContext m_context;
    private static final boolean VERBOSE = true;

    public MsgDrvTest() {
    }

    private void log(String s) {
        if (VERBOSE) System.out.println(s);
    }

    public void ejbActivate() {
        log("ejbActivate called");
    }

    public void ejbRemove() {
        log("ejbRemove called");
    }

    public void ejbPassivate() {
        log("ejbPassivate called");
    }

    public void setMessageDrivenContext(MessageDrivenContext ctx) {
        log("setMessageDrivenContext called");
        m_context = ctx;
    }

    public void ejbCreate () throws CreateException {
        log("ejbCreate called");
    }

    /////
    // MessageListener complement,we should do something at here
    //

    public void onMessage(Message msg) {
        TextMessage tm = (TextMessage) msg;
        try {
            String text = tm.getText();
            log("Received new message : " + text);
        }
        catch(JMSException ex) {
            ex.printStackTrace();
        }
    }

    //
    // MessageListener complement
    /////
}

2. ejb-jar.xml

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar>
<enterprise-beans>
    <message-driven>
      <ejb-name>TestMsgDrvBean</ejb-name>
      <ejb-class>msgdrvbeantest.MsgDrvTest</ejb-class>
      <transaction-type>Container</transaction-type>
      <message-driven-destination>
      <!-- destination type: javax.jms.Topic or javax.jms.Queue -->
        <destination-type>javax.jms.Topic</destination-type>
      </message-driven-destination>
    </message-driven>
</enterprise-beans>
</ejb-jar>

3.weblogic-ejb-jar.xml

<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN" "http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd">

<weblogic-ejb-jar>
  <weblogic-enterprise-bean>
    <ejb-name>TestMsgDrvBean</ejb-name>
    <message-driven-descriptor>
      <pool>
        <max-beans-in-free-pool>20</max-beans-in-free-pool>
        <initial-beans-in-free-pool>1</initial-beans-in-free-pool>
      </pool>
      <!-- destination-jndi-name is that we configured at JMS server -->
      <destination-jndi-name>jmstest.JMSTopic</destination-jndi-name>
    </message-driven-descriptor>
    <jndi-name>TestMsgDrvBean</jndi-name>
  </weblogic-enterprise-bean>
</weblogic-ejb-jar>
编译MsgDrvTest.java文件,然后用ejbc,生成jar文件,然后将其发布到weblogic中即可。
测试程序可以用上面的例子中的Tpublisher,输入我们在上面配置好的connection factory名和topic名,然后就可以输入字符消息,比如输入一个单词hello,那么在weblogic的控制台就应该可以看到“Received new message : hello”

你可能感兴趣的:(bean,中间件,weblogic,ejb,jms)