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

时间:2016-01-25 16:30:18

标签: converter message spring-jms

HY,

有了这三个课程,我发现了一个问题:

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);
    }
}

发送消息的类:

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);
    }
}

关键是当我在JmsJavaconfigApplication类中设置自定义消息转换器时,不会执行listener(方法receiveOrder())。当我没有设置它时,spring boot设置SimpleMessageConverter并执行监听器。

有任何错误或错过的配置?

由于

1 个答案:

答案 0 :(得分:3)

您需要为@JmsListener提供编组转换器,以便通过配置侦听器容器工厂see the documentation来解组消息。