Glassfish初体验之七

从消息队列中接收消息,无疑我们会使用MDB(Message Driven Bean)。
在刚刚发送消息的工程下,我们写MDB接收程序 TestMessageDrivenBean。
package com.test.jms;

import javax.annotation.Resource;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(mappedName = "jms/myQueue")
public class TestMessageDrivenBean implements MessageListener
{
@Resource
  private MessageDrivenContext mdc;

public TestMessageDrivenBean()
{}

public void onMessage(Message inMessage)
{
TextMessage msg = null;

    try {
        if (inMessage instanceof TextMessage) {
            msg = (TextMessage) inMessage;
            System.out.println("\n\n**** MESSAGE BEAN: Message received: " +
                msg.getText());
        } else {
        System.out.println("Message of wrong type: " +
                inMessage.getClass().getName());
        }
    } catch (JMSException e) {
        e.printStackTrace();
        mdc.setRollbackOnly();
    } catch (Throwable te) {
        te.printStackTrace();
    }

}
}

配置文件需要两个,ejb-jar.xml和sun-ejb-jar.xml
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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>MessageBean</ejb-name>
<ejb-class>com.test.jms.TestMessageDrivenBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<resource-ref>
<res-ref-name>jms/myCF</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jms/myQueue</res-ref-name>
<res-type>javax.jms.Queue</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MessageBean</ejb-name>
<method-intf>Bean</method-intf>
<method-name>onMessage</method-name>
<method-params>
<method-param>javax.jms.Message</method-param>
</method-params>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>


sun-ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Application
Server 8.1 EJB 2.1//EN'
'http://www.sun.com/software/appserver/dtds/sun-ejb-jar_2_1-1.dtd'>
<sun-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>MessageBean</ejb-name>
<jndi-name>jms/myQueue</jndi-name>
<resource-ref>
<res-ref-name>jms/myCF</res-ref-name>
<jndi-name>jms/myCF</jndi-name>
<default-resource-principal>
<name>guest</name>
<password>guest</password>
</default-resource-principal>
</resource-ref>
<mdb-connection-factory>
<jndi-name>jms/myCF</jndi-name>
<default-resource-principal>
<name>guest</name>
<password>guest</password>
</default-resource-principal>
</mdb-connection-factory>
</ejb>
</enterprise-beans>
</sun-ejb-jar>

一切准备工作完成,打包。
将上述两个xml配置文件放到META-INF目录下,目录结构为:
TestJMS_fat.jar
   META-INF
            ejb-jar.xml
             sun-ejb-jar.xml
             MANIFEST.MF
   com
       test
            jms
                TestSendMessage.class
                 TestMessageDrivenBean.class
打包好后,将TestJMS_fat.jar扔到 %Glassfish_HOME%/domains/domain1/autodeploy下即可。

这样一个完整的收发JMS消息的程序便算大功告成。
如果我们的mdb打包是一个web工程,在控制台会报错,不用管它,不会影响我们对mdb的使用。
大家可以参考: http://www.jroller.com/vprise/entry/trying_glassfish_again
这篇文章就是说的如果我们的mdb是打包的web工程,将会出现的错误。

你可能感兴趣的:(xml,ejb,jms,sun,Glassfish)