1

The goal is to make reusable a variable taken from the body in order to use it also for other transitions through the route. More specific, the intent was to obtain a token from the interface and using it for further accesses as in the image.Flow chart

The Requirements were:

  1. Keeping the variable saved for a settable time.
  2. Managing of the variable in order to obtain it when required.
Logistics
  • 21
  • 1
  • 3

1 Answers1

1

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:

Logistics
  • 21
  • 1
  • 3