How to get the file from service-activator Message object in listener class

Tags: , , ,



I need to pass the file to service layer which i am receiving in SFTP path. below is configuration and i am seeing the message receiving in my service-activator like

GenericMessage [payload=com.jcraft.jsch.ChannelSftp$2@322906a2, 
headers={closableResource=org.springframework.integration.sftp.session.SftpSession@379662a7, 
id=704c58e7-1d93-3bef-0330-233c0f9af55c, file_remoteDirectory=/tmp/charge/, 
file_remoteFile=Charge.txt, timestamp=1594158522576}]

<bean id="sftpSessionFactory" 
class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    <property name="host" value="hostname"/>
    <property name="port" value="22"/>
    <property name="user" value="vkp"/>
    <property name="password" value="1234"/>
    <property name="allowUnknownKeys" value="true"/>
</bean>
<int-sftp:inbound-streaming-channel-adapter id="sftpAdapterAutoCreate"
                                  session-factory="sftpSessionFactory"
                                  filename-pattern="*.txt"
                                  channel="receiveChannel"
                                  remote-directory="/tmp/charge/">
</int-sftp:inbound-streaming-channel-adapter>

<int:poller fixed-rate="25000" max-messages-per-poll="1" id="shippingChargePoller" default="true"/>

<int:channel id="receiveChannel">
    <int:queue/>
</int:channel>

<int:stream-transformer id="withCharset" charset="UTF-8"    input- 
channel="receiveChannel" />

<int:service-activator id="FeedListener" input-channel="receiveChannel"  method="onMessage">
    <bean class="com.listener.ChargeFeedListener"/>
</int:service-activator>


   public void onMessage(Message<?> message){
    System.out.println(message.toString());
    System.out.println( " Received File is  "+message.getPayload());
}

But i am not receiving the file in my java class . What i need to do to get the file ?

Answer

Please, read documentation: https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-streaming. The <int-sftp:inbound-streaming-channel-adapter> is not about files. It does open an InputStream for a remote entry (probably file on SFTP) and let you to do with this stream whatever you want. For example (also according that docs), there is a StreamTransformer which let’s you to read that stream into a byte[] or string if you provide a charset. If you really want to deal with files, then you need to consider to switch to the <int-sftp:inbound-channel-adapter>. That one pull the remote entry and store its content into a local file. Then that java.io.File is sent to the channel for your consideration.

I think we had a chat with you on the matter in other your question: Spring SFTP Integration is not polling the file.

Please, let us know what is wrong with our docs that confuses you so you have to raise questions like this over here.



Source: stackoverflow