JBatch Alternative on IBM i

JBatch (or rather Jakarta EE Batch) is a nice and structured way of implementing a batch job. It has many features like partitioning a task, dynamic job flow, persisting job execution, restarting a job, … .

These are great features but not every batch job needs those features. From time to time you just need to process a database table. No need for partitioning, dynamic job flow or persisting the job execution. If the job fails you just run it again (after solving the problem ;-) ).

In those cases Jakarta EE Batch would be overkill.

The Alternative

Often the solution is already there. Ready to be discovered and used.

Java Streams

Stream from the java.util.stream package is an excellent lightweight alternative. It can also be integrated into a Jakarta EE Batch program (by using a stream in a Batchlet).

But JBatch and Streams are fundamentally different!

Depending on the perspective … yes, they are different. But they also try to achieve the same thing: Run a series of steps to process data.

A fundamental thing in Java Streams is the concept of data flowing - handed from one execution step to the next. That is a really nice concept. Jakarta EE Batch follows a different concept: Input -> Processing -> Output

The sad thing is that you can only have one processor in this chain of steps.

Only a single processor element may be specified. – Jakarta EE Batch Spec 2.0

It would be nice if you could chain multiple processors and pass the data from one processor to the next.

And that is where streams are much more versatile whereas Jakarta EE Batch has a rather rigid approach.

When using streams we need to deal with functions. I won’t deep dive into functions here as it is a really big topic. We will just scratch those parts which matter for this example.

Our Use Case

Our example will be straight from the manufacturing department. We are producing some goods and these goods need to be packaged. We have several lines and workplaces for packaging and depending on the type and size of the product we need to assign it to the corresponding workplace.

We have a single table as input which contains all manufactured goods for the next production days and we have another table which receives the output data.

Rules

We have five workplaces for packaging.

  • Workplace 1 : normal packaging (hall 1)
  • Workplace 2 : normal packaging (hall 1)
  • Workplace 3 : oversize packaging (hall 1)
  • Workpalce 4 : fragile packaging (hall 1)
  • Workplace 5 : packaging for production hall 2

So not all goods can be packaged at every workplace. We need to setup rules by which the goods are assigned to the corresponding workplaces.

  1. fragile product → workplace 4
  2. oversize product → workplace 3
  3. line 5 (hall 2) → workplace 5
  4. else → distribute evenly between workplace 1 and 2

Input

In Jakarta EE Batch we would implement an ItemReader. When using streams the input comes from a java.util.stream.Stream implementation. Luckily we won’t have to implement this ourselves.

We will be using the Jdbi project for reading the data from the database. Jdbi provides a nice feature where you can more or less just write the SQL and it will make a nice POJO from the data. If we want to fetch a set of data we would just define a java.util.List as the return type and we would get all the data in one step. That may be convenient with small sets of data but when you are processing millions of rows you are running out of memory faster than you can count to three.

The solution for this is to use java.util.stream.Stream as the return type. Using a Stream will only load the next row of data into memory and is much better when it comes to conserving resources.

1
2
3
4
5
6
@SQLQuery("""
SELECT id, itemId, productionDay, category, line, width, height, depth, fragile
FROM productionPlanning
""")
@RegisterBeanMapper(ProductionPlanEntry.class)
Stream<ProductionPlanEntry> listPlannedProduction();

Processor

We got the stream we want to operate on. The main methods we need from a Stream object are map, filter and collect.

filter : We get an object of class x. Our function determines if the object will be dropped from the stream. Just return false for dropping the object from the stream.
map : We get an object of class x. We can modify the received object to our liking. Our function can freely decide what to return. It can be the same object, another object (even of another class) or null.
collect : This is a terminal operation which means that this function will receive all objects from the stream and process them in this final step. There is no further processing.

We can create a function for each rule we want to implement. But as the rules are very miniscule we just put them all into one function. We can use anonymous functions or implement them as a class.

There are multiple Java interface for functions. Here are the most common ones:

  • java.util.function.Function : Accepts an object and returns an object. The passed and returned object doesn’t need to be the same object or even of the same class. This can be used by the map method of a Stream.
  • java.util.function.Predicate : Accepts an object and returns a boolean result. This can be used by the filter method of a Stream.

