超越他人
VMware 提供培训和认证,助你加速进步。
了解更多实现方法有很多种。当今许多应用依赖于消息传递(AMQP、JMS)来弥合不同系统和数据之间的差距。另一些则依赖于 RPC(通常是 Web 服务或 REST)。然而,对于许多应用程序来说,文件传输是非常重要的方式!有几种常见的支持方式,其中最常见的三种是使用共享挂载或文件夹、使用 FTP 服务器,以及——对于更安全的交换——使用 SSH(或 SFTP)。虽然众所周知 Spring 一直为消息传递(JMS、AMQP)和 RPC 提供了头等支持(远程调用选项太多无法一一列举!),但许多人可能会对 Spring Integration 项目提供的强大文件传输选项感到惊讶。在本文中,我将基于即将发布的 Spring Integration 2.0 框架中一些令人兴奋的支持进行构建,该框架允许你在新文件到达时接入事件,并将文件发送到远程端点,例如 FTP 或 SFTP 服务器,或共享挂载。
我们将使用一对熟悉的 Java 类——一个用于生成出站数据,另一个用于接收入站数据,无论它们是用于 SFTP、FTP 还是普通的旧式文件系统,这都不重要。所有适配器都将 java.io.File
对象作为其入站负载传递,我们可以将 File、String 或 byte[]
发送到远程系统。首先,让我们看看我们的标准客户端。在 Spring Integration 中,响应入站消息执行逻辑的类称为“服务激活器”(service activators)。你只需配置一个 <service-activator>
元素,并告诉它你要使用哪个 bean 来处理 Message
。它会遵循几种不同的启发式方法来帮助你确定调度消息到哪个方法。在这里,我们只是显式地进行注解。因此,以下是我们将在整个文章中使用的客户端代码
import org.springframework.integration.annotation.*;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.Map;
@Component
public class InboundFileProcessor {
@ServiceActivator
public void onNewFileArrival(
@Headers Map<String, Object> headers,
@Payload File file) {
System.out.printf("A new file has arrived deposited into " +
"the accounting folder at the absolute " +
"path %s \n", file.getAbsolutePath());
System.out.println("The headers are:");
for (String k : headers.keySet())
System.out.println(String.format("%s=%s", k, headers.get(k)));
}
}
并且,这里是我们将用于合成数据,最终将数据存储到文件系统中作为文件的代码
import org.springframework.integration.annotation.Header;
import org.springframework.integration.aop.Publisher;
import org.springframework.integration.file.FileHeaders;
import org.springframework.stereotype.Component;
@Component
public class OutboundFileProducer {
@Publisher(channel = "outboundFiles")
public String writeReportToDisk (
@Header("customerId") long customerId,
@Header(FileHeaders.FILENAME) String fileName ) {
return String.format("this is a message tailor made for customer # %s", customerId);
}
}
最后一点是 Spring Integration 乃至整个 Spring 中我最喜欢的特性之一:接口透明性。OutboundFileProducer
类定义了一个使用 @Publisher
注解的方法。@Publisher
注解告诉 Spring Integration 将此方法调用的返回值转发到一个通道(这里我们通过注解为其命名 - outboundFiles
)。这与你直接注入一个 org.springframework.integration.MessageChannel
实例并直接在该实例上发送 Message
的效果是一样的。不同之处在于,现在这一切都隐藏在一个简洁干净的 POJO 后面!任何人都可以随意注入这个 bean——当他们调用该方法时,返回值会被写入某个地方的一个 File
中 :-) 这是我们的秘密。要激活此特性,我们在 Spring 上下文中安装一个 Spring BeanPostProcessor
。Bean 后处理器机制使你能够轻松扫描 Spring 上下文中的 bean,并在适当时增强它们的定义。在这种情况下,我们正在增强带有 @Publisher
注解的 bean。安装 BeanPostProcessor
就像实例化它一样简单
<beans:bean class="org.springframework.integration.aop.PublisherAnnotationBeanPostProcessor"/>
现在,我可以创建一个注入此 bean 的客户端(或者直接从上下文中访问它),并像使用其他任何服务一样使用它
@Autowired
private OutboundFileProducer outboundFileProducer ;
// ...
outboundFileProducer.writeReportToDisk(1L, "1.txt") ;
最后,在我所有的 Spring 上下文中,我都会启用 <context:component-scan ... />
,让 Java 代码来完成大部分工作并处理业务逻辑。我使用 XML 的唯一地方是描述全局集成解决方案的流程和配置。
在这里,Spring Integration 提供了很大的帮助——让你免于编写所有的目录轮询代码,并让你能够自由地编写对你重要的逻辑。如果你以前使用过 Spring Integration,那么你就知道从外部系统接收事件就像插入一个适配器一样简单,然后让适配器告诉你何时有值得响应的事情。设置很简单:监控一个文件文件夹以查找新文件,当新文件到达并(可选地)匹配某些条件时,Spring Integration 会转发一个 Message
,其负载是已添加文件的 java.io.File
引用。
你可以使用 file:inbound-channel-adapter
来达到此目的。该适配器以固定的时间间隔(由 poller
元素配置)监控一个目录,并在检测到新文件时发布一个 Message
。让我们看看如何在 Spring Integration 中配置它
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans ... xmlns:file="http://www.springframework.org/schema/integration/file" >
<context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/>
<file:inbound-channel-adapter channel="inboundFiles"
auto-create-directory="true"
filename-pattern=".*?csv"
directory="#{systemProperties['user.home']}/accounting">
<poller fixed-rate="10000"/>
</file:inbound-channel-adapter>
<channel id="inboundFiles"/>
<service-activator input-channel="inboundFiles" ref="inboundFileProcessor"/>
</beans:beans>
我认为这些选项都相当直观。filename-pattern
是一个正则表达式,将针对目录中的每个文件名进行评估。如果文件名匹配正则表达式,则将被处理。适配器标签内的 poller 元素告诉适配器每隔 10,000 毫秒(即 10 秒)重新检查目录。directory 属性当然允许你指定要监控的目录,而 channel 则描述当适配器发现内容时应将消息转发到哪个命名通道。在此示例中,与所有后续示例一样,我们将让它将消息转发到一个连接到 <service-activator>
元素的命名通道。服务激活器就是你提供的 Java 代码,Spring Integration 会在收到新消息时调用这些代码。你可以在那里做任何想做的事情。
写入文件系统挂载则是完全不同的事情;它更容易!
<?xml version="1.0" encoding="UTF-8"?> <beans:beans ... xmlns:file="http://www.springframework.org/schema/integration/file" > <context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/> <beans:bean class="org.springframework.integration.aop.PublisherAnnotationBeanPostProcessor"/> <channel id="outboundFiles"/> <file:outbound-channel-adapter channel="outboundFiles" auto-create-directory="true" directory="#{systemProperties['user.home']}/Desktop/sales"/> </beans:beans>
在这个例子中,我们描述了一个命名通道和一个出站适配器。回想一下,出站通道是我们之前创建的 Publisher 类中引用的。在所有示例中,当你调用 writeReportToDisk
方法时,它会将一个 Message 放入通道(outboundFiles
)中,并且这些消息会一直传输直到到达出站适配器。当你调用 writeReportToDisk
方法时,返回值(一个 String)被用作 Message
的负载,并且用 @Header
元素注解的两个方法参数会作为消息头添加到 Message
中。键为 FileHeaders.FILENAME
的 @Header
用于告诉出站适配器在配置的目录中写入文件时要使用的文件名。如果我们没有指定它,它会根据一个 UUID
为我们合成一个。相当巧妙吧?
让我们看看如何配置 Spring Integration 以从远程 FTP 服务器接收新文件。
<?xml version="1.0" encoding="UTF-8"?> <beans ... xmlns:ftp="http://www.springframework.org/schema/integration/ftp"> <context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/> <context:property-placeholder location="file://${user.home}/Desktop/ftp.properties" ignore-unresolvable="true"/> <ftp:inbound-channel-adapter remote-directory="${ftp.remotedir}" channel="ftpIn" auto-create-directories="true" host="${ftp.host}" auto-delete-remote-files-on-sync="false" username="${ftp.username}" password="${ftp.password}" port="2222" client-mode="passive-local-data-connection-mode" filename-pattern=".*?jpg" local-working-directory="#{systemProperties['user.home']}/received_ftp_files" > <int:poller fixed-rate="10000"/> </ftp:inbound-channel-adapter> <int:channel id="ftpIn"/> <int:service-activator input-channel="ftpIn" ref="inboundFileProcessor"/> </beans>
你可以看到有很多选项!其中大多数都是可选的——但知道它们存在总是好的。这个适配器将下载符合指定 filename-pattern
的文件,然后将其作为 payload 为 java.io.File
的 Message
传递,就像之前一样。这就是为什么我们可以简单地重用之前的 inboundFileProcessor
bean。如果你想进一步控制哪些文件被下载,哪些不被下载,可以考虑使用 filename-pattern 指定一个掩码。请注意,这里暴露了相当多的控制选项,包括连接模式的控制以及文件交付后是否删除源文件。
出站适配器看起来与我们为文件支持配置的出站适配器非常相似。执行时,它将整理传入 payload 的内容,然后将这些内容存储在 FTP 服务器上。目前已内置支持对 String
、byte[]
和 java.io.File
对象进行整理。
<?xml version="1.0" encoding="UTF-8"?> <beans ... xmlns:ftp="http://www.springframework.org/schema/integration/ftp"> <context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/> <context:property-placeholder location="file://${user.home}/Desktop/ftp.properties" ignore-unresolvable="true"/> <int:channel id="outboundFiles"/> <ftp:outbound-channel-adapter remote-directory="${ftp.remotedir}" channel="outboundFiles" host="${ftp.host}" username="${ftp.username}" password="${ftp.password}" port="2222" client-mode="passive-local-data-connection-mode" /> </beans>
与出站文件适配器一样,我们使用 OutboundFileProducer
类生成要存储的内容,因此无需回顾这部分。剩下的就是通道和适配器本身的配置,其中规定了你期望看到的所有内容:服务器配置以及用于存放 payload 的远程目录。
继续....
要开始使用入站适配器,只需复制粘贴 FTP 适配器,将所有出现的 FTP 重命名为 SFTP,酌情更改相关的配置值(端口、主机等),删除 client-mode 选项,然后就完成了!当然还有其他选项——许多其他选项允许你指定你的认证机制;例如公钥或用户名。这是一个熟悉的例子
<?xml version="1.0" encoding="UTF-8"?> <beans ... xmlns:sftp="http://www.springframework.org/schema/integration/sftp"> <context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/> <context:property-placeholder location="file://${user.home}/Desktop/sftp.properties" ignore-unresolvable="true"/> <sftp:inbound-channel-adapter remote-directory="${sftp.remotedir}" channel="sftpIn" auto-create-directories="true" host="${sftp.host}" auto-delete-remote-files-on-sync="false" username="${sftp.username}" password="${sftp.password}" filename-pattern=".*?jpg" local-working-directory="#{systemProperties['user.home']}/received_sftp_files" > <int:poller fixed-rate="10000"/> </sftp:inbound-channel-adapter> <int:channel id="sftpIn"/> <int:service-activator input-channel="sftpIn" ref="inboundFileProcessor"/> </beans>
相当方便吧?规则与之前的例子相同:你的客户端代码将接收到一个 java.io.File
实例,你可以随意处理它。SFTP 出站适配器完善了这组功能
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:sftp="http://www.springframework.org/schema/integration/sftp"> <context:component-scan base-package="org.springframework.integration.examples.filetransfer.core"/> <context:property-placeholder location="file://${user.home}/Desktop/sftp.properties" ignore-unresolvable="true"/> <int:channel id="outboundFiles"/> <sftp:outbound-channel-adapter remote-directory="${sftp.remotedir}" channel="outboundFiles" host="${sftp.host}" username="${sftp.username}" password="${sftp.password}" /> </beans>