为什么messageConverter.fromMessage()没有转换arraylist? java.lang.ClassCastException:java.lang.String无法强制转换

时间:2017-11-04 22:33:22

标签: java spring classcastexception spring-jms

我想发送包含销售列表的JMS消息,并通过onMessage()接收。据我所知,springframework.jms.support.converter.MessageConverter应该扮演转换或转换消息的角色,因此我不必创建XSD或额外的映射类。我的意思是,我期待测试下面发送一条消息,并(SalesMessage) messageConverter.fromMessage(message)足以转换和实现SalesMessage的对象。

基本上,错误是java.lang.ClassCastException: java.lang.String cannot be cast to ...SalesMessage

我的测试

AnnotationConfigApplicationContext context;
MyMessageSender ms;
JmsListenerEndpointRegistry bean;

@Before
public void setUpBeforeClass() throws Exception {

    context =  new AnnotationConfigApplicationContext(AppConfig.class);
    ms = context.getBean(MyMessageSender.class);
}

@Test
public void test2() {

    Sale s = new Sale("product_type", 1L);
    List<Sale> l = new ArrayList<Sale>();
    l.add(s);

    SalesMessage sm = new SalesMessage(l, 1);

    ms.sendMessage(sm.toString());

    bean = context.getBean(JmsListenerEndpointRegistry.class);
    for (MessageListenerContainer listenerContainer : bean.getListenerContainers()) {
        DefaultMessageListenerContainer container = (DefaultMessageListenerContainer) listenerContainer;
        container.shutdown();
    }

}

MyJmsListener

    @Component
    public class MyJmsListener {

         @Autowired
         private MessageConverter messageConverter;

        @JmsListener(destination = AppConfig.QUEUE_NAME)
        public void handleMessage(Message message) {
            try {
                System.out.println("received: "+message);
//this prints received: ActiveMQTextMessage {commandId = 5, ..., jmsXGroupFirstForConsumer = false, text = SalesMessage [sales=[Sale [product_type=produ...ssageType=1]}
                SalesMessage salesMessage = (SalesMessage) messageConverter.fromMessage(message); // <<< here is the focus of my question
                System.out.println("salesMessage: "+salesMessage);
            }
            catch(JMSException e) {
                 e.printStackTrace();
            }

        }
    }

Spring Config

@Configuration
@ComponentScan
@EnableJms
public class AppConfig {
    public static final String QUEUE_NAME = "example.queue";

    @Bean
    public ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory =
                new ActiveMQConnectionFactory("vm://localhost");
        return connectionFactory;
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory =
                new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        //core poll size=4 threads and max poll size 8 threads
        factory.setConcurrency("4-8");
        return factory;
    }

    @Bean
    MessageConverter converter(){
        return new SimpleMessageConverter();
    }
}

SalesMessage类

public class SalesMessage  implements Serializable{
    List<Sale> sales = new ArrayList<Sale>();
    int messageType;
    //getters/setters/toString()

销售课程

public class Sale  implements Serializable{
    private String product_type;
    private Long value;
//getters/setters/toString()

发送消息

@Component
public class MyMessageSender {

    @Autowired
    private ConnectionFactory connectionFactory;
    private JmsTemplate jmsTemplate;

    @PostConstruct
    public void init() {
        this.jmsTemplate = new JmsTemplate(connectionFactory);
    }

    public void sendMessage(String message) {
        System.out.println("sending: " + message);
        jmsTemplate.send(AppConfig.QUEUE_NAME, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }
}

***编辑 Spring Config updqted

@Configuration
@ComponentScan
@EnableJms
public class AppConfig {
    public static final String QUEUE_NAME = "example.queue";

