From 4446b5fe8ada73356989f11d2f430b028f8565bc Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 18 Mar 2015 21:21:46 +0100 Subject: [PATCH] Revised DefaultManagedAwareThreadFactory to make its non-JNDI fallback work Issue: SPR-12830 (cherry picked from commit a3e5fbf) --- .../DefaultManagedAwareThreadFactory.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedAwareThreadFactory.java b/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedAwareThreadFactory.java index 3eb676466d64..f33802663908 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedAwareThreadFactory.java +++ b/spring-context/src/main/java/org/springframework/scheduling/concurrent/DefaultManagedAwareThreadFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,11 +32,14 @@ * for JSR-236's "java:comp/DefaultManagedThreadFactory" in a Java EE 7 environment, * falling back to the local {@link CustomizableThreadFactory} setup if not found. * - *

This is a convenient way to use managed threads when running in a Java EE 7 environment, - * simply using regular local threads otherwise - without conditional setup (like profiles). + *

This is a convenient way to use managed threads when running in a Java EE 7 + * environment, simply using regular local threads otherwise - without conditional + * setup (i.e. without profiles). * *

Note: This class is not strictly JSR-236 based; it can work with any regular - * {@link java.util.concurrent.ThreadFactory} that can be found in JNDI. + * {@link java.util.concurrent.ThreadFactory} that can be found in JNDI. Therefore, + * the default JNDI name "java:comp/DefaultManagedThreadFactory" can be customized + * through the {@link #setJndiName "jndiName"} bean property. * * @author Juergen Hoeller * @since 4.0 @@ -50,7 +53,7 @@ public class DefaultManagedAwareThreadFactory extends CustomizableThreadFactory private String jndiName = "java:comp/DefaultManagedThreadFactory"; - private ThreadFactory threadFactory = this; + private ThreadFactory threadFactory; /** @@ -98,12 +101,10 @@ public void afterPropertiesSet() throws NamingException { } catch (NamingException ex) { if (logger.isDebugEnabled()) { - logger.debug("Failed to find [java:comp/DefaultManagedThreadFactory] in JNDI", ex); - } - if (logger.isInfoEnabled()) { - logger.info("Could not find default managed thread factory in JNDI - " + - "proceeding with default local thread factory"); + logger.debug("Failed to retrieve [" + this.jndiName + "] from JNDI", ex); } + logger.info("Could not find default managed thread factory in JNDI - " + + "proceeding with default local thread factory"); } } } @@ -111,7 +112,12 @@ public void afterPropertiesSet() throws NamingException { @Override public Thread newThread(Runnable runnable) { - return this.threadFactory.newThread(runnable); + if (this.threadFactory != null) { + return this.threadFactory.newThread(runnable); + } + else { + return super.newThread(runnable); + } } }