Search code examples
javaserverwebspherewebsphere-liberty

Achieving concurrency in IBM liberty server


I am working on IBM-liberty server where I need to implement concurrency for 5 threads. I have gone through the some of the links which are available on IBM's official website. But not able to understand how should I configure server.xml, so that whenever I deploys my Java application onto the liberty server 5 thread will run my Java component concurrently. Could anyone please help me with the solid example really struggling on it. Following is my codebase,

I have modified my server.xml with following changes,

<featureManager>
      <feature>concurrent-1.0</feature>
    </featureManager>

    <managedScheduledExecutorService jndiName="concurrent/scheduledExecutor1">
        <contextService jndiName="concurrent/threadContextSvc2"/>
        <concurrencyPolicy max="2"/>
    </managedScheduledExecutorService>

web.xml

<resource-env-ref>
        <resource-env-ref-name>concurrent/scheduledExecutor1</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.concurrent.ManagedScheduledExecutorService</resource-env-ref-type>
    </resource-env-ref>
    <resource-env-ref>
        <resource-env-ref-name>concurrent/threadContextSvc2</resource-env-ref-name>
        <resource-env-ref-type>javax.enterprise.concurrent.ContextService</resource-env-ref-type>
    </resource-env-ref>

main() method

InitialContext initialContext = new InitialContext();
            executor = (ManagedScheduledExecutorService) initialContext.lookup("concurrent/scheduledExecutor1");
            executor.submit(new OrderProcessCI());

run() method

public void run() {
        System.out.println(" Thread name :: "+Thread.currentThread().getName()+" Thread id :: "+Thread.currentThread().getId());
    }

In above code it seems that only single thread running.. As when I tries to hit the break point in run method the control comes only single time. Sysout gets printed only once. How can I make this for atleast 5 threads. I also checked about ThreadPool concept but how can I achieve it in liberty. Could anyone please help me with any example.


Solution

  • You're only seeing a single thread because you're only submitting a single task to the executor executor.submit(new OrderProcessCI());, if you want 5 of those tasks, somehow you'll need to submit 5 instances to the executor. Are you intentionally using ManagedScheduleExecutorService because you need to schedule reoccurring or future jobs? If you only need to run tasks, change to use ManangedExecutorService instead. Since you don't appear to changing the thread context, omit it from config and let the executor use its default. Given you stated "5 threads", I'm assuming you want exactly that many (the preferred method to let the executor figure out how many it needs), you need to do this in server.xml <concurrencyPolicy max="5" maxPolicy="strict"/>