GlassFish allows you to tune the global EJB pool size, as well as specific pool sizes per EJB. By setting the max-pool-size
under glassfish-ejb-jar.xml
I can control how many instances of a EJB can be used in parallel.
<glassfish-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>MyExpensiveEJB</ejb-name>
<bean-pool>
<max-pool-size>10</max-pool-size>
</bean-pool>
</ejb>
</enterprise-beans>
</glassfish-ejb-jar>
I wonder if something similar can be done with the Java EE Batch API. I would like to configure the max number of parallel jobs (either globally or to a specific job) so that calls to BatchRuntime.getJobOperator().start()
enqueues job executions after a specified limit.
Anyone knows if this is possible?
Answering my own question for further reference.
Problem
By default GlassFish Batch Runtime uses concurrent/__defaultManagedExecutor
as its executor service.
You can retrieve its properties with:
./asadmin get resources.managed-executor-service.concurrent/\
__defaultManagedExecutorService.*
In my environment I get the following values:
context-info=Classloader,JNDI,Security,WorkArea
context-info-enabled=true
core-pool-size=0
deployment-order=100
enabled=true
hung-after-seconds=0
jndi-name=concurrent/__defaultManagedExecutorService
keep-alive-seconds=60
long-running-tasks=false
maximum-pool-size=2147483647
object-type=system-all
task-queue-capacity=2147483647
thread-lifetime-seconds=0
thread-priority=5
Notice thatmaximum-pool-size=2147483647
, this is a unlimited managed executor.
Solution
Create your own Managed Executor Service.
./asadmin create-managed-executor-service --maximumpoolsize=20 \
--taskqueuecapacity=5000 --longrunningtasks=true concurrent/myJobExecutor
This will limit the pool to 20 concurrent threads. If all threads are currently in use the executor service will queue up to 5000 tasks before rejecting new entries.
Unfortunately the last option(--longrunningtasks=true
) did not work in my environment. This flag is important, it prevents long running tasks to be hung. I had to set the property manually:
./asadmin set resources.managed-executor-service.concurrent/\
myJobExecutor.long-running-tasks=true
Tweak the batch runtime configuration to use your new Managed Exercutor Service (I had to set the datasource as well):
./asadmin set-batch-runtime-configuration --executorservicelookupname \
concurrent/myJobExecutor --datasourcelookupname jdbc/__TimerPool
Fire your jobs and enjoy nice, sane, parallelism.
You can find further information about Configuring the Batch Runtime and Configuring Managed Executor Services in the GlassFish Administration Guide