Batch - Consuming Web Services
Most of the time the input for a batch workflow comes from databases or files. These are already very easily accessable through various libraries.
In the last years web services have gained much popularity and is one if not the most common way to exchange data. Most of the time the data is presented in JSON.
Object by Object
For very large sets of data it is very inconvenient to load the whole data set into memory before processing. I already made a post about how to process a JSON object by object by using the library Jackson, see Streaming data - the client side.
Authentication
Many web services are secured by something like OAuth 2.0 and OpenID Connect. OpenID Connect explicitly has a grant type for batch programs or M2M (machine to machine) communications:
Client Credentials
So an access token can be obtained from your OIDC provider just by passing a client id and a
client secret. To make this as easy as it sounds I created a new project which contains an
OidcTokenProvider. It just needs the configuration passed in the
MicroProfile Config
way like using a microprofile-config.properties file or using environment variables.
- batch.ws.oidc.client-id
- batch.ws.oidc.client-secret
- batch.ws.oidc.scope (default: openid)
- batch.ws.oidc.url (token URL)
Then the OidcTokenProvider can be injected and an access token obtained.
1 |
|
Multiple Token Provider
The library provides a prepackaged token provider which can be used out-of-the-box (with the
corresponding configuration). But sometimes we need to use multiple web services and each may
have their own OIDC provider or client id. That means we need two instances of OidcTokenProvider
with two different configurations.
This can be achieved with some lines of code. A new class needs to be created which extends
AbstractOidcTokenProvider.
1 |
|
By prefixing the config properties (prefix = "packaging") two seperate configuration instances
can exist side by side. And instead of batch.ws.oidc.url the configuration packaging.oidc.url
is used for this instance. And both token provider instances can be injected into the same bean.
1 |
|
Source Code
All those classes have been grouped together into the library batch-ws.
1 | <dependency> |
The source code is available at Bitbucket.
Wrap Up
Accessing web services in a batch prorgram can be very easy and even memory friendly. No need to dump the content to a local file and bother with CCSIDs and clean up strategies.
Happy coding!
Mihael