gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:36:27 27 4
gpt4 key购买 nike

我是 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]

最佳答案

方法

<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/ )。

关于ActiveMQ、Wildfly 和获取消息体(getBody),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com