当前位置: 首页 > news >正文

网站开发技术难点博文阅读网站建设

网站开发技术难点,博文阅读网站建设,钓鱼平台怎么制作,咨询网站建设1引言 文件支持是Spring Integration与外部系统通信的另一个端点。 在这种情况下#xff0c;它提供了几个组件来读取#xff0c;写入和转换文件。 在这篇文章中#xff0c;我们将编写一个监视目录的应用程序#xff0c;以便读取其中的所有文件。 具体而言#xff0c;它执行… 1引言 文件支持是Spring Integration与外部系统通信的另一个端点。 在这种情况下它提供了几个组件来读取写入和转换文件。 在这篇文章中我们将编写一个监视目录的应用程序以便读取其中的所有文件。 具体而言它执行以下操作 当应用程序启动时它将读取目录中存在的所有文件。 然后应用程序将密切注意目录以检测新文件和已修改的现有文件。 可以在Github中找到源代码。 2配置 该应用程序是使用Spring Boot构建的因为它大大简化了配置。 要创建应用程序的初始基础结构您可以转到https://start.spring.io/ 选择Integration模块并生成项目。 然后您可以在自己喜欢的IDE中打开zip文件。 我在pom.xml中添加了一些依赖项例如commons.io或Spring Integration Java DSL。 我的pom.xml文件如下所示 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdxpadro.spring.integration/groupIdartifactIdfile-read-directory/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnamefile-read-directory/namedescriptionDemo project for Spring Boot/descriptionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.3.5.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-integration/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- Spring Integration - Java DSL --dependencygroupIdorg.springframework.integration/groupIdartifactIdspring-integration-java-dsl/artifactIdversion1.0.0.RELEASE/version/dependencydependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.5/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project 起点是FileReadDirectoryApplication SpringBootApplication public class FileReadDirectoryApplication {public static void main(String[] args) throws IOException, InterruptedException {SpringApplication.run(FileReadDirectoryApplication.class, args);} } 从这里开始我们将添加Spring Integration组件以从文件系统的特定文件夹中读取。 3添加适配器 为了从文件系统读取我们需要一个入站通道适配器。 适配器是一个文件读取消息源它负责轮询文件系统目录中的文件并从找到的每个文件中创建一条消息。 Bean InboundChannelAdapter(value fileInputChannel, poller Poller(fixedDelay 1000)) public MessageSourceFile fileReadingMessageSource() {CompositeFileListFilterFile filters new CompositeFileListFilter();filters.addFilter(new SimplePatternFileListFilter(*.txt));filters.addFilter(new LastModifiedFileFilter());FileReadingMessageSource source new FileReadingMessageSource();source.setAutoCreateDirectory(true);source.setDirectory(new File(DIRECTORY));source.setFilter(filters);return source; } 我们可以通过为消息源设置过滤器列表来防止某些类型的文件被轮询。 对于此示例已包含两个过滤器 SimplePatternFileListFilter Spring提供的过滤器。 仅具有指定扩展名的文件将被轮询。 在这种情况下将仅接受文本文件。 LastModifiedFileFilter 自定义过滤器。 该过滤器跟踪已轮询的文件并将过滤自上次跟踪以来未修改的文件。 4处理文件 对于每个轮询的文件我们将其内容转换为String然后再将其传递给处理器。 为此Spring已经提供了一个组件 Bean public FileToStringTransformer fileToStringTransformer() {return new FileToStringTransformer(); } 因此处理器将收到Message String而不是接收Message File。 文件处理器是我们的自定义组件它将执行与打印文件内容一样高级的操作 public class FileProcessor {private static final String HEADER_FILE_NAME file_name;private static final String MSG %s received. Content: %s;public void process(MessageString msg) {String fileName (String) msg.getHeaders().get(HEADER_FILE_NAME);String content msg.getPayload();System.out.println(String.format(MSG, fileName, content));} }5建立流程 现在我们已经准备好所有必需的组件让我们构建流程。 我们正在使用Spring Integration Java DSL因为它使流程更具可读性 Bean public IntegrationFlow processFileFlow() {return IntegrationFlows.from(fileInputChannel).transform(fileToStringTransformer()).handle(fileProcessor, process).get();}Beanpublic MessageChannel fileInputChannel() {return new DirectChannel();}6运行应用程序 在我的目录中我已经有一个名为“ previousFile.txt”的文件。 启动应用程序后我们将创建两个文件并修改其中一个。 public static void main(String[] args) throws IOException, InterruptedException {SpringApplication.run(FileReadDirectoryApplication.class, args);createFiles(); }private static void createFiles() throws IOException, InterruptedException {createFile(file1.txt, content);createFile(file2.txt, another file);appendFile(file1.txt, modified); } 如果运行该应用程序则应该看到以下打印语句 previousFile.txt received. Content: previous content file1.txt received. Content: content file2.txt received. Content: another file file1.txt received. Content: content modified7结论 这个例子展示了使用Spring Integration从目录中读取文件非常简单显然是借助Spring Boot来简化配置。 根据您的需要您可以将自己的自定义过滤器添加到消息源或者使用Spring提供的另一个过滤器例如RegexPatternFileListFilter 。 您可以在此处检查其他实现。 如果您发现此帖子有用请分享或给我的存储库加注星标:) 我正在Google Plus和Twitter上发布我的新帖子。 如果您要更新新内容请关注我。 翻译自: https://www.javacodegeeks.com/2016/07/spring-integration-polling-file-creation-modification.html
http://www.zqtcl.cn/news/82506/

相关文章:

  • 网站不足之处石家庄北国商城
  • 南昌优化网站分析代做ppt
  • 济南网站建设销售招聘中国地图36个省的地图
  • 搜索引擎优化网站的网址wordpress提货下载
  • 网站建设seo基本要求做外贸网站那个平台好
  • 怎样登录韵网网站涂料网站建设
  • 学做网站要会哪些嘉兴建设网站
  • 湛江网站制作工具重庆市建设工程信息网官方
  • 网络建设网站有关知识蓝希菏泽网站建设
  • 设计网站的合同wordpress批量注册会员
  • 网站上截小屏幕 怎么做南昌网站做
  • 制作制作网站建设的那些网站可做国外零售
  • wordpress自建站哪里换logo阿里云网站目录
  • 万网做网站花多少钱中国空间站图片高清
  • 简约 网站国外外贸需求网站
  • 5G网站建设网站维护服务公司
  • 推广一个网站周期网站建设页面
  • 海南茶叶网站建设网站防护空间
  • 手机制作网站主页软件网页游戏前十名游戏
  • al万词推广网站引流宁乡做网站
  • 鲜花网站建设企划书htmi如何做网站
  • 罗湖网站-建设深圳信科住房和城乡建设网站方案
  • 卖域名的公司 骗做网站网站专题页优化
  • 做网站有个名字叫小廖网页界面设计一般步骤
  • 惠州网站建设哪里找湖北招聘网
  • 网站app搭建无锡谁做网站好
  • 莘县做网站推广用asp怎么做网站
  • 开发一个网站平台多少钱局门户网站的建设方案
  • 市场营销和网络营销网站云优化
  • 邯郸企业网站建设费用学电子商务专业可以从事哪些工作