001/* 002 * Copyright 2002-2020 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.Executor; 021 022import javax.naming.NamingException; 023 024import org.springframework.beans.factory.InitializingBean; 025import org.springframework.jndi.JndiLocatorDelegate; 026import org.springframework.jndi.JndiTemplate; 027import org.springframework.lang.Nullable; 028 029/** 030 * JNDI-based variant of {@link ConcurrentTaskExecutor}, performing a default lookup for 031 * JSR-236's "java:comp/DefaultManagedExecutorService" in a Java EE 7/8 environment. 032 * 033 * <p>Note: This class is not strictly JSR-236 based; it can work with any regular 034 * {@link java.util.concurrent.Executor} that can be found in JNDI. 035 * The actual adapting to {@link javax.enterprise.concurrent.ManagedExecutorService} 036 * happens in the base class {@link ConcurrentTaskExecutor} itself. 037 * 038 * @author Juergen Hoeller 039 * @since 4.0 040 * @see javax.enterprise.concurrent.ManagedExecutorService 041 */ 042public class DefaultManagedTaskExecutor extends ConcurrentTaskExecutor implements InitializingBean { 043 044 private final JndiLocatorDelegate jndiLocator = new JndiLocatorDelegate(); 045 046 @Nullable 047 private String jndiName = "java:comp/DefaultManagedExecutorService"; 048 049 050 /** 051 * Set the JNDI template to use for JNDI lookups. 052 * @see org.springframework.jndi.JndiAccessor#setJndiTemplate 053 */ 054 public void setJndiTemplate(JndiTemplate jndiTemplate) { 055 this.jndiLocator.setJndiTemplate(jndiTemplate); 056 } 057 058 /** 059 * Set the JNDI environment to use for JNDI lookups. 060 * @see org.springframework.jndi.JndiAccessor#setJndiEnvironment 061 */ 062 public void setJndiEnvironment(Properties jndiEnvironment) { 063 this.jndiLocator.setJndiEnvironment(jndiEnvironment); 064 } 065 066 /** 067 * Set whether the lookup occurs in a Java EE container, i.e. if the prefix 068 * "java:comp/env/" needs to be added if the JNDI name doesn't already 069 * contain it. PersistenceAnnotationBeanPostProcessor's default is "true". 070 * @see org.springframework.jndi.JndiLocatorSupport#setResourceRef 071 */ 072 public void setResourceRef(boolean resourceRef) { 073 this.jndiLocator.setResourceRef(resourceRef); 074 } 075 076 /** 077 * Specify a JNDI name of the {@link java.util.concurrent.Executor} to delegate to, 078 * replacing the default JNDI name "java:comp/DefaultManagedExecutorService". 079 * <p>This can either be a fully qualified JNDI name, or the JNDI name relative 080 * to the current environment naming context if "resourceRef" is set to "true". 081 * @see #setConcurrentExecutor 082 * @see #setResourceRef 083 */ 084 public void setJndiName(String jndiName) { 085 this.jndiName = jndiName; 086 } 087 088 @Override 089 public void afterPropertiesSet() throws NamingException { 090 if (this.jndiName != null) { 091 setConcurrentExecutor(this.jndiLocator.lookup(this.jndiName, Executor.class)); 092 } 093 } 094 095}