【问题标题】:Using of Cache component in order to save variables as global in Apache Camel使用缓存组件以在 Apache Camel 中将变量保存为全局变量
【发布时间】:2021-01-07 16:04:59
【问题描述】:

我们的目标是使从主体中获取的变量可重用,以便将它也用于路径中的其他转换。更具体地说,目的是从接口获取令牌并使用它进行进一步的访问,如图所示。Flow chart

要求是:

  1. 在可设置的时间内保存变量。
  2. 管理变量以便在需要时获取它。

【问题讨论】:

    标签: java apache-camel caffeine-cache


    【解决方案1】:

    为了保存它,可以使用一个叫做 Caffeine 的 Cache 组件。

    以下是实现目标的一些有用的关键步骤:

    //get of the token from the cache
    .setHeader(CaffeineConstants.ACTION, constant(CaffeineConstants.ACTION_GET))
    .setHeader(CaffeineConstants.KEY, constant("<KEY>")))
    .toF("caffeine-cache://%s", cacheName?evictionType=TIME_BASED&expireAfterWriteTime=60) //options settings
    
    
    .choice()
          //if is not valid
          .when(header(CaffeineConstants.ACTION_HAS_RESULT).isEqualTo(Boolean.FALSE))
                    .to("direct-some-external-service") //token obtaining
                    
          // save resulting token into cache
                    .setHeader(CaffeineConstants.ACTION, constant(CaffeineConstants.ACTION_PUT))
                    .setHeader(CaffeineConstants.KEY, constant(constant(<KEY>")))
                    .toF("caffeine-cache://%s", cacheName?evictionType=TIME_BASED&expireAfterWriteTime=60)
                    .otherwise()
    .end()
    
    //some other steps
    

    这是将令牌保存为全局变量并使其可用 60 秒的过程。

    这里是该组件文档的直接链接:

    还有一个有用的例子:

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多