如何使用SpringIntegration

时间:2019-04-10 06:14:48

标签: spring spring-boot spring-integration

我正在尝试使用SpringIntegration读取文本文件并将其转换为字符串。

需要帮助将文件转换为字符串。

Git链接:https://github.com/ravikalla/spring-integration

源代码-

    @Bean
    @InboundChannelAdapter(value = "payorFileSource", poller = @Poller(fixedDelay = "10000"))
    public MessageSource<File> fileReadingMessageSource() {
        FileReadingMessageSource sourceReader = new FileReadingMessageSource();
        sourceReader.setDirectory(new File(INPUT_DIR));
        sourceReader.setFilter(new SimplePatternFileListFilter(FILE_PATTERN));
        return sourceReader;
    }

    @Bean
    @Transformer(inputChannel="payorFileSource", outputChannel="payorFileContent")
    public FileToStringTransformer transformFileToString() {
        FileToStringTransformer objFileToStringTransformer = new FileToStringTransformer();
        return objFileToStringTransformer;
    }

错误-

SEVERE: org.springframework.integration.handler.ReplyRequiredException: No reply produced by handler 'fileCopyConfig.transformPayorStringToObject.transformer.handler', and its 'requiresReply' property is set to true., failedMessage=GenericMessage [payload=1|test1, headers={sequenceNumber=1, file_name=payor.txt, sequenceSize=4, correlationId=ff1fef7d-7011-ee99-8d71-96146ac9ea07, file_originalFile=source/payor.txt, id=fd4f950b-afcf-70e6-a053-7d59ff593add, file_relativePath=payor.txt, timestamp=1554875904858}]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:119)

3 个答案:

答案 0 :(得分:1)

您可以将文件转换为InputStream并使用 IOUtils.toString(inputStream)将其转换为字符串。

答案 1 :(得分:0)

该错误来自其他地方; FTST无法返回null

我还没有看完您的所有代码,但这看起来很可疑:

@Bean
@Transformer(inputChannel="payorRawStringChannel", outputChannel="payorRawObjectChannel")
public GenericTransformer<String, Payor> transformPayorStringToObject() {
    return new GenericTransformer<String, Payor>() {
        @Override
        public Payor transform(String strPayor) {
            String[] arrPayorData = strPayor.split(",");
            Payor objPayor = null;
            if (null != arrPayorData && arrPayorData.length > 1)
                objPayor = new Payor(Integer.parseInt(arrPayorData[0]), arrPayorData[1]);
            return objPayor;
        }
    };
}

它可以返回null;不允许变压器这样做。

打开调试日志记录并按照消息流查看哪个组件有故障。

答案 2 :(得分:0)

package org.springframework.integration.samples.tcpclientserver;

import java.io.UnsupportedEncodingException;

import org.springframework.core.convert.converter.Converter;

/**
 * Simple byte array to String converter; allowing the character set
 * to be specified.
 *
 * @author Gary Russell
 * @since 2.1
 *
 */
public class ByteArrayToStringConverter implements Converter<byte[], String> {

    private String charSet = "UTF-8";

    public String convert(byte[] bytes) {
        try {
            return new String(bytes, this.charSet);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new String(bytes);
        }
    }

    /**
     * @return the charSet
     */
    public String getCharSet() {
        return charSet;
    }

    /**
     * @param charSet the charSet to set
     */
    public void setCharSet(String charSet) {
        this.charSet = charSet;
    }

}