    @Bean
    public ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
        return connectionFactory;
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        // core poll size=4 threads and max poll size 8 threads
        factory.setConcurrency("4-8");
        return factory;
    }

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setMessageConverter(jacksonJmsMessageConverter());
        template.setConnectionFactory(connectionFactory());
        return template;
    }

***编辑  当我在Listener一侧打印消息时,我得到了

  

收到:ActiveMQObjectMessage {commandId = 5,responseRequired = true,messageId = ID:win10-cha-51561-1509914170598-4:2:1:1:1,originalDestination = null,originalTransactionId = null,producerId = ID:win10 -cha-51561-1509914170598-4:2:1:1,destination = queue://example.queue,transactionId = null,expiration = 0,timestamp = 1509914232995,arrival = 0,brokerInTime = 1509914232995,brokerOutTime = 1509914233010,correlationId = null,replyTo = null,persistent = true,type = null,priority = 4,groupID = null,groupSequence = 0,targetConsumerId = null,compressed = false,userID = null,content = org.apache.activemq.util.ByteSequence @ 24f339bf,marshalledProperties = null,dataStructure = null,redeliveryCounter = 0,size = 1346,properties = null,readOnlyProperties = true,readOnlyBody = true,droppable = false,jmsXGroupFirstForConsumer = false}

和SalesMessage salesMessage =(SalesMessage)messageConverter.fromMessage(message)引发此异常

  

org.springframework.jms.support.converter.MessageConversionException:无法在目的地[ID:win10-cha-51561-1509914170598-4:2:1:1:1]上找到类型ID属性[_type]队列://example.queue]

在发送方面,我打印消息

SalesMessage [sales=[Sale [product_type=product_type, value=1]], messageType=1]

并且,这是我更新的完整应用程序配置(我按照提供的建议)

@Configuration
@ComponentScan
@EnableJms
public class AppConfig {
    public static final String QUEUE_NAME = "example.queue";

    @Bean
    public ConnectionFactory connectionFactory() {
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
        return connectionFactory;
    }

    @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrency("4-8");
        factory.setMessageConverter(jacksonJmsMessageConverter());
        return factory;
    }

    @Bean 
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");


        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        converter.setObjectMapper(objectMapper);
        return converter;
    }
}

而且,这是我在最后一个建议之后发送消息的方式(重大变化是从jmsTemplate.send到jmsTemplate.convertAndSend):

@Component
public class MyMessageSender {

    @Autowired
    private ConnectionFactory connectionFactory;
    private JmsTemplate jmsTemplate;

    @PostConstruct
    public void init() {
        this.jmsTemplate = new JmsTemplate(connectionFactory);
    }

    public void sendMessage(SalesMessage  message) {
        System.out.println("sending: " + message);
        jmsTemplate.convertAndSend(AppConfig.QUEUE_NAME, message);
    }
}

***已编辑

enter image description here

2 个答案:

答案 0 :(得分:2)

咦?

  

ms.sendMessage(sm.toString());

     

java.lang.ClassCastException:java.lang.String无法强制转换为... SalesMessage。

这是因为您以JSON格式发送了Stringsm.toString())而不是SalesMessage

使用jmsTemplate.convertAnsSend(destinationName, sm)

听众容器工厂也需要Jackson转换器。

答案 1 :(得分:1)

我看到你在这里尝试做什么,而你可能试图效仿这个例子:

https://spring.io/guides/gs/messaging-jms/

但是,该示例是在Spring Boot上。我有一个预感你没有使用Spring Boot,基于那里没有@SpringBootApplication注释的面孔。

如果我是对的,你需要使用你定义为bean的SimpleMessageConverter来快速调用DefaultJmsListenerContainerFactory.setMessageConverter方法。我敢打赌Spring Boot可能会自动为你做这件事,但在你的实现中你必须手动完成。

所以,看起来应该是这样的:

 @Bean
    public JmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory =
                new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMessageConverter(converter());
        factory.setConcurrency("4-8");
        return factory;
    }

您还需要确保您的对象是可序列化的,然后您可以使用对象本身执行jmsTemplate.send。转换器将为您执行必要的转换为消息有效负载类型。