简体   繁体   中英

Spring Integration - JMS

I am trying to read a message from a JMS Queue (using ActiveMQ). Issue I am facing is, messages are being read from the queue, but not getting displayed in the "service-activator".

Any help is much appreciated.

My code is as below:

(1) Spring Configuration

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
    http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<!-- Component scan to find all Spring components -->
<context:component-scan base-package="com.poc.springinteg._3" />

<!--  -->
<bean id="remoteJndiTemplate" class="org.springframework.jndi.JndiTemplate" lazy-init="false"> 
    <property name="environment"> 
        <props> 
            <prop key="java.naming.provider.url">tcp://localhost:61616</prop>
            <prop key="java.naming.factory.url.pkgs">org.apache.activemq.jndi</prop>
            <prop key="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</prop>
            <prop key="connectionFactoryNames">DefaultActiveMQConnectionFactory,QueueConnectionFactory</prop>
            <prop key="queue.SendReceiveQueue">org.apache.geronimo.configs/activemq-ra/JCAAdminObject/SendReceiveQueue</prop>
            <prop key="queue.SendQueue">org.apache.geronimo.configs/activemq-ra/JCAAdminObject/MDBTransferBeanOutQueue</prop> 
        </props> 
    </property> 
</bean> 

<bean id="remoteConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="false">
    <property name="jndiTemplate" ref="remoteJndiTemplate"/>
    <property name="jndiName" value="QueueConnectionFactory"/>
    <property name="lookupOnStartup" value="true" />
    <property name="proxyInterface" value="javax.jms.ConnectionFactory" />
</bean>


<!-- Reading Queue -->
<bean id="inputQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg index="0">
        <value>InputQueue_3</value>
    </constructor-arg> 
</bean>

<bean id="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="remoteConnectionFactory"/>
    <property name="destination" ref="inputQueue"/>
    <property name="sessionTransacted" value="true"/>
    <property name="maxConcurrentConsumers" value="1"/>
    <property name="concurrentConsumers" value="1"/>
    <property name="autoStartup" value="true"/>         
</bean>

<int:channel id="inbound"/>

<int-jms:message-driven-channel-adapter id="jmsIn" 
                                        channel="inbound"
                                        container="messageListenerContainer" />


<int:service-activator  input-channel="inbound"
                        ref="messageHandler"
                        method="onMessage"/>


<bean id="messageHandler" class="com.poc.springinteg._3.HelloServiceImpl"/>


</beans>

(2) Service Activator MDP:

package com.poc.springinteg._3;
import javax.jms.Message;
public class HelloServiceImpl
{

public String onMessage(Message name) {
    System.out.println( "getHelloMessage:, " + name );

    return  "getHelloMessage:, " + name ;
}
}

(3) Application start class:

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {

 public static void main( String[] args )
    {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:3_applicationContext.xml" );

        applicationContext.registerShutdownHook();
    }
}

Thanks

To make your onMessage(javax.jms.Message name) working you should specify extract-payload="false" on your <int-jms:message-driven-channel-adapter> :

/**
 * Specify whether the JMS request Message's body should be extracted prior
 * to converting into a Spring Integration Message. This value is set to
 * <code>true</code> by default. To send the JMS Message itself as a
 * Spring Integration Message payload, set this to <code>false</code>.
 * @param extractRequestPayload true if the request payload should be extracted.
 */
public void setExtractRequestPayload(boolean extractRequestPayload) {

And quoting Reference Manual :

If extract-payload is set to true (which is the default), the received JMS Message will be passed through the MessageConverter. When relying on the default SimpleMessageConverter, this means that the resulting Spring Integration Message will have the JMS Message's body as its payload. A JMS TextMessage will produce a String-based payload, a JMS BytesMessage will produce a byte array payload, and a JMS ObjectMessages Serializable instance will become the Spring Integration Message's payload. If instead you prefer to have the raw JMS Message as the Spring Integration Message's payload, then set 'extract-payload to false.

Got the solution to my problem: Parameter type in method "onMessage" should be String:

import javax.jms.Message;

public class HelloServiceImpl
{

public String onMessage(String name) {
    System.out.println( "getHelloMessage:, " + name );

    return  "getHelloMessage:, " + name ;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM