我有一条路由 (route1),它将数据发送到 HTTP 端点。为此,它必须设置授权 header 。 header 值每小时超时一次,必须更新。
为此,我创建了另一个路由 (route2),它使用提供的凭据 (getCredentials) 定期从 Web 服务获取访问 token 。这很好用。
如何使访问 token 可用于 route1?
我尝试过简单的局部变量、静态变量、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。
关于apache-camel - 我可以在同一个 Camel 上下文中的 Camel Routes 之间共享本地数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49130793/