Skip to content

Commit 49c5e2c

Browse files
committed
Use available processor number for default thread count in consumer work service
The current value is available processors times 2, which may be overkill nowadays. The commit also allows using the rabbitmq.amqp.client.availableProcessors system property value, which is convenient as it's configuration-based (no code changes required). Fixes #730
1 parent 2720301 commit 49c5e2c

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/main/java/com/rabbitmq/client/impl/ConsumerWorkService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -22,19 +22,26 @@
2222
import java.util.concurrent.ThreadFactory;
2323

2424
import com.rabbitmq.client.Channel;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527

2628
final public class ConsumerWorkService {
29+
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerWorkService.class);
2730
private static final int MAX_RUNNABLE_BLOCK_SIZE = 16;
28-
private static final int DEFAULT_NUM_THREADS = Runtime.getRuntime().availableProcessors() * 2;
31+
private static final int DEFAULT_NUM_THREADS = Math.max(1, Utils.availableProcessors());
2932
private final ExecutorService executor;
3033
private final boolean privateExecutor;
3134
private final WorkPool<Channel, Runnable> workPool;
3235
private final int shutdownTimeout;
3336

3437
public ConsumerWorkService(ExecutorService executor, ThreadFactory threadFactory, int queueingTimeout, int shutdownTimeout) {
3538
this.privateExecutor = (executor == null);
36-
this.executor = (executor == null) ? Executors.newFixedThreadPool(DEFAULT_NUM_THREADS, threadFactory)
37-
: executor;
39+
if (executor == null) {
40+
LOGGER.debug("Creating executor service with {} thread(s) for consumer work service", DEFAULT_NUM_THREADS);
41+
this.executor = Executors.newFixedThreadPool(DEFAULT_NUM_THREADS, threadFactory);
42+
} else {
43+
this.executor = executor;
44+
}
3845
this.workPool = new WorkPool<>(queueingTimeout);
3946
this.shutdownTimeout = shutdownTimeout;
4047
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2022 VMware, Inc. or its affiliates. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.client.impl;
17+
18+
final class Utils {
19+
20+
private static final int AVAILABLE_PROCESSORS =
21+
Integer.parseInt(
22+
System.getProperty(
23+
"rabbitmq.amqp.client.availableProcessors",
24+
String.valueOf(Runtime.getRuntime().availableProcessors())));
25+
26+
static int availableProcessors() {
27+
return AVAILABLE_PROCESSORS;
28+
}
29+
30+
private Utils() {}
31+
}

0 commit comments

Comments
 (0)