JBatch Streams - Refined!

The last post was about the low cost alternative Java Streams for batch processing. But the stream processing was pressed into a Jakarta EE Batchlet which has some nice side benefits but also some overhead which I would like to get rid off in this post.

Kick Jakarta EE Batch

First I kicked out Jakarta EE Batch. This was not too difficult and in hindsight the number of removed libraries and dependencies was surprisingly small.

Replacements

But removing the dependencies from the Maven POM was the least effort in this milestone. I now had to replace some of the functionality Jakarta EE Batch brings to the table.

  • Batch Properties Injection
  • Workflow Configuration
  • Workflow Starter

Batch Properties Injection

Instead of having a separate mechanism for injecting properties I used MicroProfile Config for injecting values. I added a new ConfigSource implementation which delivers the values passed as batch properties via the command line. The only thing one needs to take care of is using a Provider instead of the concrete type because at the time the ConfigSource instance is created the command line parameters haven’t been evaluated yet.

1
2
3
@Inject
@ConfigProperty(name = "database")
private Provider<String> database;

As MicroProfile Config is used for providing injectable values I can override the parameters passed on the command line with values from a ConfigSource rated higher than the one for the batch properties.

The ordering is as follows:

  • 100 : microprofile-config.properties file
  • 200 : Batch properties
  • 300 : Environment variables
  • 400 : Java system properties

So if every ConfigSource provides a value for the same property the batch properties passed on the command line would trump the value in the microprofile-config.properties file. The value from the environment variables ConfigSource would trump the batch properties and the Java system property would trump them all.

Workflow Configuration

Jakarta EE Batch supports many batch scenarios and features, but I only intend to support a simple batch job scenario and don’t need all those additional capabilities.

The result is a workflow which declares an array of steps that should be executed. So the developer only needs to implements two interfaces: Workflow and WorkflowStep. All declared workflow steps will be executed sequentially in the specified order.

Optionally you can also implement only the WorkflowStep interface if you just only have one step anyway. No need to also implement a Workflow.

Workflow Starter

Jakarta EE Batch uses XML to define the workflow and also the starting point of the workflow. In this little batch implementation we are using CDI to mark possible starting points with the Named annotation.

1
2
3
4
5
6
7
8
9
10
11
@ApplicationScoped
@Named("packaging")
public class PackagingWorkflow implements TypedWorkflow<Void> {

@SuppressWarnings("unchecked")
@Override
public Class<? extends TypedWorkflowStep<Void>>[] steps() {
return new Class[] { DatabaseInitializer.class, PackagingWorkplaceSelectionStep.class };
}

}

The workflow name packaging is passed on the command line as a second parameter to the application.

1
java -cp production-planning.jar:libs/* rpgnextgen.batch.Main start packaging --database h2

The batch parameters/properties are passed as additional parameters. The parameter name is prefixed with two hyphens followed by the parameter value.

The workflow name can also be prefixed with workflow: which identifies it as a workflow name. We can also directly start a specific step by providing a step name prefixed with step:

1
java -cp production-planning.jar:libs/* rpgnextgen.batch.Main start step:packaging-workflow-selection

The java class for the step must be annotated with Named and it must also be available as a CDI bean.

1
2
3
4
5
@ApplicationScoped
@Named("packaging-workplace-selection")
public class PackagingWorkplaceSelectionStep implements TypedWorkflowStep<Void> {
...
}

By using CDI and Named we can package multiple workflows into one application and choose the workflow at application start by passing the corresponding workflow or step name.

Dependencies

We only declared 10 dependencies and many of those dependencies are about the database and with all the transitive dependencies we are at 32 libraries in total. That is very maintainable compared to other programming languages where even for the simplest things you got 100(s) of dependencies.

And the maintainers of the dependencies …?

  • Apache Software Foundation
  • Eclipse Foundation
  • IBM
  • JBoss (Red Hat)
  • OW2 (Non Profit Community)
  • SmallRye (Red Hat)

There are only 2 libraries which doesn’t belong to any big organization:

  • Jdbi
  • GeanTyRef

Compare that to the dependency graph of other programming languages.

Source Code

The source code for this example is available at Bitbucket.

Wrap Up

What has been achieved by removing Jakarta EE Batch:

  • more generic approach to batch processing
  • more flexibility
  • better runtime performance
  • chaining
  • less dependencies

Is it a replacement for Jakarta EE Batch?

Definitely not! Jakarta EE Batch brings so much to the table that it is by far no general replacement for it.

But if you want to develop a small batch programm in Java and you don’t need all the bells and whistles of Jakarta EE Batch then it may be a good alternative.

Happy streaming!

Mihael