通过spring集成从队列中消费消息

时间:2016-02-20 16:10:54

标签: java jms spring-integration

我有以下配置,它连接到一个基本上是tibco队列的队列,并将消息放入队列,现在我想将它增强到接收器部分

我想创建一个单独的xml配置,其中jms适配器将连接到tibco消息代理并使用来自同一队列的消息并将该消息写入文本文件并将该文本文件存储在我的C驱动器中,请指教该配置将如何

对于发件人部分我有以下配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:jms="http://www.springframework.org/schema/integration/jms"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
        http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">     

<context:component-scan base-package="com.apress.prospringintegration" /> 

 <int:poller  id="poller" default="true" >
 <int:interval-trigger interval="200"/>
 </int:poller> 



<int:channel id="input">
<int:queue capacity="10"/>
</int:channel>

<bean id="tibcoEMSJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
<prop key="java.naming.provider.url">tcp://werems1.fm.absgrp.net:5678</prop>
<prop key="java.naming.security.principal">xyz</prop>
<prop key="java.naming.security.credentials">xyz</prop>
</props>
</property>
</bean>

    <bean id="tibcoEMSConnFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="tibcoEMSJndiTemplate" />
        </property>
        <property name="jndiName">
            <value>GenericConnectionFactory</value>
        </property>
    </bean>

 <bean id="tibcosendJMSTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref local="tibcoEMSConnFactory" />
        </property>
        <property name="defaultDestinationName">
            <value>qwert.dev.queue.test.data</value>
        </property>         
        <property name="pubSubDomain">
            <value>false</value> 
        </property>
        <property name ="receiveTimeout">
            <value>120000</value>
        </property> 
    </bean>    

   <jms:outbound-channel-adapter channel="input" destination-name="qwert.dev.queue.test.data"  connection-factory="tibcoEMSConnFactory" /> 



</beans>

1 个答案:

答案 0 :(得分:0)

int-jms:message-driven-channel-adapter -> int-file:outbound-channel-adapter

在发送方,您的input频道不一定是队列频道;删除队列元素;同样在接收器端,只需使用直接通道。

修改

<jms:message-driven-channel-adapter id="jmsIn"
        destination-name="qwert.dev.queue.test.data"
        channel="jmsInChannel" />

<channel id="jmsInChannel" />

<int-file:outbound-channel-adapter channel="jmsInChannel" directory="/tmp"
  filename-generator-expression=
   "new java.text.SimpleDateFormat('yyyyMMdd-HHmmss.SSS').format(new java.util.Date()) + '.txt'" />

<强> EDIT2

在Spring Integration 3.0.x之前表达式不可用...

<int-file:outbound-channel-adapter channel="jmsInChannel" directory="/tmp"
        filename-generator="generator" />

<bean id="generator" class="foo.TimestampTextGenerator" />

public class TimestampTextGenerator implements FileNameGenerator {

    @Override
    public String generateFileName(Message<?> message) {
        return new java.text.SimpleDateFormat("yyyyMMdd-HHmmss.SSS")
                .format(new java.util.Date()) + ".txt";
    }

}