2017-05-04 196 views
0

在我的Spring Boot應用程序中,我必須實現一個導入服務。用戶可以提交一堆JSON文件,應用程序將嘗試從這些文件中導入數據。根據JSON文件的數據量,單個導入過程可能需要1或2個小時。Spring Boot和長時間運行的任務

我不想在導入過程中阻止用戶,所以我打算接受導入任務並通知用戶此數據計劃處理。我將把數據放入隊列中,另一端的空閒隊列使用者將開始導入過程。另外,我需要有可能監視隊列中的作業,並在需要時終止它們。

現在我正在考慮使用嵌入式Apache ActiveMQ來引入消息生成器和消費者邏輯,但在此之前,我想問一下從架構的角度來看,它是上述任務的一個好選擇,還是它可以用更合適的工具來實現..例如普通彈簧@Async等等?

回答

1

有可能與駱駝兼治文件這樣

from("file://incoming?maxMessagesPerPoll=1&idempotent=true&moveFailed=failed&move=processed&readLock=none").threads(5).process() 

看看http://camel.apache.org/file2.html

,但我認爲這是更好地爲您的要求使用獨立的ActiveMQ,一個獨立的服務將文件移動到ActiveMQ和獨立使用者,以便能夠獨立地終止或重啓每個文件。

最好是如你所說使用ActiveMQ的,你可以很容易地創建一個服務將郵件移動到隊列駱駝這樣的:

 CamelContext context = new DefaultCamelContext(); 
     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=true"); 
     context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); 
     context.addRoutes(new RouteBuilder() { 
      public void configure() { 
      // convertBodyTo to use TextMessage or maybe send them as file to the Queue   from("file://testFolderPath").convertBodyTo(String.class).to("test-jms:queue:test.queue"); 
      } 
     }); 
     context.start(); 

下面一些例子

http://www.programcreek.com/java-api-examples/index.php?api=org.apache.camel.component.jms.JmsComponent

https://skills421.wordpress.com/2014/02/08/sending-local-files-to-a-jms-queue/

https://github.com/apache/camel/blob/master/examples/camel-example-jms-file/src/main/java/org/apache/camel/example/jmstofile/CamelJmsToFileExample.java

https://github.com/apache/camel/tree/master/examples

監控和管理,你可以使用JMX與VisualVM的或Hawtio http://hawt.io/getstarted/index.html

http://camel.apache.org/camel-jmx.html

吞噬你可以在隊列中使用使用DefaultMessageListenerContainer併發消費者,爲此,你需要改變prefetchPolicy在DefaultMessageListenerContainer的ConnectionFactory上,Multithreaded JMS client ActiveMQ

相關問題