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
2
3
4
5
6
@Inject
private OidcTokenProvider tokenProvider;

...

String accessToken = tokenProvider.get();

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
2
3
4
5
6
7
8
9
@ApplicationScoped
public class PackagingOidcTokenProvider extends AbstractOidcTokenProvider {

@Inject
public PackagingOidcTokenProvider(@ConfigProperties(prefix = "packaging") OidcTokenProviderConfig config) {
super(config);
}

}

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
2
3
4
5
@Inject
private OidcTokenProvider tokenProvider;

@Inject
private PackagingOidcTokenProvider packagingTokenProvider;

Source Code

All those classes have been grouped together into the library batch-ws.

1
2
3
4
5
<dependency>
<groupId>com.rpgnextgen</groupId>
<artifactId>batch-ws</artifactId>
<version>[1.0.0,2.0.0)</version>
</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