簡體   English   中英

Apache Camel文件組件 - 如何僅掃描新文件?

[英]Apache Camel File Component - how to scan for new files only?

我在Scala中設置了一個簡單的Apache Camel Route,如下所示:

import org.apache.camel.builder.RouteBuilder
import akka.actor.ActorRef
import akka.camel._

    class SimpleRouteBuilder(val responder: ActorRef) extends RouteBuilder  {
      def configure  {
        from("file:C:\\datafiles\\input?noop=true&readLock=changed").to(responder)
      }
    }

我的問題是如何掃描新文件 現在,它為目錄中的每個文件發送一條消息。 我想以這樣的方式讓它只發送我的camel應用程序啟動創建的文件的消息。

干杯!

更新

好的我找到了一個解決方案...實現一個時間戳actor,其唯一目的是檢查(文件創建時間>應用程序啟動時間)並將文件消息轉發給循環路由器。

現在的路線是:

 from(s"file:$directoryName?noop=true&readLock=changed&delay=$delay&recursive=true&include=$include").
  convertBodyTo(classOf[java.io.File]).to(timestampActor)

我的時間戳演員的代碼是:

object TimestampActor {
  def apply(timeMillis0: Long): Props = Props(new TimestampActor(timeMillis0))
}

// timeMillis0 is the time when the application has started
class TimestampActor(timeMillis0: Long ) extends Actor {

  val messageRouterActor: ActorRef = context.system.actorFor(s"akka://${Constants.ActorSystemName}/user/${Constants.MessageRouterActorName}")
  val logger: Logger = LoggerFactory.getLogger(classOf[TimestampActor])


  def receive = {

      case camelMessage: CamelMessage => {
       // Need to unbox
        self ! camelMessage.body.asInstanceOf[java.io.File]
      }

      case file: File => {
        try {
          val attrs: BasicFileAttributes  = Files.readAttributes(file.toPath, classOf[BasicFileAttributes])
          if (attrs.creationTime().toMillis  >= timeMillis0) {
            // Forward the message to the Round Robin message router
            messageRouterActor ! file
          }
        }
        catch {
          case ioe:IOException => {
            logger.error(s"Failed to get creation time for file ${file.getCanonicalPath}", ioe)
            // Retry in 20 minutes
            import context.dispatcher
            context.system.scheduler.scheduleOnce(Duration.create(20, TimeUnit.MINUTES), self, file)

          }
        }
      }
  }

}

您需要使用Apache Camel文件組件idempotent選項。 它將記住已處理的文件,並僅使用新文件。 默認情況下,它會記住1000個條目,密鑰將是文件的絕對路徑,但您可以將其更改為您需要的內容。 它會是這樣的:

from("file:C:\\datafiles\\input?noop=true&readLock=changed&idempotent=true").to(responder)

此外,您可以使用不同類型的Idempotent存儲庫來做更多花哨的東西。

如果您擔心丟失原始文件,我建議您使用多播。 這樣,您可以將文件的副本發送到備份文件夾。

from("file:C:\\datafiles\\input").multicast().to("backuplocation", "responder");

默認情況下,camel將處理放置在輸入文件夾中的任何文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM