设置自定义消息转换器时未执行带有@JmsListener 的方法

Method with @JmsListener not executed when setting custom message converter

嗨,

有这三个class我发现了一个问题:

package xpadro.spring.jms;


import javax.jms.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MarshallingMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;





@SpringBootApplication
public class JmsJavaconfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(JmsJavaconfigApplication.class, args);
    }




    @Bean
    public Jaxb2Marshaller jaxb2Marshaller(){
        Jaxb2Marshaller jaxb = new Jaxb2Marshaller();
        Class array[] = new Class[1];
        array[0]= xpadro.spring.jms.model.Order.class;      
        jaxb.setClassesToBeBound(array);
        return jaxb;
    }


    @Bean
    @Autowired
    public MarshallingMessageConverter marshallingMessageConverter( Jaxb2Marshaller marshaller){
        MarshallingMessageConverter mc = new MarshallingMessageConverter();     
        mc.setMarshaller(marshaller);
        mc.setUnmarshaller(marshaller);     
        return mc;      
    }


    @Bean
    @Autowired
    public JmsTemplate init(MarshallingMessageConverter messageConverter,ConnectionFactory  connectionFactory){
        JmsTemplate template = new JmsTemplate();
        template.setMessageConverter(messageConverter); 
        template.setConnectionFactory(connectionFactory);
        return template;
    } 


}

接下来是模特:

package xpadro.spring.jms.model;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "order")
@XmlAccessorType(XmlAccessType.FIELD)
public class Order implements Serializable {
    private static final long serialVersionUID = -797586847427389162L;

    @XmlElement(required = true)
    private String id="";

    public Order() {

    }

    public Order(String id) {
        this.id = id;
    }


    public String getId() {
        return id;
    }
}

接下来是听众:

package xpadro.spring.jms.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import xpadro.spring.jms.model.Order;
import xpadro.spring.jms.service.StoreService;

@Component
public class SimpleListener {
    private final StoreService storeService;

    @Autowired
    public SimpleListener(StoreService storeService) {
        this.storeService = storeService;
    }

    @JmsListener(destination = "simple.queue")
    public void receiveOrder(Order order) {
        storeService.registerOrder(order);
    }
}

发送消息的class:

package xpadro.spring.jms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import xpadro.spring.jms.model.Order;

@Service
public class ClientServiceImpl implements ClientService {
    private static final String SIMPLE_QUEUE = "simple.queue";
    private static final String IN_QUEUE = "in.queue";

    private final JmsTemplate jmsTemplate;

    @Autowired
    public ClientServiceImpl(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    @Override
    public void addOrder(Order order) {
        //MessageRegistered with a bean
        jmsTemplate.convertAndSend(SIMPLE_QUEUE, order);
    }

    @Override
    public void registerOrder(Order order) {
        //MessageRegistered with a bean
        jmsTemplate.convertAndSend(IN_QUEUE, order);
    }
}

关键是当我在 class JmsJavaconfigApplication 中设置自定义消息转换器时,没有执行 listener(method receiveOrder())。当我不设置它时,spring 引导设置一个 SimpleMessageConverter 并执行侦听器。

配置有误或遗漏吗?

谢谢

您需要通过配置侦听器容器工厂 see the documentation.

@JmsListener 提供编组转换器以解组消息