其目标是使可重用的变量从主体中提取出来,以便在通过路由进行其他转换时也使用它。更具体地说,目的是从接口获得一个令牌,并使用它进行进一步的访问(如图像中的)。
所需经费如下:
发布于 2021-01-07 16:41:44
为了保存它,可以使用一种叫做咖啡因的缓存成分。
以下是实现这一目标的一些有用的关键步骤:
//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秒的过程。
这里直接链接了这个组件的文档:
还有一个有用的例子:
https://stackoverflow.com/questions/65615853
复制相似问题