Glassfish set max number of concurrent Batch Jobs

2019-07-23 19:17发布

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?

1条回答
我只想做你的唯一
2楼-- · 2019-07-23 19:25

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

  1. 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.

  2. 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
    
  3. 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
    
  4. 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

查看更多
登录 后发表回答