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