【问题标题】:How to get the file from service-activator Message object in listener class如何从侦听器类中的服务激活器消息对象获取文件
【发布时间】:2020-10-28 06:24:36
【问题描述】:

我需要将文件传递给我在 SFTP 路径中接收的服务层。 下面是配置,我看到在我的服务激活器中接收到的消息就像

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

但我没有收到我的 java 类中的文件。我需要做什么来获取文件?

【问题讨论】:

    标签: java spring spring-integration sftp


    【解决方案1】:

    请阅读文档:https://docs.spring.io/spring-integration/docs/current/reference/html/sftp.html#sftp-streaming&lt;int-sftp:inbound-streaming-channel-adapter&gt; 与文件无关。它确实会为远程条目(可能是 SFTP 上的文件)打开一个 InputStream,并让您随心所欲地处理此流。例如(也根据该文档),有一个StreamTransformer,如果您提供charset,您可以将该流读入byte[] 或字符串。如果你真的要处理文件,那么你需要考虑切换到&lt;int-sftp:inbound-channel-adapter&gt;。那个拉远程条目并将其内容存储到本地文件中。然后将java.io.File 发送到频道供您考虑。

    我想我们已经就您的其他问题与您进行了交谈:Spring SFTP Integration is not polling the file

    请让我们知道我们的文档有什么问题让您感到困惑,所以您必须在这里提出这样的问题。

    【讨论】:

    • 我添加了转换器,但在消息中看不到转换后的字符串或字节[]。我更新了我当前的配置。对不起,我错过了什么。
    • 您现在拥有的就像是同一个receiveChannel 的两个订阅者。您的&lt;int:stream-transformer&gt; 可能根本收不到任何消息。更重要的是,您在此转换器上没有 output-channel 来发送结果。我不确定你的逻辑是什么,但可能你需要从理论开始,了解什么是端点和通道:enterpriseintegrationpatterns.com。您可能希望将转换器的结果发送到 FeedListener 服务。因此,将变压器的output-channel 设置为服务激活器的input-channel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多