简体   繁体   English

春季集成-JMS

[英]Spring Integration - JMS

I am trying to read a message from a JMS Queue (using ActiveMQ). 我正在尝试从JMS队列读取消息(使用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 (1)弹簧配置

<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: (2)服务激活器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: (3)应用开始类:

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> : 为了使onMessage(javax.jms.Message name)工作,您应该在<int-jms:message-driven-channel-adapter>上指定extract-payload="false"

/**
 * 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. 如果extract-payload设置为true(这是默认设置),则收到的JMS消息将通过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. 当依赖默认的SimpleMessageConverter时,这意味着生成的Spring Integration Message将以JMS Message的主体作为其有效负载。 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. JMS TextMessage将产生基于字符串的有效载荷,JMS BytesMessage将产生字节数组的有效载荷,而JMS ObjectMessages Serializable实例将成为Spring Integration Message的有效载荷。 If instead you prefer to have the raw JMS Message as the Spring Integration Message's payload, then set 'extract-payload to false. 相反,如果您更喜欢将原始JMS消息作为Spring Integration Message的有效负载,则将'extract-payload设置为false。

Got the solution to my problem: Parameter type in method "onMessage" should be String: 解决了我的问题:方法“ onMessage”中的参数类型应为String:

import javax.jms.Message;

public class HelloServiceImpl
{

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

    return  "getHelloMessage:, " + name ;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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