Ravi Kalla
Ravi Kalla

Reputation: 1

How to convert file to a string using SpringIntegration

I am trying to read a text file and convert it to a string using SpringIntegration.

Need help in transforming file to a string.

Git Link: https://github.com/ravikalla/spring-integration

Source Code -

    @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;
    }

Error -

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)

Upvotes: 0

Views: 747

Answers (3)

NAVEENREDDY K
NAVEENREDDY K

Reputation: 11

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;
    }

}

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174544

That error is coming from somewhere else; the FTST can't return null.

I haven't looked at all your code, but this looks suspicious:

@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;
        }
    };
}

It can return null; transformers are not allowed to do that.

Turn on DEBUG logging and follow the message flow to see which component is at fault.

Upvotes: 0

Mohendra Amatya
Mohendra Amatya

Reputation: 441

You can convert the file into an InputStream and the use IOUtils.toString(inputStream) to convert it into a String.

Upvotes: 1

Related Questions