• 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

0.0
today-context/src/main/java/infra/scheduling/concurrent/DefaultManagedAwareThreadFactory.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.concurrent;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Properties;
23
import java.util.concurrent.ThreadFactory;
24

25
import javax.naming.NamingException;
26

27
import infra.beans.factory.InitializingBean;
28
import infra.jndi.JndiAccessor;
29
import infra.jndi.JndiLocatorDelegate;
30
import infra.jndi.JndiLocatorSupport;
31
import infra.jndi.JndiTemplate;
32
import infra.logging.Logger;
33
import infra.logging.LoggerFactory;
34

35
/**
36
 * JNDI-based variant of {@link CustomizableThreadFactory}, performing a default lookup
37
 * for JSR-236's "java:comp/DefaultManagedThreadFactory" in a Jakarta EE environment,
38
 * falling back to the local {@link CustomizableThreadFactory} setup if not found.
39
 *
40
 * <p>This is a convenient way to use managed threads when running in a Jakarta EE
41
 * environment, simply using regular local threads otherwise - without conditional
42
 * setup (i.e. without profiles).
43
 *
44
 * <p>Note: This class is not strictly JSR-236 based; it can work with any regular
45
 * {@link ThreadFactory} that can be found in JNDI. Therefore,
46
 * the default JNDI name "java:comp/DefaultManagedThreadFactory" can be customized
47
 * through the {@link #setJndiName "jndiName"} bean property.
48
 *
49
 * @author Juergen Hoeller
50
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
51
 * @since 4.0
52
 */
53
@SuppressWarnings("serial")
54
public class DefaultManagedAwareThreadFactory extends CustomizableThreadFactory implements InitializingBean {
×
55

56
  protected final Logger logger = LoggerFactory.getLogger(getClass());
×
57

58
  private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate();
×
59

60
  @Nullable
×
61
  private String jndiName = "java:comp/DefaultManagedThreadFactory";
62

63
  @Nullable
64
  private ThreadFactory threadFactory;
65

66
  /**
67
   * Set the JNDI template to use for JNDI lookups.
68
   *
69
   * @see JndiAccessor#setJndiTemplate
70
   */
71
  public void setJndiTemplate(JndiTemplate jndiTemplate) {
72
    this.jndiLocator.setJndiTemplate(jndiTemplate);
×
73
  }
×
74

75
  /**
76
   * Set the JNDI environment to use for JNDI lookups.
77
   *
78
   * @see JndiAccessor#setJndiEnvironment
79
   */
80
  public void setJndiEnvironment(Properties jndiEnvironment) {
81
    this.jndiLocator.setJndiEnvironment(jndiEnvironment);
×
82
  }
×
83

84
  /**
85
   * Set whether the lookup occurs in a Jakarta EE container, i.e. if the prefix
86
   * "java:comp/env/" needs to be added if the JNDI name doesn't already
87
   * contain it. PersistenceAnnotationBeanPostProcessor's default is "true".
88
   *
89
   * @see JndiLocatorSupport#setResourceRef
90
   */
91
  public void setResourceRef(boolean resourceRef) {
92
    this.jndiLocator.setResourceRef(resourceRef);
×
93
  }
×
94

95
  /**
96
   * Specify a JNDI name of the {@link ThreadFactory} to delegate to,
97
   * replacing the default JNDI name "java:comp/DefaultManagedThreadFactory".
98
   * <p>This can either be a fully qualified JNDI name, or the JNDI name relative
99
   * to the current environment naming context if "resourceRef" is set to "true".
100
   *
101
   * @see #setResourceRef
102
   */
103
  public void setJndiName(String jndiName) {
104
    this.jndiName = jndiName;
×
105
  }
×
106

107
  @Override
108
  public void afterPropertiesSet() throws NamingException {
109
    if (this.jndiName != null) {
×
110
      try {
111
        this.threadFactory = this.jndiLocator.lookup(jndiName, ThreadFactory.class);
×
112
      }
113
      catch (NamingException ex) {
×
114
        if (logger.isTraceEnabled()) {
×
115
          logger.trace("Failed to retrieve [{}] from JNDI", jndiName, ex);
×
116
        }
117
        logger.info("Could not find default managed thread factory in JNDI - " +
×
118
                "proceeding with default local thread factory");
119
      }
×
120
    }
121
  }
×
122

123
  @Override
124
  public Thread newThread(Runnable runnable) {
125
    if (this.threadFactory != null) {
×
126
      return this.threadFactory.newThread(runnable);
×
127
    }
128
    else {
129
      return super.newThread(runnable);
×
130
    }
131
  }
132

133
}
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