There are many more interface (like Consumer, Supplier, …) but those are of no interest to us for this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class PackagingWorkplaceSelector implements Function<ProductionPlanningEntry, ProductionPlanningEntry> {

private final long OVERSIZED = 120000000l; // volume in millimeter³
private int lastDefaultPackagingWorkplace = 0;

@Override
public ProductionPlanningEntry apply(ProductionPlanningEntry entry) {

if (entry.isFragile())
entry.setPackagingWorkplace(4);
else if (isOversized(entry))
entry.setPackagingWorkplace(3);
else if (entry.getLine() == 5)
entry.setPackagingWorkplace(5);
else
entry.setPackagingWorkplace(getNextDefaultWorkplace());
return entry;
}

private Integer getNextDefaultWorkplace() {
lastDefaultPackagingWorkplace = lastDefaultPackagingWorkplace == 1 ? 2 : 1;
return lastDefaultPackagingWorkplace;
}

private boolean isOversized(ProductionPlanningEntry entry) {
return entry.getVolume() >= OVERSIZED;
}

}

Output

Writing the data to a file or table can be done in a mapping function. We use Jdbi for writing the data to an output table.

1
2
3
4
5
@SqlUpdate("""
INSERT INTO productionPlanningPackaging (itemId, productionDay, packagingWorkplace)
VALUES(:itemId, :productionDay, :packagingWorkplace)
""")
void insertPackagingWorkplace(@BindBean ProductionPlanningEntry entry);

Putting it all together

Putting all those parts together may look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class ProductionWorkflow implements Batchlet {

private final Logger logger = Logger.getLogger(this.getClass().getName());

@Inject
@BatchProperty(name = "database")
public String database;

@Override
public String process() throws Exception {
Jdbi jdbi = CDI.current().select(Jdbi.class, NamedLiteral.of(database)).get();

try (Handle handle = jdbi.open()) {
ProductionPlanningDao dao = handle.attach(ProductionPlanningDao.class);
Map<Integer, List<ProductionPlanningEntry>> groupedWorkplaces = dao.stream()
.map(new VolumeCalculator())
.map(new PackagingWorkplaceSelector())
.map(new PackagingWorkplaceWriter(dao))
.collect(Collectors.groupingBy(ProductionPlanningEntry::getPackagingWorkplace));

for (Integer key : groupedWorkplaces.keySet()) {
logger.info(key + ": " + groupedWorkplaces.get(key).size());
}
}

return "ok";
}

@Override
public void stop() throws Exception {
// nothing to do
}

}

Output:

1
2
3
4
5
2025-12-22 10:48:02 INFORMATION rpgnextgen.packaging.ProductionWorkflow: 1: 427
2025-12-22 10:48:02 INFORMATION rpgnextgen.packaging.ProductionWorkflow: 2: 426
2025-12-22 10:48:02 INFORMATION rpgnextgen.packaging.ProductionWorkflow: 3: 36
2025-12-22 10:48:02 INFORMATION rpgnextgen.packaging.ProductionWorkflow: 4: 100
2025-12-22 10:48:02 INFORMATION rpgnextgen.packaging.ProductionWorkflow: 5: 11

I planned to support the use of different databases in this demo and thus the database gets injected, see batch property database. Depending on the database we get the corresponding Jdbi provider.

The different functions are chained in the stream and as a final step the number of products for each workplace are summed up.

Miscellaneous

Sometimes we have processed all objects in the stream in a filter or mapping function and just want to just drop all objects after our last function. filter and map are intermediate operations. We need to make a final call on the stream with a terminal operation. There is no terminal operation like sink, drop or dump but you can use count which is probably the fastest terminal operation.

If we want to make sure that no null values are passed down the stream we can use stream.filter(Objects::notNull) to filter out all null values.

Source Code

The source code for this example is available at Bitbucket.

Wrap Up

The JBatch example project was a good starting point for using streams in a batch process. We get a lot of benefits from it like CDI and argument processing and it works but we could make it even easier and more straightforward if we drop the Jakarta EE stuff and just use the streams (and perhaps Weld for CDI ;-) ). This sounds like a topic for the next blog post ;-).

… and if you are interested in using streams in RPG there is actually an implementation at Bitbucket. I have written a post about it a while ago.

Happy streaming!

Mihael