0

用例:当用户点击端点时,我们生成一个 csv 文件,我们希望将其上传到不同的源(FTP 和 S3)。我们需要在上传之前对文件进行加密和转换。以下是路线的样子(过于简化):

FTP 路由:

from("file:temp?include=sample.csv&noop=true")
.routeId("ftpRoute" + LocalDateTime.now())
.marshal().pgp("file:temp/encryptionKey.asc", "someuserid", null, true, true)
.process(new SomeComplexProcessor())
.to("sftp:localhost:22/destinationDir?username=username&password=RAW(password)")
.setHeader(FILE_NAME, "metadata.txt")
.process(new MetadataFileGenerator())
.marshal()
.bindy(BindyType.Csv, MetadataFile.class)
.to("sftp:localhost:22/destinationDir?username=username&password=RAW(password)")
.process(new KillRouteProcessor());

S3路线:

from("file:temp?include=sample.csv&noop=true")
.routeId("s3Route" + LocalDateTime.now())
.marshal().pgp("file:temp/encryptionKey.asc", "someuserid", null, true, true)
.process(new SomeComplexProcessor())
.to("aws-s3:bucketName?accessKey=ACCESS_KEY&secretKey=RAW(SECRET_KEY)&region=REGION")
.setHeader(FILE_NAME, "metadata.txt")
.process(new MetadataFileGenerator())
.marshal()
.bindy(BindyType.Csv, MetadataFile.class)
.to("aws-s3:bucketName?accessKey=ACCESS_KEY&secretKey=RAW(SECRET_KEY)&region=REGION")
.process(new KillRouteProcessor());

工作原理:S3 和 SFTP 上传路由正常工作。

要求:

  1. 如果代码可以共享就太好了。两条路线中都可能存在一些不同的参数/处理器。
  2. 两个路由都执行后需要删除文件。
  3. 我们需要在上传后终止路由,因为每个请求都有不同的文件名。
  4. 以上两条路线不能合并为一条,因为实际路线中有太多的 if-else 条件,这会使情况更加复杂。
  5. 路由应该是可选的(例如,选择上传到 s3/sftp 或两者都应该可用)

我试过的:

  1. Camel Direct:它有助于代码重用,但它不允许多个消费者(在我的情况下这两条路线)
  2. Camel Seda:它允许多个消费者,但似乎不允许同步路由。
  3. 删除骆驼上下文之外的文件。这是一个问题,因为我们不知道上传文件需要多少时间。

环境:
Camel 3.4.3、Spring Boot 2.2.3、Java8、

4

1 回答 1

0

我设法通过使用静态路由 id (Removed LocalDateTime.now()) 让它工作。这是我在修复 Camel 问题时学到的知识。

  1. 始终为每条路线提供路线 ID。特别是在使用“直接”路线时。
  2. 永远不要使用动态路由 ID。这个非常重要。当我LocalDateTime.now()在路由 ID 中使用时,我看到了这个问题。我在更改之前看到了这个错误:

不允许同一端点的多个使用者:direct://routeName .....

  1. 使用循环或调用任何直接路由时,请始终使用丰富。

例如。.enrich("direct:subroute", AggregationStrategies.useOriginal()).
这会将父路由的标头副本共享给子路由。它将帮助您避免一些奇怪的问题。

如果您想了解更多信息,请随时发表评论。

于 2020-08-29T10:35:46.173 回答