In my Spring MVC application i need to schedule a task with specific date & Time. Like- i have to schedule to send a email which will be configured dynamically by customer. In Spring #Schedule annotation is there but how can i change value dynamically every time with any date & Time.
Any help is appreciated.
You should try TaskScheduler, see the javadoc here:
private TaskScheduler scheduler = new ConcurrentTaskScheduler();
#PostConstruct
private void executeJob() {
scheduler.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
// your business here
}
}, INTERVAL);
}
Refer Spring Task Execution and Scheduling
Example Annotations
#Configuration
#EnableAsync
#EnableScheduling
public class MyComponent {
#Async
#Scheduled(fixedDelay=5000, repeatCount=0)
public void doSomething() {
// something that should execute periodically
}
}
I think the repeatCount=0 will make the function execute only once (yet to test)
Full example with Quartz scheduler http://www.mkyong.com/spring/spring-quartz-scheduler-example/
You need to introduce XML configuration as follows
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>}
You can achieve this easily within standard java API, by scheduling task for time difference between the time task is created and target date entered by customers. Simply provide this difference as parameter delay.
ScheduledThreadPoolExecutor
schedule(Callable<V> callable, long delay, TimeUnit unit)
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
Creates and executes a one-shot action that becomes enabled after the given delay.
So you either have to submit Runnable or Callable to this service.
You can refer to this answer for calculation between dates:
time difference
Related
I want to implement the ExecutorService in my Spring-MVC application.
I need a global ExecutorService which take tasks put them in a queue and then executes them sequentially. So I want to pass the tasks from different locations in the application. Therefore I am using the Executors.newSingleThreadExecutor(); so I have only 1 thread executing these tasks.
However, I am not sure how to integrate it in a Spring application:
public enum TaskQueue {
INSTANCE;
ExecutorService executorService;
private TaskQueue() {
executorService = Executors.newSingleThreadExecutor();
}
public void addTaskToQueue (Runnable task){
executorService.submit(task);
executorService.shutdown();
}
}
So I am thinking of creating a Singleton and then just passing the Task (Runnable object) to the method:
TaskQueue.INSTANCE.addTaskToQueue(new Runnable() {
#Override
public void run() {
System.out.println("Executing Task1 inside : " + Thread.currentThread().getName());
}
});
However, I have several questions:
I am not sure how to integrate it in a Spring MVC application where I have controllers, services and so on.
The application receives some notifications from a web-service. These notifications will be processed on different locations in the code. I want to
execute them sequentially. So I need to identify all tasks I want to run asynchronously and then pass them to the method above (`addTaskToQueue) wrapped in a Runnabel object, so they can be executed asynchronously. Is this the correct approach?
So I always pass a Runnable objects to this method to execute it. This method executes it and shuts the executorservice down. So each time I pass a task to this service - it creates a new service and then closes it. But I dont want to have that - I want the executorservice to stay alive and execute the tasks that are comming and not shutdown after each task. How do I achieve this?
Or am I totally wrong in implementing it this way?
EDIT:
Using the TaskExecutor provided by Spring - I would implement it like this:
#Autowired
private TaskExecutor taskExecutor;
Then calling it from different location in my code:
taskExecutor.execute(new Runnable() {
#Override
public void run() {
//TODO add long running task
}
});
Probably I need to make sure somewhere in the configuration that it needs to be executed sequentially - so it should create only 1 thread. Is it safe to call it from different locations? It wont always create a new service with a new queue?
Spring already has a bean for this: org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor. It implements DisposableBean interface and shutdown() method is called when Spring context is destroyed.
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="${threadPoolSize}"/>
<property name="maxPoolSize" value="${threadPoolSize}"/>
<property name="WaitForTasksToCompleteOnShutdown" value="false"/>
</bean>
The Singleton design pattern is not a very good fit for this. Since you're using Spring, it makes much more sense to make a component with #PostConstruct and #PreDestroy methods. Here and here are articles that explain this.
In your case, I'd do something like the following:
#Component
public class TaskQueue {
private ExecutorService executorService;
#PostConstruct
public void init() {
executorService = Executors.newSingleThreadExecutor();
}
#PreDestroy
public void cleanup() {
executorService.shutdown();
}
public void addTaskToQueue (Runnable task){
executorService.submit(task);
}
}
And then you can autowire this component.
Edit: #Ivan's answer is to be preferred.
I am working on spring based server application. Basically it will poll scores of various sporting events in very short interval and save in db. For polling there will be many(can be around 100) calls to different apis concurrently at regular interval for example some api call will have 3 seconds interval some have 5 seconds etc., server will keep polling for latest data at frequent interval.
These calls will be added and removed dynamically. I have little experience in using spring. I think I have to use some scheduler. Can anyone point in right direction what approach or which scheduler is best in this scenario.
In essence you want to inject an instance of a scheduling task executor
#Configuration
public class MyApplicationConfiguration {
#Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
ThreadPoolTaskScheduler tpts = new ThreadPoolTaskScheduler();
// maybe configure it a little?
return tpts;
}
#Bean
public MyService myService() {
return new MyService();
}
}
class MyService {
#Autowired
private ThreadPoolTaskScheduler tpts;
public void doSomething() {
Runnable task = ...
tpts.scheduleWithFixedDelay(task, 1000);
}
}
You can see a reasonable guide here, or the SchedulingTaskExecutor Javadoc and the Spring Task Execution and Scheduling Reference
You can use #Scheduled Spring Annotation for this. Refer this link for examples.
Currently I am building a spring standalone program in order to learn new methods and architectures.
The last few days I tried to learn scheduler. I never used them before so I read some articles handling the different possible methods. Two of them are especially interesting: The spring nativ #Scheduler and Quartz.
From what I read, Spring is a little bit smaller then Quartz and much more basic. And quartz is not easy to use with spring (because of the autowired and components).
My problem now is, that there is one thing I do not understand:
From my understanding, both methods are creating parallel Threads in order to asynchronously run the jobs. But what if I now have a spring #Service in my main Application, that is holding a HashMap with some information. The data is updated and changed with user interaction. Parallel there are the scheduler. And a scheduler now whants to use this HashMap from the main application as well. Is this even possible?
Or do I understand something wrong? Because there is also the #Async annotation and I did not understand the difference. Because a scheduler itself is already parallel to the main corpus, isn't it?
(summing up, two questions:
can a job that is executed every five seconds, implemented with a scheduler, use a HashMap out of a service inside the main program? (in spring #Scheduler and/or in Quartz?)
Why is there a #Async annotation. Isn't a scheduler already parallel to the main process?
)
I have to make a few assumptions about which version of Spring you're using but as you're in the process of learning, I would assume that you're using spring-boot or a fairly new version, so please excuse if the annotations don't match your version of Spring. This said, to answer your two questions the best I can:
can a job that is executed every five seconds, implemented with a scheduler, use a HashMap out of a service inside the main program? (in spring #Scheduler and/or in Quartz?)
Yes, absolutely! The easiest way is to make sure that the hashmap in question is declared as static. To access the hashmap from the scheduled job, simply either autowire your service class or create a static get function for the hashmap.
Here is an example of a recent Vaadin project where I needed a scheduled message sent to a set of subscribers.
SchedulerConfig.class
#Configuration
#EnableAsync
#EnableScheduling
public class SchedulerConfig {
#Scheduled(fixedDelay=5000)
public void refreshVaadinUIs() {
Broadcaster.broadcast(
new BroadcastMessage(
BroadcastMessageType.AUTO_REFRESH_LIST
)
);
}
}
Broadcaster.class
public class Broadcaster implements Serializable {
private static final long serialVersionUID = 3540459607283346649L;
private static ExecutorService executorService = Executors.newSingleThreadExecutor();
private static LinkedList<BroadcastListener> listeners = new LinkedList<BroadcastListener>();
public interface BroadcastListener {
void receiveBroadcast(BroadcastMessage message);
}
public static synchronized void register(BroadcastListener listener) {
listeners.add(listener);
}
public static synchronized void unregister(BroadcastListener listener) {
listeners.remove(listener);
}
public static synchronized void broadcast(final BroadcastMessage message) {
for (final BroadcastListener listener: listeners)
executorService.execute(new Runnable() {
#Override
public void run() {
listener.receiveBroadcast(message);
}
});
}
}
Why is there a #Async annotation. Isn't a scheduler already parallel to the main process?
Yes, the scheduler is running in its own thread but what occurs to the scheduler on long running tasks (ie: doing a SOAP call to a remote server that takes a very long time to complete)?
The #Async annotation isn't required for scheduling but if you have a long running function being invoked by the scheduler, it becomes quite important.
This annotation is used to take a specific task and request to Spring's TaskExecutor to execute it on its own thread instead of the current thread. The #Async annotation causes the function to immediately return but execution will be later made by the TaskExecutor.
This said, without the #EnableAsync or #Async annotation, the functions you call will hold up the TaskScheduler as they will be executed on the same thread. On a long running operation, this would cause the scheduler to be held up and unable to execute any other scheduled functions until it returns.
I would suggest a read of Spring's Documentation about Task Execution and Scheduling It provides a great explanation of the TaskScheduler and TaskExecutor in Spring
I have several methods annotated with #Scheduled(fixedDelay=10000).
In the application context, I have this annotation-driven setup:
<task:annotation-driven />
The problem is, sometimes some of the method executions get delayed by seconds and even minutes.
I'm assuming that even if a method takes a while to finish executing, the other methods would still execute. So I don't understand the delay.
Is there a way to maybe lessen or even remove the delay?
For completeness, code below shows the simplest possible way to configure scheduler with java config:
#Configuration
#EnableScheduling
public class SpringConfiguration {
#Bean(destroyMethod = "shutdown")
public Executor taskScheduler() {
return Executors.newScheduledThreadPool(5);
}
...
When more control is desired, a #Configuration class may implement SchedulingConfigurer.
The documentation about scheduling says:
If you do not provide a pool-size attribute, the default thread pool will only have a single thread.
So if you have many scheduled tasks, you should configure the scheduler, as explained in the documentation, to have a pool with more threads, to make sure one long task doesn't delay all the other ones.
If you're using Spring Boot:
There is also a property you can set in your application properties file that increases the pool size:
spring.task.scheduling.pool.size=10
Seems to be there since Spring Boot 2.1.0.
A method annotated with #Scheduled is meant to be run separately, on a different thread at a moment in time.
If you haven't provided a TaskScheduler in your configuration, Spring will use
Executors.newSingleThreadScheduledExecutor();
which returns an ScheduledExecutorService that runs on a single thread. As such, if you have multiple #Scheduled methods, although they are scheduled, they each need to wait for the thread to complete executing the previous task. You might keep getting bigger and bigger delays as the the queue fills up faster than it empties out.
Make sure you configure your scheduling environment with an appropriate amount of threads.
The #EnableScheduling annotation provides the key information and how to resolve it:
By default, will be searching for an associated scheduler definition:
either a unique TaskScheduler bean in the context, or a TaskScheduler
bean named "taskScheduler" otherwise; the same lookup will also be
performed for a ScheduledExecutorService bean. If neither of the two
is resolvable, a local single-threaded default scheduler will be
created and used within the registrar.
When more control is desired, a #Configuration class may implement
SchedulingConfigurer. This allows access to the underlying
ScheduledTaskRegistrar instance. For example, the following example
demonstrates how to customize the Executor used to execute scheduled
tasks:
#Configuration
#EnableScheduling
public class AppConfig implements SchedulingConfigurer {
#Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskExecutor());
}
#Bean(destroyMethod="shutdown")
public Executor taskExecutor() {
return Executors.newScheduledThreadPool(100);
}
}
(emphasis added)
you can use:
#Bean()
public ThreadPoolTaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(2);
return taskScheduler;
}
Use below link for the reference..great explanation and implementation:
https://crmepham.github.io/spring-boot-multi-thread-scheduling/#:~:text=By%20default%20Spring%20Boot%20will,there%20is%20enough%20threads%20available).
Using XML file add below lines..
<task:scheduler id="taskScheduler" pool-size="15" />
<task:scheduled-tasks scheduler="taskScheduler" >
....
</task:scheduled-tasks>
default spring using a single thread for schedule task. you can using #Configuration for class implements SchedulingConfigurer . referce: https://crmepham.github.io/spring-boot-multi-thread-scheduling/
We need to pass our own thread pool scheduler, otherwise it will use default single threaded executor. Have added below code to fix-
#Bean
public Executor scheduledTaskThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setThreadNamePrefix("name-");
executor.initialize();
return executor;
}
Our web app has few scheduled tasks and we like this feature of Spring so much, many have started relying on it. We have a 'pilot' machine which shares the same configuration/db as prod machines. Since this machine points to the same db as prod machines, when it runs a scheduled task - it may affect prod data. Is there a way to not run Spring Scheduled task on this machine? We thought of relying on the machine name, but dont want to introduce a check each time a task starts. Any suggestions?
With Spring 3.1 Profiles it will be really easy, but here is a way you can do it in Spring 3.0.
In your context:
<task:annotation-driven executor="taskExecutor" scheduler="configScheduler"/>
<task:scheduler id="taskScheduler"/>
<task:executor id="taskExecutor"/>
Use #Bean to define configScheduler, using a dummy scheduler if a system property noScheduler is set.
#Configuration
public class SchedulerConfig {
#Resource(name="taskScheduler")
ThreadPoolTaskScheduler taskScheduler;
#Bean
ThreadPoolTaskScheduler configScheduler() {
ThreadPoolTaskScheduler scheduler =
System.getProperty("noScheduler") == null : taskScheduler ?
new ThreadPoolTaskScheduler() {
#Override public ScheduledFuture schedule(Runnable task, Trigger trigger) { return null; } // Cron
#Override public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) { return null; }
#Override public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) { return null; }
};
return scheduler;
}
}
With Spring 3.1, you'll get profiles, which might help you out.