Spring JMS侦听器应如何处理有效载荷为空的消息? [英] How should a Spring JMS listener handle a message with an empty payload?

查看:79
本文介绍了Spring JMS侦听器应如何处理有效载荷为空的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我工​​作的公司开发一个错误队列监视工具,并且使用Spring Boot通过注解(而不是使用任何xml)设置了所有bean,从而使这件事得以运行.该工具(到目前为止)可以按预期工作,除了以下事实之外:在我们的错误队列(IBM的MQ系列)上,一些xml消息包含一个新行,我的JMS侦听器将其作为自己的行.实际上,似乎它逐行地专门处理xml消息,而不是整个消息(尽管它可以很好地处理整个HL7消息).该功能超出我的能力,但这是它自己的问题.-

I'm working on a error queue monitoring tool for a company I work for and I have got this thing up and running using Spring Boot doing all my bean set up using annotations (as opposed to using any xml). The tool works (so far) as intended except for the fact that on our error queue (which is IBM's MQ series) some xml messages contain a new line which my JMS listener picks up as its own thing. In fact it would seem it exclusively processes xml message line by line as opposed as a whole message (though it processes entire HL7 messages just fine). This functionality is beyond my knowledge but that's its own question.-

当我的JMS侦听器抓住这个换行符时,它抛出一个异常,指出消息有效负载不能为空,然后在索引为零的位置上乱窜.它启动回滚并调用我的JMSErrorhandler,问题是尽管它停留在此循环中(永远……).从调试器看来,此异常发生在Spring代码中某个地方,恰好在进入我的processOrder()方法之前,因为它永远不会到达我的断点.我不在工作现场,因此我无法访问代码,也无法访问堆栈跟踪(希望我对错误的糟糕解释就足够了),但是我可以说是直接从Spring站点使用此监听器建模的:

When my JMS Listener grabs this newline character it tosses an exception stating that message payload cannot be empty and then rabbles about something at index zero. It initiates a rollback and calls my JMSErrorhandler, problem is though it stays in this loop (forever...). From the debugger it looks like this exception happens somewhere in the Spring code right before it steps into my processOrder() method as it never gets to my breakpoints. I'm not on the worksite so I don't have access to the code, nor a stacktrace (hopefully my terrible explanation of the error is sufficient), but I can say the listener is modeled directly from this, from the Spring site:

@Component
public class MyService {

    @JmsListener(destination = "myDestination")
    public void processOrder(String data) { ... }
}

那么我有什么可以接触的?为了防止发生回滚,是否应该选择诸如换行符之类的东西?另外,就像一般性的声明一样,换行符可能是由另一个将这些消息放入队列的应用程序引起的,但是我想涵盖所有基础知识并获得一些知识.

So is there anything I have access to; to prevent the rollback from occurring should something like a newline character be picked up? Also, just as a general statement, the newline character could be caused by another application that puts these messages onto the queue, but I want to cover all my bases, and gain some knowledge.

为了弄清楚异常,在我的侦听器能够处理消息之前,在Spring代码中的某个地方引发了异常.我明白了:

To clarify an exception is thrown somewhere in the Spring code before my listener is able to handle the message. I get this:

Caused by: org.springframework.messaging.converter.MessageConversionException: No converter found to convert to class java.lang.String, message=GenericMessage [payload=
  JMSMessage class: jms_none
  JMSType:          null
  JMSDeliveryMode:  2
  JMSExpiration:    0
  JMSPriority:      5
  JMSMessageID:     ID:414d5120514d5f4445565f595731202056d7ea6b28797f6b
  JMSTimestamp:     1460469585820
  JMSCorrelationID: null
  JMSDestination:   null
  JMSReplyTo:       null
  JMSRedelivered:   false
    JMSXAppID: perl                        
    JMSXDeliveryCount: 1
    JMSXUserID: cisadm      
    JMS_IBM_Character_Set: ISO-8859-1
    JMS_IBM_Encoding: 273
    JMS_IBM_Format: MQSTR   
    JMS_IBM_MsgType: 8
    JMS_IBM_PutApplType: 6
    JMS_IBM_PutDate: 20160412
    JMS_IBM_PutTime: 13594582, headers={JMS_IBM_Character_Set=ISO-8859-1, JMS_IBM_MsgType=8, JMSXUserID=cisadm      , jms_priority=5, JMS_IBM_Encoding=273, jms_timestamp=1460469585820, JMSXAppID=perl                        , JMS_IBM_PutApplType=6, JMS_IBM_Format=MQSTR   , jms_redelivered=false, JMS_IBM_PutDate=20160412, jms_deliveryMode=2, JMSXDeliveryCount=1, JMS_IBM_PutTime=13594582, id=1dfefda3-49cb-dfcc-1a31-b8087e3d6ebd, jms_expiration=0, jms_messageId=ID:414d5120514d5f4445565f595731202056d7ea6b28797f6b, timestamp=1460469638210}]
    at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:118) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:139) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:108) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:90) ~[spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    ... 10 more

不确定如何解决此问题.有建议吗?

Not sure how to resolve this. Suggestions?

推荐答案

此视图具有足够的视角来促使我发布此答案.我基本上通过这篇文章以更直接的方式提出了这个问题: Spring JMSListener-它应该如何处理空的有效载荷?,并得出了结论.从注释中采纳了其他人的建议,只需将参数类型从String更改为javax.jms.Message.这样一来,有效负载为空的消息(即空字符串,甚至是换行符)就不会影响侦听器的流程,而只会对其进行处理.

This has enough views to incline me to post this answer. I basically re-asked this question in a more direct manner with this post: Spring JMSListener - How should it handle empty payloads?, and came to a conclusion. Took someone else's suggestion from the comments and simply changed the parameter type from String to javax.jms.Message. This allows messages with an otherwise empty payload (i.e. empty strings, and even newline characters), to not affect the flow of the listener and it will just process them.

现在您所要做的就是从传入的Message中提取字符串,然后您可以检查其内容并决定如何处理它.因此,听众最终看起来像这样:(或至少是我做的)

Now all you have to do is extract the string from the incoming Message and then you can check its contents and decide what to do with it. So the listener ends up looking like this: (or at least what I did)

@JmsListener
public void processOrder(Message message) throws JMSException {
     String convertedMessage = ((TextMessage) message).getText();
     :
     :
}

达菲(duffymo)是正确的,因为没有魔法在发生.正如您在另一篇文章中所看到的那样,如果您不想使用这样的解决方案,则可以实现自己的消息转换器,以对消息进行所需的操作.

duffymo was correct in that there is no magic going on. And as you can see in the other post, if you didn't want to use a solution such as this, you could implement your own message converter to do what you want with the message.

这篇关于Spring JMS侦听器应如何处理有效载荷为空的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