DefaultManagedTaskScheduler
|
This implementation implicitly initializes the underlying task scheduler via JNDI lookup. |
According to JSR-236 specification: The Java EE product provider (the application server) must bind a pre-configured, default ManagedScheduledExecutorService so that application components can find it under the JNDI name java:comp/DefaultManagedScheduledExecutorService.
Spring's DefaultManagedTaskScheduler internally initializes the underlying scheduler from this JNDI look up. The scheduler must be used as a bean otherwise this class will work as super class ConcurrentTaskScheduler.
Also this class is not strictly JSR-236 based; it can work with any regular ScheduledExecutorService that can be found in JNDI.
Example
In the following example we are not going to demonstrate implicit JNDI binding in a JEE server environment but instead we are going to bind "java:comp/DefaultManagedScheduledExecutorService" manually by using SimpleNamingContextBuilder.
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler;
@Configuration
public class MyConfig {
@Bean
TaskScheduler taskScheduler() {
return new DefaultManagedTaskScheduler();
}
@Bean
MyBean myBean() {
return new MyBean();
}
}
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import java.time.LocalTime;
public class MyBean {
@Autowired
TaskScheduler taskScheduler;
public void runTask() {
taskScheduler.scheduleWithFixedDelay((Runnable) () -> {
System.out.println("running " + LocalTime.now());
}, 1000L);
}
}
package com.logicbig.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler;
import javax.naming.NamingException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DefaultManagedTaskSchedulerExample {
public static void main (String[] args) throws NamingException, InterruptedException {
//binding the scheduler manually,
// In JEE compliant server environment this will be
// provided by the server product
SimpleNamingContextBuilder b = new SimpleNamingContextBuilder();
b.bind("java:comp/DefaultManagedScheduledExecutorService",
Executors.newScheduledThreadPool(5));
b.activate();
//bootstrapping spring
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.runTask();
//shutdown after 10 sec
Thread.sleep(10000);
DefaultManagedTaskScheduler scheduler = context.getBean(
DefaultManagedTaskScheduler.class);
Executor exec = scheduler.getConcurrentExecutor();
ExecutorService es = (ExecutorService) exec;
es.shutdownNow();
}
}
Outputrunning 16:31:29.728035500 running 16:31:30.731231800 running 16:31:31.732378100 running 16:31:32.733564200 running 16:31:33.734986400 running 16:31:34.736386 running 16:31:35.736719300 running 16:31:36.737566200 running 16:31:37.738595800 running 16:31:38.738936200
SimpleNamingContextBuilder has been deprecated
SimpleNamingContextBuilder was deprecated in Spring Framework 5.2 and was removed in Spring Framework 6.0. It is recommended to use complete solutions from third parties such as Simple-JNDI
Example ProjectDependencies and Technologies Used: - spring-context 5.3.39 (Spring Context)
Version Compatibility: 4.0.0.RELEASE - 5.3.39
- spring-test 5.3.39 (Spring TestContext Framework)
- JDK 25
- Maven 3.9.11
|