In order to save it, it is possible to use a Cache component called Caffeine.
In the following, some useful key steps to accomplish the goal:
//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
This is the procedure to save the token as global variable and make it available for 60 seconds.
Here a direct link of the documentation of this component:
And one useful example: