日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Integration –轮询文件的创建和修改

發布時間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Integration –轮询文件的创建和修改 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>xpadro.spring.integration</groupId><artifactId>file-read-directory</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>file-read-directory</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.5.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- Spring Integration - Java DSL --><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-java-dsl</artifactId><version>1.0.0.RELEASE</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-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 MessageSource<File> fileReadingMessageSource() {CompositeFileListFilter<File> 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(Message<String> 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 modified

7結論

這個例子展示了使用Spring Integration從目錄中讀取文件非常簡單,顯然是借助Spring Boot來簡化配置。 根據您的需要,您可以將自己的自定義過濾器添加到消息源,或者使用Spring提供的另一個過濾器,例如RegexPatternFileListFilter 。 您可以在此處檢查其他實現。

如果您發現此帖子有用,請分享或給我的存儲庫加注星標:)

我正在Google Plus和Twitter上發布我的新帖子。 如果您要更新新內容,請關注我。

翻譯自: https://www.javacodegeeks.com/2016/07/spring-integration-polling-file-creation-modification.html

總結

以上是生活随笔為你收集整理的Spring Integration –轮询文件的创建和修改的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。