它应该如何处理空负载

它应该如何处理空负载

本文介绍了Spring JMSListener - 它应该如何处理空负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我用这篇文章问了基本相同的问题:Spring JMS 侦听器应该如何处理带有空负载的消息?,但我得到的只是一条微不足道的评论,建议我重新编写我的侦听器以执行我的操作"想要".有效的声明,但在我看来不清楚,因为我仍在掌握 Spring-Boot.从那以后我就学会了,想更直接地重新问这个问题(而不是悬赏旧问题).

I asked basically the same thing a few months ago with this post: How should a Spring JMS listener handle a message with an empty payload?, but all I got was a measly comment suggesting I "re-write my listener to do what I want". Valid statement, but unclear in my eyes as I'm still coming to grips with Spring-Boot. I've learned since then and want to re-ask this question more directly (as opposed to placing a bounty on the old one).

我用 @Configuration@EnableJms 设置了一个带注释的 bean 类,我的容器工厂看起来像:

I set up an annotated bean class with @Configuration and @EnableJms and my container factory looks like:

@Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(mqConnectionFactory());
        factory.setDestinationResolver(destinationResolver());
        factory.setConcurrency("1");
        factory.setErrorHandler(errorHandler());
        factory.setSessionTransacted(true);
        factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
        return factory;
    }

监听器看起来像:

@JmsListener(id = "qID", destination = "qName")
    public void processOrder(String message) {. . .}

据我所知,一旦带注释的 bean 类运行完毕,JMSListener 基本上就会启动(除非我将 autoStartup 设置为 false),所以我无法理解我可以在何时何地控制 JmsListener 处理什么或如何处理事情.从我的角度来看,它只是运行".因此,如果队列上有 "\n" 或只是一个空字符串,则侦听器将抛出异常.特别是 org.springframework.messaging.converter.MessageConversionException: No converter found to convert to class java.lang.String.而这个异常是在幕后抛出的.我从来没有机会在监听器内执行任何事情

As I understand it, once the annotated bean class gets ran through, the JMSListener basically kicks off (unless I set autoStartup to false), so I fail to understand where and when I have control over what or how the JmsListener handles things. From my perspective it "just runs". So if a queue has "\n" on it or just an empty string, the listener is going to throw an exception. Specifically org.springframework.messaging.converter.MessageConversionException: No converter found to convert to class java.lang.String. And this exception is thrown behind the scenes. I never get the chance to execute anything inside the listener

我查看了 SimpleMessageConverter,但似乎没有看到任何可以让我说出类似 setIgnoreStringPattern() 的内容.这显然不存在,但这正是我所需要的.我错过了什么?有没有办法告诉 JmsListener 忽略某些字符串?

I looked into SimpleMessageConverter but didn't seem to see anything that would allow me to say something like setIgnoreStringPattern(). That obviously doesn't exist, but that's what I need. What am I missing? Is there a way to tell the JmsListener to ignore certain strings?

推荐答案

我接受了 M. Deinum 的建议(因为它看起来又快又干净),然后简单地将参数类型设为 javax.jms.Message将传入的消息转换为字符串.所以我的监听器现在看起来像

I took M. Deinum's suggestion (as it seemed quick and clean) and simply made the parameter type javax.jms.Message then converted the incoming message into a string. So my Listener now looks like

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

这可能会抛出一个 JMSException,但我不太关心这个,因为现在当我实现的 ErrorHandler 类被调用时,我现在知道为什么并且可以做一些更具体的事情来处理一个转换失败.这正是我需要的.

This may throw a JMSException, but I'm not too concerned with that as now when my implemented ErrorHandler class is called, I'll now know why and can do something more specific to handle a failed conversion. This does exactly what I need it to.

为了回应 Jonh K 的建议,听众不喜欢将 byte[] 作为参数.它基本上想要一个转换器来从字节数组转换为字符串.选择不实施我自己的自定义转换器.

And in response to Jonh K's suggestion, the listener did not like having byte[] as a parameter. It basically wanted a converter to convert from byte array to string. Opted out of implementing my own custom converter.

这篇关于Spring JMSListener - 它应该如何处理空负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:35