ActiveMQ,Wildfly和获取消息体(getBody)

时间:2015-03-26 06:26:36

标签: activemq wildfly-8

我是JEE7的新手并且正在进行一些快速练习,但我遇到了一个问题。我有一个示例Java SE应用程序,它将消息发送到ActiveMQ队列,我在Wildfly 8上部署了一个MDB,它在消息进入时读取。这一切都正常,我可以使用getText接收消息。但是,当我使用getBody来获取消息体时,我得到一个"未知错误"。谁能让我知道我做错了什么?

以下是我的代码;

/***CLIENT CODE****/
    import javax.jms.*;
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;

    public class SimpleMessageClient {
        // URL of the JMS server. DEFAULT_BROKER_URL will just mean
        // that JMS server is on localhost
        private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

        // Name of the queue we will be sending messages to
        private static String subject = "MyQueue";

        public static void main(String[] args) throws JMSException {
            // Getting JMS connection from the server and starting it
            ConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory(url);
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // JMS messages are sent and received using a Session. We will
            // create here a non-transactional session object. If you want
            // to use transactions you should set the first parameter to 'true'
            Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

            // Destination represents here our queue 'TESTQUEUE' on the
            // JMS server. You don't have to do anything special on the
            // server to create it, it will be created automatically.
            Destination destination = session.createQueue(subject);

            // MessageProducer is used for sending messages (as opposed
            // to MessageConsumer which is used for receiving them)
            MessageProducer producer = session.createProducer(destination);

            // We will send a small text message saying 'Hello' in Japanese
            TextMessage message = session.createTextMessage("Jai Hind");

            //Message someMsg=session.createMessage();
           // someMsg.

            // Here we are sending the message!
            producer.send(message);
            System.out.println("Sent message '" + message.getText() + "'");

            connection.close();
        }
    }

和消费者;

    package javaeetutorial.simplemessage.ejb;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.annotation.Resource;
    import javax.ejb.ActivationConfigProperty;
    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(activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "MyQueue")
    })
    public class SimpleMessageBean implements MessageListener {

        @Resource
        private MessageDrivenContext mdc;
        static final Logger logger = Logger.getLogger("SimpleMessageBean");

        public SimpleMessageBean() {
        }

        @Override
        public void onMessage(Message inMessage) {
            try {            
if (inMessage instanceof TextMessage) {
                logger.log(Level.INFO,
                        "MESSAGE BEAN: Message received: {0}",
                        inMessage.getBody(String.class));
            } else {
                logger.log(Level.WARNING,
                        "Message of wrong type: {0}",
                        inMessage.getClass().getName());
            }
            } catch (JMSException e) {
                e.printStackTrace();
                logger.log(Level.SEVERE,
                        "SimpleMessageBean.onMessage: JMSException: {0}",
                        e.toString());
                mdc.setRollbackOnly();
            }
        }
    }

我得到的部分错误是;

16:47:48,510 ERROR [org.jboss.as.ejb3] (default-threads - 32) javax.ejb.EJBTransactionRolledbackException: Unexpected Error
16:47:48,511 ERROR [org.jboss.as.ejb3.invocation] (default-threads - 32) JBAS014134: EJB Invocation failed on component SimpleMessageBean for method public void javaeetutorial.simplemessage.ejb.SimpleMessageBean.onMessage(javax.jms.Message): javax.ejb.EJBTransactionRolledbackException: Unexpected Error
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleInCallerTx(CMTTxInterceptor.java:157) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInCallerTx(CMTTxInterceptor.java:253) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:342) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]

1 个答案:

答案 0 :(得分:4)

方法

<T> T Message.getBody(Class<T> c)

您所指的是JMS 2.0的补充(另请参阅:http://www.oracle.com/technetwork/articles/java/jms20-1947669.html)。

虽然WildFly 8完全符合Java EE 7,因此完全符合JMS 2.1,但当前的ActiveMQ(5.12.0)仍然仅限于JMS 1.1。

由于您可能在SimpleMessageBean中导入了JMS 2.1 API,因此您引用的方法根本不存在于ActiveMQ消息中。

当您尝试在消息上调用getBody()方法时,无法在消息实现中解析它,因此抛出了AbstractMethodError。这导致事务回滚,从而为您提供EJBTransactionRolledbackException。

我为您的问题找到了两个直接的解决方案:

  1. 如果您想继续使用ActiveMQ,请将自己限制在JMS 1.1 API中。您提到的getText()方法是JMS 1.1的一部分,因此可以完美地运行。请参阅此处了解JMS 1.1 API(https://docs.oracle.com/javaee/6/api/javax/jms/package-summary.html),此处查看当前的ActiveMQ API文档(http://activemq.apache.org/maven/5.12.0/apidocs/index.html)。

  2. 切换到符合JMS 2.x的消息代理。由于您使用的是WildFly,我建议您查看HornetQ(http://hornetq.jboss.org/)。