【问题标题】:ClassNotFound Exception Occured while consuming JMS ObjectMessage使用 JMS ObjectMessage 时发生 ClassNotFound 异常
【发布时间】:2020-07-13 13:01:48
【问题描述】:

我正在通过用 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. 我对@9​​87654330@(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;
}

现在有什么问题?为什么它给了我例外。我哪里错了?有什么我忘记了需要补充的吗?

【问题讨论】:

  • 您为什么使用 JMS ObjectMessage?出于多种原因不鼓励这样做。
  • @Justin:我知道它出于多种原因不鼓励,但我仍然有这样做的要求,所以无能为力。
  • 您有使用ObjectMessage 的特定要求,即使从技术上讲,数据可以通过其他更安全且不受相同限制的消息进行传输?通常需求不会像这样在实现级别表达。

标签: java spring-boot jms activemq classnotfoundexception


【解决方案1】:

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

此外,您不需要对经纪人的wrapper.conf 进行这些更改。 listener 试图反序列化消息,而不是代理。

【讨论】:

  • 我的侦听器应用程序的类路径中有camel.EquityFeeds,它是用SpringBoot 编写的,它正在消耗JMS ObjectMessage。我想知道为什么JVM找不到它。我确实了解侦听器正在尝试反序列化消息而不是代理。我做了这些更改以查看代理中的对象,即 ActiveMQ。我如何让 SpringBoot 理解 camelproject.EquityFeeds 在它的类路径中。我该如何解决这个问题。
  • 我刚刚解决了这个问题。我为我的 POJO 类 EquityFeeds 提供了与我的 Spring Boot 应用程序中相同的包名称结构。这解决了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
相关资源
最近更新 更多