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

歡迎訪問 生活随笔!

生活随笔

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

javascript

spring 长轮询_Spring集成文件轮询和测试

發布時間:2023/12/3 javascript 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 长轮询_Spring集成文件轮询和测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring 長輪詢

我最近實施了一個小項目,在該項目中,我們必須輪詢文件夾中的新文件,然后在文件內容上觸發服務流。

Spring Integration非常適合此要求,因為它帶有一個通道適配器 ,該適配器可以掃描文件夾中的新文件,然后通過消息傳遞流程獲取文件。

本文的目的是介紹如何測試文件輪詢器流。

首先考慮使用以下Spring集成配置實現的簡單流程:

<beans:beans xmlns="http://www.springframework.org/schema/integration".....<channel id="fileChannel"></channel><channel id="processedFileChannel"></channel><int-file:inbound-channel-adapter directory="${inbound.folder}" channel="fileChannel" filename-pattern="*.*"><poller fixed-delay="5000"></poller></int-file:inbound-channel-adapter><service-activator input-channel="fileChannel" ref="fileHandlerService" method="handle" output-channel="processedFileChannel"></service-activator><logging-channel-adapter id="loggingChannelAdapter" channel="processedFileChannel"></logging-channel-adapter><beans:bean name="fileHandlerService" class="files.RealFileHandlingService"/><context:property-placeholder properties-ref="localProps" local-override="true"/><util:properties id="localProps"></util:properties> </beans:beans>

在圖中更好地表示了這一點:

流程簡單測試

現在要測試此流程,我的第一種方法是將示例文件放入類路徑中的文件夾中,在測試過程中動態發現并使用該文件夾名稱,并將最終消息寫入測試通道,并對進入該通道的消息進行斷言。 這就是組成原始Spring配置的測試配置的樣子:

<beans:beans xmlns="http://www.springframework.org/schema/integration"....<beans:import resource="si-fileprocessing.xml"/><util:properties id="localProps"><beans:prop key="inbound.folder">#{new java.io.File(T(java.io.File).getResource("/samplefolder/samplefile.txt").toURI()).parent}</beans:prop></util:properties><channel id="testChannel"><queue/></channel><bridge id="loggingChannelAdapter" input-channel="processedFileChannel" output-channel="testChannel"></bridge> </beans:beans>

測試看起來像這樣:

@Autowired @Qualifier("testChannel") PollableChannel testChannel;@Test public void testService() throws Exception{Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Test Sample file content")); }

這可以很好地工作,尤其要注意,可以使用Spring-EL表達式在配置中完全提供路徑:

#{new java.io.File(T(java.io.File).getResource("/samplefolder/samplefile.txt").toURI()).parent}

模擬服務

現在,進一步介紹一下,如果我想模擬正在處理文件的服務(files.RealFileHandlingService),該怎么辦。 一旦實現了模擬,這里就會出現一個問題。 注入模擬服務的一種方法是使用Mockito和它提供的幫助程序功能,以使用Spring配置文件通過這種方式創建模擬:

<beans:bean name="fileHandlerService" factory-method="mock" class="org.mockito.Mockito"><beans:constructor-arg value="files.FileHandlingService"></beans:constructor-arg> </beans:bean>

創建此模擬bean之后,可以通過以下方式在junit的@Before注釋方法中添加模擬行為:

@Before public void setUp() {when(fileHandlingServiceMock.handle((File)(anyObject()))).thenReturn("Completed File Processing"); }@Test public void testService() throws Exception{Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Completed File Processing")); }

如果現在再次進行測試,則令人驚訝的是它將失敗,原因是–在調用Junit的@Before方法之前,Spring應用程序上下文已完全初始化,并且輪詢進入文件夾的文件的輪詢程序已在模擬之前觸發行為被添加,因此,如果沒有正確的模擬服務行為,測試將失敗。

我確定還有其他修復程序,但是對我有用的修復程序實際上是控制掃描哪個文件夾,以及在什么時候將文件放置在文件夾中以觸發流。 這很大程度上基于我在Spring Integration項目本身中看到的一些測試 。 訣竅是先使用Spring config創建一個臨時文件夾,然后將該文件夾設置為輪詢文件夾:

<beans:bean id="temp" class="org.junit.rules.TemporaryFolder"init-method="create" destroy-method="delete"/><beans:bean id="inputDirectory" class="java.io.File"><beans:constructor-arg value="#{temp.newFolder('sitest').path}"/> </beans:bean> <util:properties id="localProps"><beans:prop key="inbound.folder">#{inputDirectory.getAbsolutePath()}</beans:prop> </util:properties>

現在,僅在測試運行期間將文件放置在臨時文件夾中,這樣@Before注釋方法就有機會向模擬添加行為,并且可以明確聲明模擬行為:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("si-test.xml") public class FileHandlingFlowTest {@Autowired @Qualifier("testChannel") private PollableChannel testChannel;@Autowired private FileHandlingService fileHandlingServiceMock;@Autowired @Qualifier("inputDirectory") private File inputDirectory;@Beforepublic void setUp() {when(fileHandlingServiceMock.handle((File)(anyObject()))).thenReturn("Completed File Processing");}@Testpublic void testService() throws Exception{Files.copy(new File(this.getClass().getResource("/samplefolder/samplefile.txt").toURI()), new File(inputDirectory, "samplefile.txt"));Message<?> message = this.testChannel.receive(5000);assertThat((String)message.getPayload(), equalTo("Completed File Processing"));} }

參考: all和其他博客中來自JCG合作伙伴 Biju Kunjummen的Spring集成文件輪詢和測試 。

翻譯自: https://www.javacodegeeks.com/2013/05/spring-integration-file-polling-and-tests.html

spring 長輪詢

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的spring 长轮询_Spring集成文件轮询和测试的全部內容,希望文章能夠幫你解決所遇到的問題。

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