001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.scheduling.concurrent; 018 019import java.util.Properties; 020import java.util.concurrent.ThreadFactory; 021 022import javax.naming.NamingException; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026 027import org.springframework.beans.factory.InitializingBean; 028import org.springframework.jndi.JndiLocatorDelegate; 029import org.springframework.jndi.JndiTemplate; 030import org.springframework.lang.Nullable; 031 032/** 033 * JNDI-based variant of {@link CustomizableThreadFactory}, performing a default lookup 034 * for JSR-236's "java:comp/DefaultManagedThreadFactory" in a Java EE 7 environment, 035 * falling back to the local {@link CustomizableThreadFactory} setup if not found. 036 * 037 * <p>This is a convenient way to use managed threads when running in a Java EE 7 038 * environment, simply using regular local threads otherwise - without conditional 039 * setup (i.e. without profiles). 040 * 041 * <p>Note: This class is not strictly JSR-236 based; it can work with any regular 042 * {@link java.util.concurrent.ThreadFactory} that can be found in JNDI. Therefore, 043 * the default JNDI name "java:comp/DefaultManagedThreadFactory" can be customized 044 * through the {@link #setJndiName "jndiName"} bean property. 045 * 046 * @author Juergen Hoeller 047 * @since 4.0 048 */ 049@SuppressWarnings("serial") 050public class DefaultManagedAwareThreadFactory extends CustomizableThreadFactory implements InitializingBean { 051 052 protected final Log logger = LogFactory.getLog(getClass()); 053 054 private JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); 055 056 @Nullable 057 private String jndiName = "java:comp/DefaultManagedThreadFactory"; 058 059 @Nullable 060 private ThreadFactory threadFactory; 061 062 063 /** 064 * Set the JNDI template to use for JNDI lookups. 065 * @see org.springframework.jndi.JndiAccessor#setJndiTemplate 066 */ 067 public void setJndiTemplate(JndiTemplate jndiTemplate) { 068 this.jndiLocator.setJndiTemplate(jndiTemplate); 069 } 070 071 /** 072 * Set the JNDI environment to use for JNDI lookups. 073 * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment 074 */ 075 public void setJndiEnvironment(Properties jndiEnvironment) { 076 this.jndiLocator.setJndiEnvironment(jndiEnvironment); 077 } 078 079 /** 080 * Set whether the lookup occurs in a Java EE container, i.e. if the prefix 081 * "java:comp/env/" needs to be added if the JNDI name doesn't already 082 * contain it. PersistenceAnnotationBeanPostProcessor's default is "true". 083 * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef 084 */ 085 public void setResourceRef(boolean resourceRef) { 086 this.jndiLocator.setResourceRef(resourceRef); 087 } 088 089 /** 090 * Specify a JNDI name of the {@link java.util.concurrent.ThreadFactory} to delegate to, 091 * replacing the default JNDI name "java:comp/DefaultManagedThreadFactory". 092 * <p>This can either be a fully qualified JNDI name, or the JNDI name relative 093 * to the current environment naming context if "resourceRef" is set to "true". 094 * @see #setResourceRef 095 */ 096 public void setJndiName(String jndiName) { 097 this.jndiName = jndiName; 098 } 099 100 @Override 101 public void afterPropertiesSet() throws NamingException { 102 if (this.jndiName != null) { 103 try { 104 this.threadFactory = this.jndiLocator.lookup(this.jndiName, ThreadFactory.class); 105 } 106 catch (NamingException ex) { 107 if (logger.isTraceEnabled()) { 108 logger.trace("Failed to retrieve [" + this.jndiName + "] from JNDI", ex); 109 } 110 logger.info("Could not find default managed thread factory in JNDI - " + 111 "proceeding with default local thread factory"); 112 } 113 } 114 } 115 116 117 @Override 118 public Thread newThread(Runnable runnable) { 119 if (this.threadFactory != null) { 120 return this.threadFactory.newThread(runnable); 121 } 122 else { 123 return super.newThread(runnable); 124 } 125 } 126 127}