我有一个路由(route1),它将数据发送到HTTP端点。为此,它必须设置授权标头。标头值每小时超时,必须续订。
为此我创建了另一个路由(route2),它使用提供的凭据(getCredentials)定期从Web服务获取访问令牌。这很好用。
如何为access1提供访问令牌?
我尝试过简单的局部变量,静态变量,AtomicReference变量(volatile和static ......)
我的代码(为了便于阅读而缩短):
public class DataRoute extends RouteBuilder{
volatile static AtomicReference<String> cache = new AtomicReference<>();
@Override
public void configure() throws Exception {
from("timer://test?period=3500000")
.routeId("route2")
.setHeader("Authorization", constant(getCredentials()))
.to("http://127.0.0.1:8099/v1/login")
.process(exchange -> {
cache.set(parseAuthString(exchange.getIn().getBody(String.class)));
});
... other route producing for direct:rest
from("direct:rest")
.routeId("route1")
.setHeader("Authorization",constant((cache.get()==null?"":cache.get())))
.to("http://localhost:8099/v1/shipment");
}
}
缓存的值始终为空...
不要使用constant
来设置动态值,它只是一次性CONSTANT。
而是使用内联处理器(您可以使用java 8 lambda)或消息转换/ setBody与处理器。