AsyncRestTemplate配置队列大小

时间:2017-01-22 12:31:53

标签: java spring multithreading asynchronous

我有一个每小时运行一次的工作,在40核服务器上,每个作业可以有1到10万个任务(需要大队列),每个任务在完成时执行HTTP请求,每个任务都很关键,这意味着它必须运行&安培;完整。

任务可以异步运行。

如何配置池中的线程数?如何配置队列大小?

在这个测试中我试图让我的任务被拒绝并充斥我的线程池但是我得到了SocketTimeoutException

public static void main(String[] args) throws IOReactorException {
    String url = "http://internal.server:8001/get";
    int connectionTimeout = 3000;
    int soTimeout = 3000;
    int maxHttpConnections = 30;

    IOReactorConfig customIOReactorConfig = IOReactorConfig.custom()
            .setIoThreadCount(Runtime.getRuntime().availableProcessors())
            .setConnectTimeout(connectionTimeout)
            .setSoTimeout(soTimeout)
            .build();

    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(customIOReactorConfig);

    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor);
    connManager.setDefaultMaxPerRoute(maxHttpConnections);
    connManager.setMaxTotal(maxHttpConnections);
    CloseableHttpAsyncClient customHttpAsyncClient = HttpAsyncClients.custom()
            .setConnectionManager(connManager)
            .build();

    HttpComponentsAsyncClientHttpRequestFactory asyncRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(customHttpAsyncClient);
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(asyncRequestFactory);

    System.out.println("start");
    for (int i = 0; i < 30_000; i++) {
        asyncRestTemplate.execute(url, HttpMethod.GET, request -> logger.info("doWithRequest..."), response -> {
            logger.info("extractData...");
            return response.getStatusText();
        }).addCallback(new ListenableFutureCallback<String>() {
            @Override
            public void onFailure(Throwable ex) {
                logger.error("onFailure [{}] [{}]", ex.getMessage(), ex.getStackTrace()[0].toString());
            }

            @Override
            public void onSuccess(String result) {
                logger.info("onSuccess");
            }
        });
    }
    System.out.println("end loop");
}

0 个答案:

没有答案