TIBCO Thread Pool

Overview

This is a common component for AciveSpaces Transactions (AST) and Business Events Extreme (BE-X). It provides support for shared access to named thread pools within a JVM.

Please see the enclosed javadoc.

Usage

Each thread pool (java.util.concurrent.ExecutorService) is managed by an instance of a ThreadPoolService. If not specified by the application, the default pool is an ExecutorService with a fixed number of threads and an unbounded queue. The application can also provide an implementation of a ExecutorServiceFactory interface if the default pool isn't enough.

Here is an example of how to start managing a new named pool with 10 threads :-

  ThreadPoolService mySharkPoolService = 
    new ThreadPoolService("SharkPool", "A pool that ...", 10);

By not explicitly providing the number of threads in the constructor, the number of threads will be based on available processors to the JVM :-

  ThreadPoolService mySharkPoolService = 
    new ThreadPoolService("SharkPool", "A pool that ...");

Pool size ranges can also be used. For example to start with 5 threads, max of 15, keep-alive of 60 seconds and a bounded queue of 100 :-

  ThreadPoolService mySharkPoolService = 
    new ThreadPoolService("SharkPool", "A pool that ...", 5, 15, 60, 100);

Specifying a queue of 0 gives an un-bounded queue.

Here is an example of how to provide your own pool :-

  ThreadPoolService mySharkPoolService = 
    new ThreadPoolService(
        "SharkPool", "A pool that ...",
        new ExecutorServiceFactory() {

            @Override
            public ExecutorService create(ThreadFactory threadFactory) {
                return new ThreadPoolExecutor(
                    5,
                    10,
                    10,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue<Runnable>(10000),
                    threadFactory,
                    new ThreadPoolExecutor.DiscardPolicy());
                }
        }); 

In order to use the pool, it must be started :-

  mySharkPoolService.start();

Applications interact with the pool via the ThreadPool interface :-

  ThreadPool sharkPool = ThreadPoolLocator.findThreadPool("SharkPool");

The ThreadPool API provides access to the underlying ExecutorService instance. Here is an example of adding a job (Runnable) to the pool :-

    sharkPool.getExecutorService().execute(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello, I was added to the job queue immediately");
        }           
    });

In addition, the API also supports adding a job to the pool if, and only if, the current transaction commits. Here is an example :-

    sharkPool.executeOnCommit(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello, I was added to the job after commit");
        }
    });

Note: If the scheduled job need access to transactional data, then the application needs to explicitly create and initialize a Transaction object for executing the code. Here is an example :-

    sharkPool.getExecutorService().execute(new Runnable() {
        @Override
        public void run() {
            new Transaction() {
                @Override
                protected void run() throws Rollback {
                    // do transactional stuff...
                    //
                }
                
            }.execute();
        }
    });

Administration

The following administration targets are available :-

  • administrator display threadpool - displays the list of thread pools registered, their status and queue length