Skip to content

Commit fc0b6bd

Browse files
authored
Expose additional thread pool options (#1420)
1 parent 7943b14 commit fc0b6bd

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@
173173

174174
<spring.batch.taskExecutor.corePoolSize>10</spring.batch.taskExecutor.corePoolSize>
175175
<spring.batch.taskExecutor.maxPoolSize>20</spring.batch.taskExecutor.maxPoolSize>
176+
<spring.batch.taskExecutor.queueCapacity>2147483647</spring.batch.taskExecutor.queueCapacity>
177+
<spring.batch.taskExecutor.threadGroupName></spring.batch.taskExecutor.threadGroupName>
178+
<spring.batch.taskExecutor.threadNamePrefix></spring.batch.taskExecutor.threadNamePrefix>
176179

177180
<!-- Sensitive Info settings -->
178181
<sensitiveinfo.admin.role>admin</sensitiveinfo.admin.role>

src/main/java/org/ohdsi/webapi/JobConfig.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import javax.annotation.PostConstruct;
44
import javax.sql.DataSource;
55

6+
import org.apache.commons.lang3.StringUtils;
67
import org.ohdsi.webapi.common.generation.AutoremoveJobListener;
78
import org.ohdsi.webapi.common.generation.CancelJobListener;
89
import org.ohdsi.webapi.job.JobTemplate;
910
import org.ohdsi.webapi.service.JobService;
1011
import org.ohdsi.webapi.shiro.management.Security;
12+
import org.ohdsi.webapi.util.ManagedThreadPoolTaskExecutor;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315
import org.springframework.batch.admin.service.*;
@@ -57,6 +59,15 @@ public class JobConfig {
5759

5860
@Value("${spring.batch.taskExecutor.maxPoolSize}")
5961
private Integer maxPoolSize;
62+
63+
@Value("${spring.batch.taskExecutor.queueCapacity}")
64+
private Integer queueCapacity;
65+
66+
@Value("${spring.batch.taskExecutor.threadGroupName}")
67+
private String threadGroupName;
68+
69+
@Value("${spring.batch.taskExecutor.threadNamePrefix}")
70+
private String threadNamePrefix;
6071

6172
@Autowired
6273
private DataSource dataSource;
@@ -68,9 +79,16 @@ public String batchTablePrefix() {
6879

6980
@Bean
7081
public TaskExecutor taskExecutor() {
71-
final ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
82+
final ThreadPoolTaskExecutor taskExecutor = new ManagedThreadPoolTaskExecutor();
7283
taskExecutor.setCorePoolSize(corePoolSize);
7384
taskExecutor.setMaxPoolSize(maxPoolSize);
85+
taskExecutor.setQueueCapacity(queueCapacity);
86+
if (StringUtils.isNotBlank(threadGroupName)) {
87+
taskExecutor.setThreadGroupName(threadGroupName);
88+
}
89+
if (StringUtils.isNotBlank(threadNamePrefix)) {
90+
taskExecutor.setThreadNamePrefix(threadNamePrefix);
91+
}
7492
taskExecutor.afterPropertiesSet();
7593
return taskExecutor;
7694
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.ohdsi.webapi.util;
2+
3+
import org.springframework.jmx.export.annotation.ManagedAttribute;
4+
import org.springframework.jmx.export.annotation.ManagedResource;
5+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
6+
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
import java.util.concurrent.BlockingQueue;
10+
import java.util.concurrent.ThreadPoolExecutor;
11+
12+
@ManagedResource
13+
public class ManagedThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
14+
15+
@ManagedAttribute
16+
@Override
17+
public int getCorePoolSize() {
18+
return super.getCorePoolSize();
19+
}
20+
21+
@ManagedAttribute
22+
@Override
23+
public int getMaxPoolSize() {
24+
return super.getMaxPoolSize();
25+
}
26+
27+
@ManagedAttribute
28+
@Override
29+
public int getKeepAliveSeconds() {
30+
return super.getKeepAliveSeconds();
31+
}
32+
33+
@ManagedAttribute
34+
@Override
35+
public int getPoolSize() {
36+
return super.getPoolSize();
37+
}
38+
39+
@ManagedAttribute
40+
@Override
41+
public int getActiveCount() {
42+
return super.getActiveCount();
43+
}
44+
45+
@ManagedAttribute
46+
public long getCompletedTaskCount() {
47+
48+
Optional<ThreadPoolExecutor> executor = Optional.ofNullable(getThreadPoolExecutor());
49+
return executor.map(ThreadPoolExecutor::getCompletedTaskCount).orElse(0L);
50+
}
51+
52+
@ManagedAttribute
53+
public int getLargestPoolSize() {
54+
55+
Optional<ThreadPoolExecutor> executor = Optional.ofNullable(getThreadPoolExecutor());
56+
return executor.map(ThreadPoolExecutor::getLargestPoolSize).orElse(0);
57+
}
58+
59+
@ManagedAttribute
60+
public long getTaskCount() {
61+
62+
Optional<ThreadPoolExecutor> executor = Optional.ofNullable(getThreadPoolExecutor());
63+
return executor.map(ThreadPoolExecutor::getTaskCount).orElse(0L);
64+
}
65+
66+
@ManagedAttribute
67+
public int getQueuedTaskCount() {
68+
69+
ThreadPoolExecutor executor = getThreadPoolExecutor();
70+
BlockingQueue<Runnable> queue = Objects.nonNull(executor) ? executor.getQueue() : null;
71+
return Objects.nonNull(queue) ? queue.size() : 0;
72+
}
73+
}

src/main/resources/application.properties

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ spring.batch.repository.tableprefix=${spring.batch.repository.tableprefix}
6969
spring.batch.repository.isolationLevelForCreate=${spring.batch.repository.isolationLevelForCreate}
7070
spring.batch.taskExecutor.corePoolSize=${spring.batch.taskExecutor.corePoolSize}
7171
spring.batch.taskExecutor.maxPoolSize=${spring.batch.taskExecutor.maxPoolSize}
72+
spring.batch.taskExecutor.queueCapacity=${spring.batch.taskExecutor.queueCapacity}
73+
spring.batch.taskExecutor.threadGroupName=${spring.batch.taskExecutor.threadGroupName}
74+
spring.batch.taskExecutor.threadNamePrefix=${spring.batch.taskExecutor.threadNamePrefix}
7275

7376
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
7477
server.port = ${server.port}

0 commit comments

Comments
 (0)