使用Spring JMS时未调用侦听器方法

时间:2016-12-07 12:02:05

标签: spring spring-jms

我有一个基于Spring 4.1构建的应用程序。我正在尝试使用XML配置创建一个JMS侦听器,并尝试将传入的XML消息转换为Java对象。下面是我的xml配置:

<jms:listener-container concurrency="10"
    connection-factory="connectionFactory"
    message-converter="marshallingMessageConverter">
    <jms:listener destination="destination.name" ref="messageListener" method="processMessage"/>
</jms:listener-container>

<bean id="messageListener" class="com.example.CustomMessageListener">
</bean>

<bean id="marshallingMessageConverter" class="org.springframework.jms.support.converter.MarshallingMessageConverter">
        <property name="marshaller" ref="xmlMarshaller"/>
        <property name="unmarshaller" ref="xmlMarshaller"/>
</bean>

<bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound" value="com.example.CustomObject"/>
</bean>

班级com.example.CustomMessageListener如下所示:

public class CustomMessageListener{
    public void processMessage(Message message, CustomObject object){
        //Do Something.
    }

现在当我将消息发布到目标队列时,监听器上的方法processMessage()没有被调用,我在spring日志中收到以下错误:

Failed to invoke target method 'processMessage' with arguments {com.example.CustomObject@52ee271d}; 
nested exception is java.lang.NoSuchMethodException:
com.example.CustomMessageListener.processMessage(com.example.CustomObject).

现在,如果我将Listerner方法的参数更改为只接受CustomObject,它就可以工作,我从XML中正确构造了CustomObject:

public void processMessage(CustomObject, object)

但我还需要原始javax.jms.Message实例,并且根据this文档,应该可以通过在参数列表中指定它来接收该实例。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

试试这个应该有效

<jms:listener-container concurrency="10"   connection-factory="connectionFactory" >
    <jms:listener destination="destination.name" ref="defaultMessageListener"  />
</jms:listener-container>

<bean id="messageListener" class="com.example.CustomMessageListener">
</bean>

<bean id="marshallingMessageConverter" class="org.springframework.jms.support.converter.MarshallingMessageConverter">
        <property name="marshaller" ref="xmlMarshaller"/>
        <property name="unmarshaller" ref="xmlMarshaller"/>
</bean>

<bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound" value="com.example.CustomObject"/>
</bean>

<bean id="payloadArgumentResolver" class="org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver">
    <property name="converter" ref="marshallingMessageConverter"/>
</bean>

<bean id="handlerMethod" class="org.springframework.messaging.handler.invocation.InvocableHandlerMethod">
    <constructor-arg
        ref="messageListener" index="0"/>
    <constructor-arg
        value="processMessage" index="1"/>
    <constructor-arg
        value="javax.jms.Message"  type="java.lang.Class" index="2"/>
    <constructor-arg
        value="com.example.CustomObject"  type="java.lang.Class" index="3"/>
    <property name="argumentResolvers" ref="payloadArgumentResolver"   />
</bean>

<bean id="defaultMessageListener" class="org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter">
    <property name="handlerMethod" ref="handlerMethod"   />
</bean>