【发布时间】: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 和网上的其他帖子,我做了以下更改:
-
我对@987654330@(apache-activemq-5.15.11-bin\apache-activemq-5.15.11\bin\win64)进行了更改
- 然后添加条目:
wrapper.java.classpath.3=C:\Users\sidbharg\eclipse-workspace\Sid\target\camelproject-0.0.1-SNAPSHOT.jar这是我拥有 POJO (EquityFeeds) 的 .jar 文件的位置。 - 还添加了以下条目:
wrapper.java.additional.13=-Dorg.apache.activemq.SERIALIZABLE_PACKAGES="*"
- 然后添加条目:
在我的消费者代码中,我还添加了
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