Java Executor服务连接池

时间:2017-03-03 07:52:21

标签: java connection-pooling executorservice apache-commons-dbcp scheduledexecutorservice

我正在尝试为Executor Service使用连接池。

当连接池配置为initialSize = 3,maxToal = 5,maxIdle = 5时,我遇到了一些问题。

我需要每分钟处理10个服务。但它每分钟只挑选5项服务。

如果我配置initialSize = 3,maxToal = 10,maxIdle = 10则每分钟选择10个服务..

我是多线程和连接的新手。以下是我的代码段。请提供建议。

public class TestScheduledExecutorService {
    public static void main (String a[]) {
        ScheduledExecutorService service = null;
        try {
            TestObject runnableBatch = new TestObject() {
                public void run() {
                    testMethod ();
                }
            };
            service = Executors.newSingleThreadScheduledExecutor();
            service.scheduleAtFixedRate(runnableBatch, 0, 30, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class TestObject implements Runnable{

    public void testMethod (int inc) {
        ExecutorService service = null;
        try {
            service = Executors.newFixedThreadPool(10);
            for (int i = 0; i < 10; i++) {
                service.submit(new TestService());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
    }
}

public class TestService implements Callable{

    Connection conn = null;

    public void process(Connection conn) {
        try {
            if (conn != null) {
                System.out.println("Thread & Connection pool conn : "+Thread.currentThread() + " :: " +conn);
                // service process here
            } else {
                System.out.println("Connection pool conn is null : ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }

    @Override
    public Object call() throws Exception {
        ConnectionPoolTest cp = ConnectionPoolTest.getInstance();
        BasicDataSource bds = cp.getBasicDataSource();
        conn = bds.getConnection();
        System.out.println(" call() "); **// it prints only 5 times for every minute eventhough total services are 10**
        process(conn);
        return null;
    }

}

public class ConnectionPoolTest {

private static ConnectionPoolTest dataSource = new ConnectionPoolTest();

    private static BasicDataSource basicDataSource = null;

    private ConnectionPoolTest() {  
    }

    public static ConnectionPoolTest getInstance() { 
        if (dataSource == null) 
            dataSource = new ConnectionPoolTest();
        return dataSource;
    }

    public BasicDataSource getBasicDataSource() throws Exception {
        try {
            basicDataSource = new BasicDataSource();

            basicDataSource.setInitialSize(3);
            basicDataSource.setMaxTotal(10);
            basicDataSource.setMaxIdle(10);
        } catch (Exception e) {
            throw e;
        }
        return basicDataSource;
    }

}

1 个答案:

答案 0 :(得分:2)

对于执行者服务

 initialSize : Specified Number of Threads to spin , when New executor is created.
 maxTotal    : Number of Threads that can exist at max peak load.
 maxIdle     : Number of Thread that are kept active even if load goes below threshold.

正如您所提到的,您希望并行拾取10个任务,我们应该将 maxTotal 设置为10. intialSize 可以配置为您的数字认为在开始时是最佳的,比方说3 - 5. maxIdle 是您希望保持活动的线程数,我们通常假设在提交任务时需要多少线程。虽然没有标准的推荐,但是可能会确定各种因素。

  1. 分钟内提交的任务分发
  2. 任务持续时间
  3. 并行执行这些任务的紧迫性。
  4. 正如您所提到的,您需要10个并行任务,然后您必须将10配置为maxTotal,考虑您的任务分配和持续时间导致重叠。如果持续时间非常小,并且分布均匀,那么您也可以使用较低的数字生存。