• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.3
today-context/src/main/java/infra/scheduling/config/TaskExecutorFactoryBean.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.scheduling.config;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.concurrent.RejectedExecutionHandler;
23

24
import infra.beans.factory.BeanNameAware;
25
import infra.beans.factory.DisposableBean;
26
import infra.beans.factory.FactoryBean;
27
import infra.beans.factory.InitializingBean;
28
import infra.core.task.TaskExecutor;
29
import infra.scheduling.concurrent.ThreadPoolTaskExecutor;
30
import infra.util.StringUtils;
31

32
/**
33
 * {@link FactoryBean} for creating {@link ThreadPoolTaskExecutor} instances,
34
 * primarily used behind the XML task namespace.
35
 *
36
 * @author Mark Fisher
37
 * @author Juergen Hoeller
38
 * @since 4.0
39
 */
40
public class TaskExecutorFactoryBean
3✔
41
        implements FactoryBean<TaskExecutor>, BeanNameAware, InitializingBean, DisposableBean {
42

43
  @Nullable
44
  private String poolSize;
45

46
  @Nullable
47
  private Integer queueCapacity;
48

49
  @Nullable
50
  private RejectedExecutionHandler rejectedExecutionHandler;
51

52
  @Nullable
53
  private Integer keepAliveSeconds;
54

55
  @Nullable
56
  private String beanName;
57

58
  @Nullable
59
  private ThreadPoolTaskExecutor target;
60

61
  public void setPoolSize(String poolSize) {
62
    this.poolSize = poolSize;
3✔
63
  }
1✔
64

65
  public void setQueueCapacity(int queueCapacity) {
66
    this.queueCapacity = queueCapacity;
4✔
67
  }
1✔
68

69
  public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
70
    this.rejectedExecutionHandler = rejectedExecutionHandler;
×
71
  }
×
72

73
  public void setKeepAliveSeconds(int keepAliveSeconds) {
74
    this.keepAliveSeconds = keepAliveSeconds;
4✔
75
  }
1✔
76

77
  @Override
78
  public void setBeanName(String beanName) {
79
    this.beanName = beanName;
3✔
80
  }
1✔
81

82
  @Override
83
  public void afterPropertiesSet() {
84
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
4✔
85
    determinePoolSizeRange(executor);
3✔
86
    if (this.queueCapacity != null) {
3✔
87
      executor.setQueueCapacity(this.queueCapacity);
5✔
88
    }
89
    if (this.keepAliveSeconds != null) {
3✔
90
      executor.setKeepAliveSeconds(this.keepAliveSeconds);
5✔
91
    }
92
    if (this.rejectedExecutionHandler != null) {
3!
93
      executor.setRejectedExecutionHandler(this.rejectedExecutionHandler);
×
94
    }
95
    if (this.beanName != null) {
3!
96
      executor.setThreadNamePrefix(this.beanName + "-");
5✔
97
    }
98
    executor.afterPropertiesSet();
2✔
99
    this.target = executor;
3✔
100
  }
1✔
101

102
  private void determinePoolSizeRange(ThreadPoolTaskExecutor executor) {
103
    if (StringUtils.hasText(this.poolSize)) {
4✔
104
      try {
105
        int corePoolSize;
106
        int maxPoolSize;
107
        int separatorIndex = this.poolSize.indexOf('-');
5✔
108
        if (separatorIndex != -1) {
3✔
109
          corePoolSize = Integer.parseInt(this.poolSize, 0, separatorIndex, 10);
7✔
110
          maxPoolSize = Integer.parseInt(this.poolSize, separatorIndex + 1, this.poolSize.length(), 10);
11✔
111
          if (corePoolSize > maxPoolSize) {
3!
112
            throw new IllegalArgumentException(
×
113
                    "Lower bound of pool-size range must not exceed the upper bound");
114
          }
115
          if (this.queueCapacity == null) {
3✔
116
            // No queue-capacity provided, so unbounded
117
            if (corePoolSize == 0) {
2!
118
              // Actually set 'corePoolSize' to the upper bound of the range
119
              // but allow core threads to timeout...
120
              executor.setAllowCoreThreadTimeOut(true);
3✔
121
              corePoolSize = maxPoolSize;
3✔
122
            }
123
            else {
124
              // Non-zero lower bound implies a core-max size range...
125
              throw new IllegalArgumentException(
×
126
                      "A non-zero lower bound for the size range requires a queue-capacity value");
127
            }
128
          }
129
        }
130
        else {
131
          int value = Integer.parseInt(this.poolSize);
4✔
132
          corePoolSize = value;
2✔
133
          maxPoolSize = value;
2✔
134
        }
135
        executor.setCorePoolSize(corePoolSize);
3✔
136
        executor.setMaxPoolSize(maxPoolSize);
3✔
137
      }
138
      catch (NumberFormatException ex) {
1✔
139
        throw new IllegalArgumentException("Invalid pool-size value [" + this.poolSize + "]: only single " +
8✔
140
                "maximum integer (e.g. \"5\") and minimum-maximum range (e.g. \"3-5\") are supported", ex);
141
      }
1✔
142
    }
143
  }
1✔
144

145
  @Override
146
  @Nullable
147
  public TaskExecutor getObject() {
148
    return this.target;
3✔
149
  }
150

151
  @Override
152
  public Class<? extends TaskExecutor> getObjectType() {
153
    return this.target != null ? this.target.getClass() : ThreadPoolTaskExecutor.class;
9✔
154
  }
155

156
  @Override
157
  public boolean isSingleton() {
158
    return true;
2✔
159
  }
160

161
  @Override
162
  public void destroy() {
163
    if (this.target != null) {
3!
164
      this.target.destroy();
3✔
165
    }
166
  }
1✔
167

168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc