Method with @JmsListener not executed when setting custom message converter

懵懂的女人 提交于 2019-12-23 05:15:59

问题


Hy,

Having these three classes I have found a problem:

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


}

Next, the model:

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

Next, the listener:

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

The class that sends messages:

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

The point is that listener(method receiveOrder()) is not executed when I set a custom message converter in the class JmsJavaconfigApplication. When I dont set it, spring boot sets a SimpleMessageConverter and the listener is executed.

Any wrong or missed configuration?

Thanks


回答1:


You need to provide an marshalling converter to the @JmsListener to unmarshall the message, by configuring the listener container factory see the documentation.



来源:https://stackoverflow.com/questions/34997479/method-with-jmslistener-not-executed-when-setting-custom-message-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!