gpt4 book ai didi

java - 使用 JMS ObjectMessage 时发生 ClassNotFound 异常

转载 作者:行者123 更新时间:2023-12-01 17:39:42 25 4
gpt4 key购买 nike

我正在通过 Spring Boot 编写的监听器使用 JMS ObjectMessage。我通过 Camel 应用程序将 ObjectMessage 发送到 ActiveMQ 队列,并从 Spring Boot 应用程序中的监听器类监听该队列。

代码:

final BindyCsvDataFormat bindy = new BindyCsvDataFormat(EquityFeeds.class);
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
CamelContext _ctx = new DefaultCamelContext();
_ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
_ctx.addRoutes(new RouteBuilder() {

public void configure() throws Exception {
from("file:src/main/resources?fileName=data.csv")
.unmarshal(bindy)
.split(body())
.streaming().to("jms:queue:javaobjects.upstream.queue");
}
});

这是我的 POJO 类 EquityFeeds:

@CsvRecord(separator = ",",skipFirstLine = true)
public class EquityFeeds implements Serializable {

private static final long serialVersionUID = 1L;

@DataField(pos = 1)
private String externalTransactionId;

@DataField(pos = 2)
private String clientId;

// other fields and getters setters
...

在 ActiveMQ 队列中,我收到的消息为:

camelproject.EquityFeeds@1665425

我在 Spring Boot 中的 JMS 监听器:

@JmsListener(destination = "javaobjects.upstream.queue")
public void capitalIQProcessor(final Message objectMessage) throws JMSException {
Object messageData = null;

if(objectMessage instanceof ObjectMessage) {
ObjectMessage objMessage = (ObjectMessage) objectMessage;
System.out.println("Starting Object Message.");

Object object = objMessage.getObject();

EquityFeeds equityFeeds = (EquityFeeds) object;

System.out.println("Object: "+equityFeeds.toString());
}
}

我收到一个异常@ line Object object = objMessage.getObject();:

capitalIQProcessor(javax.jms.Message) throws javax.jms.JMSException' threw exception; nested exception is javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: camelproject.EquityFeeds. Caused by: javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: camelproject.EquityFeeds. Caused by: java.lang.ClassNotFoundException: camelproject.EquityFeeds

通过 Stack Overflow 和网上的其他帖子,我进行了以下更改:

  1. 我对 wrapper.conf (apache-activemq-5.15.11-bin\apache-activemq-5.15.11\bin\win64) 进行了更改

    1. 添加了条目:wrapper.java.classpath.3=C:\Users\sidbharg\eclipse-workspace\Sid\target\camelproject-0.0.1-SNAPSHOT.jar这是我的 POJO (EquityFeeds) 的 .jar 文件的位置。
    2. 还添加了以下条目:wrapper.java.additional.13=-Dorg.apache.activemq.SERIALIZABLE_PACKAGES="*"
  2. 在我的消费者代码中,我还添加了 activeMQConnectionFactory.setTrustAllPackages(true);:

public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerURL);
activeMQConnectionFactory.setTrustAllPackages(true);
return activeMQConnectionFactory;
}

现在有什么问题吗?为什么它给我异常(exception)。我哪里错了?有什么我忘记了需要补充的吗?

最佳答案

您需要在使用 JMS ObjectMessage 的应用程序的类路径上使用 camelproject.EquityFeeds。显然 JVM 找不到它,因此抛出 java.lang.ClassNotFoundException

此外,您不需要对代理的 wrapper.conf 进行这些更改。 监听器正在尝试反序列化消息,而不是代理。

关于java - 使用 JMS ObjectMessage 时发生 ClassNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60976647/

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