我们有一个使用Spring Boot和Tibco EMS JMS实现的应用程序。当我创建war并在本地Tomcat上部署时,我可以毫无问题地发送和接收消息。但是,如果我从main((方法运行应用程序:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
作为Run As>Spring Boot App,我在发送消息进行消费时就看到了这个错误。
这就是创建和发送消息的地方:
@Transactional
private void sendAndLog (Long id1, Long id2, String s, List<Mapping> mappings) {
queue.send(new Request (
id1,
id2,
s,
mappings));
log.info("Message sent.");
}
这就是它被消耗的地方——我在send((被调用后立即看到错误,并且永远不会到达receive((方法:
Processor.java:
@Transactional
public void receive (Request mappings) {
// Process the message
}
这是请求类:
public class Request implements Serializable {
private static final long serialVersionUID = 1L;
private Long id1;
private Long id2;
private String s;
private List<Mapping> mappings;
public Request() {}
public Request (Long id1, Long id2, String s, List<Mapping> mappings) {
this.id1 = id1;
this.id2 = id2;
this.s = s;
this.mappings = mappings;
}
//getter() and setter() methods.
}
这就是错误。
哪里出了问题?为什么我只在使用Spring Runner运行时看到这个错误,而在部署到Tomcat时没有看到?
2018-07-25 17:19:19.924 WARN 4796 --- [enerContainer-7] o.s.j.l.DefaultMessageListenerContainer : Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method could not be invoked with incoming message
Endpoint handler details:
Method [public void com.package.tasks.Processor.receive(com.package.dto.Request)]
Bean [com.package.tasks.Processor@475d2a6d]
; nested exception is org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.package.dto.Request] to [com.package.dto.Request] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@6d5d2920, failedMessage=org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@6d5d2920
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:118)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:77)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1189)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1179)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1076)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.messaging.converter.MessageConversionException: Cannot convert from [com.package.dto.Request] to [com.package.dto.Request] for org.springframework.jms.listener.adapter.AbstractAdaptableMessageListener$MessagingMessageConverterAdapter$LazyResolutionMessage@6d5d2920
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:144)
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:114)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:137)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:109)
at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:114)
... 10 common frames omitted
17:19:19.924 [DefaultMessageListenerContainer-7] WARN o.s.j.l.DefaultMessageListenerContainer - Execution of JMS message listener failed, and no ErrorHandler has been set.
首先,您应该调试以查看发生了什么。将参数类型替换为"Object",从调试开始,看看您收到的是什么类型的Object。试着在调试模式下手动转换对象,看看发生了什么
这条消息令人困惑,看看你的应用程序的包装,看看你是否可以在不同的罐子里用同一个包装有多个dto。如果您只在SpringRunner中遇到问题,那么您可能有本地Tomcat中不存在的依赖项,这就是您出现错误的原因。
此外,可能不是问题的根源,但私有方法上的@Transactionnal通常不起作用,这取决于你在哪里运行代码(Eclipse运行config、gradle、tomcat等(。像这样的注释应该出现在公共方法上。我必须进行研究,但我的猜测是,当您在上下文中从组件调用方法时,当它看到@Transactional时,spring会注入事务。由于您从另一个公共方法调用了私有方法,Spring可能不会注入它