3

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.

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
anand mishra
  • 33
  • 1
  • 1
  • 7

3 Answers3

4

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);
}
codeaholicguy
  • 1,671
  • 1
  • 11
  • 18
  • 2
    I don't want this to run at any interval. it may be one time only like- i want to send an email at 01-july-2015, 10:35 AM. For different different user it will be different according to their input. – anand mishra Jun 30 '15 at 04:48
  • 1
    @anandmishra you can change scheduleAtFixedRate() to schedule(), i added javadoc above. – codeaholicguy Jun 30 '15 at 04:54
  • 2
    Thanks @codeaholicguy i think it will work. But how many tasks we can schedule in my applications thousands of user has to schedule their task. so will it work? – anand mishra Jun 30 '15 at 05:23
  • If you declare it in a class, with one instance created it will create a scheduler because we use @PostConstruct anotation. You should move TaskScheduler out and become static for better performance. – codeaholicguy Jun 30 '15 at 06:25
  • Good, I am happy because this can help. – codeaholicguy Jun 30 '15 at 14:53
1

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"/>}
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
0

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

Community
  • 1
  • 1
John
  • 5,189
  • 2
  • 38
  • 62