001/* 002 * Copyright 2002-2015 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; 021import javax.naming.NamingException; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026import org.springframework.beans.factory.InitializingBean; 027import org.springframework.jndi.JndiLocatorDelegate; 028import org.springframework.jndi.JndiTemplate; 029 030/** 031 * JNDI-based variant of {@link CustomizableThreadFactory}, performing a default lookup 032 * for JSR-236's "java:comp/DefaultManagedThreadFactory" in a Java EE 7 environment, 033 * falling back to the local {@link CustomizableThreadFactory} setup if not found. 034 * 035 * <p>This is a convenient way to use managed threads when running in a Java EE 7 036 * environment, simply using regular local threads otherwise - without conditional 037 * setup (i.e. without profiles). 038 * 039 * <p>Note: This class is not strictly JSR-236 based; it can work with any regular 040 * {@link java.util.concurrent.ThreadFactory} that can be found in JNDI. Therefore, 041 * the default JNDI name "java:comp/DefaultManagedThreadFactory" can be customized 042 * through the {@link #setJndiName "jndiName"} bean property. 043 * 044 * @author Juergen Hoeller 045 * @since 4.0 046 */ 047@SuppressWarnings("serial") 048public class DefaultManagedAwareThreadFactory extends CustomizableThreadFactory implements InitializingBean { 049 050 protected final Log logger = LogFactory.getLog(getClass()); 051 052 private JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); 053 054 private String jndiName = "java:comp/DefaultManagedThreadFactory"; 055 056 private ThreadFactory threadFactory; 057 058 059 /** 060 * Set the JNDI template to use for JNDI lookups. 061 * @see org.springframework.jndi.JndiAccessor#setJndiTemplate 062 */ 063 public void setJndiTemplate(JndiTemplate jndiTemplate) { 064 this.jndiLocator.setJndiTemplate(jndiTemplate); 065 } 066 067 /** 068 * Set the JNDI environment to use for JNDI lookups. 069 * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment 070 */ 071 public void setJndiEnvironment(Properties jndiEnvironment) { 072 this.jndiLocator.setJndiEnvironment(jndiEnvironment); 073 } 074 075 /** 076 * Set whether the lookup occurs in a J2EE container, i.e. if the prefix 077 * "java:comp/env/" needs to be added if the JNDI name doesn't already 078 * contain it. PersistenceAnnotationBeanPostProcessor's default is "true". 079 * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef 080 */ 081 public void setResourceRef(boolean resourceRef) { 082 this.jndiLocator.setResourceRef(resourceRef); 083 } 084 085 /** 086 * Specify a JNDI name of the {@link java.util.concurrent.ThreadFactory} to delegate to, 087 * replacing the default JNDI name "java:comp/DefaultManagedThreadFactory". 088 * <p>This can either be a fully qualified JNDI name, or the JNDI name relative 089 * to the current environment naming context if "resourceRef" is set to "true". 090 * @see #setResourceRef 091 */ 092 public void setJndiName(String jndiName) { 093 this.jndiName = jndiName; 094 } 095 096 @Override 097 public void afterPropertiesSet() throws NamingException { 098 if (this.jndiName != null) { 099 try { 100 this.threadFactory = this.jndiLocator.lookup(this.jndiName, ThreadFactory.class); 101 } 102 catch (NamingException ex) { 103 if (logger.isDebugEnabled()) { 104 logger.debug("Failed to retrieve [" + this.jndiName + "] from JNDI", ex); 105 } 106 logger.info("Could not find default managed thread factory in JNDI - " + 107 "proceeding with default local thread factory"); 108 } 109 } 110 } 111 112 113 @Override 114 public Thread newThread(Runnable runnable) { 115 if (this.threadFactory != null) { 116 return this.threadFactory.newThread(runnable); 117 } 118 else { 119 return super.newThread(runnable); 120 } 121 } 122 123}