【问题标题】:consuming message from a queue through spring integration通过 spring 集成从队列中消费消息
【发布时间】:2016-06-02 05:28:24
【问题描述】:

我有以下配置,它连接到一个队列,该队列基本上是一个 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>

【问题讨论】:

    标签: java jms spring-integration


    【解决方案1】:

    int-jms:message-driven-channel-adapter -&gt; 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";
        }
    
    }
    

    【讨论】:

    • 非常感谢,只是为了补充,请您展示一下我如何从队列中接收消息并将每条消息写入一个单独的文件,提前感谢这将帮助我掌握很多并且文件名将根据消息完全写入文件时的时间戳记,因此文件名将是 message-timestamp.txt
    • 得到以下错误无法解析对 bean 'new java.text.SimpleDateFormat('yyyyMMdd-HHmmss.SSS').format(new java.util.Date()) + '.txt' 的引用' 同时设置 bean 属性 'fileNameGenerator'
    • 问题是我想使用队列中的消息并将它们写入文本文件,但文本文件的名称将是 timestamp.txt 这样名称本身暗示文件何时已创建
    • 正是这个示例的作用;看来您正在设置filename-generator(它引用一个bean)而不是filename-generator-expression(它使用SpEL 表达式来生成文件名)。如果filename-generator-expression 不可用,您必须使用非常 旧版本的框架。当前版本是 4.2.5.RELEASE。请read the documentation.
    • 我目前正在使用 spring 集成版本 2-0-5 mote bhi 那螨是原因
    猜你喜欢
    • 2016-06-04
    • 2021-05-23
    • 1970-01-01
    • 2016-11-11
    • 2023-04-07
    • 1970-01-01
    • 2018-05-27
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多