Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schuduler Enable/Disable from a boolean value. [SPR-10376] #15009

Closed
spring-issuemaster opened this issue Mar 14, 2013 · 8 comments
Closed

Schuduler Enable/Disable from a boolean value. [SPR-10376] #15009

spring-issuemaster opened this issue Mar 14, 2013 · 8 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Mar 14, 2013

Ashis Kumar Panigrahy opened SPR-10376 and commented

There should be option to Enable and Disable the Scheduler.


No further details from SPR-10376

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 14, 2013

Phil Webb commented

Could you provide a brief code example of where this is need and a little more explanation of why it would be useful.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 14, 2013

Ashis Kumar Panigrahy commented

Hi P.Webb,

Please find the below code snippet where I'm using scheduler:

@Scheduled(cron="${scheduler.frequency}")
public void scheduler() {
  // ... Code has been deleted as per security Issue hope you don't mind.
}

Property File Value:

scheduler.frequency = 0 * * * * ?

Now its working fine.
But I have similar type of 5 scheduler. Some time I need to run 3 some time all.
So, I need to build the Project every time. So to avoid this property file configuration is help full.
But there is no option where i can mention to enable/disable the scheduler.

Thanks,
Ashis

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 15, 2013

Phil Webb commented

Have you considered using separate beans in combination with the @Profile annotation. Alternatively you could perhaps add the conditional logic within the method body.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 17, 2013

Ashis Kumar Panigrahy commented

Conditional Logic is the option that I came to know. But the thing is it won't disable the scheduler. It will just skip calling the method if the condition not satisfied.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 18, 2013

Phil Webb commented

Indeed, the scheduler will still be created with conditional logic. The @Profile annotation however might work for you if you can put each @Scheduled method in its own bean.

Another option that you could consider would be to create a TaskScheduler variant for the conditional logic.

public class ConditionalTaskScheduler extends ... {

  public ScheduledFuture schedule(Runnable task, Trigger trigger) {
    if(!(task instanceof ScheduledMethodRunnable) || canRun((ScheduledMethodRunnable)task) {
      super.schedule(task, trigger);
    }
  }

  private boolean canRun(ScheduledMethodRunnable task) {
    // Can use task.getMethod() here to get back to the bean method
    // perhaps condition could be in another annotation
  }

}

You could also consider trying to craft a cron expression that will never run (http://stackoverflow.com/questions/8324306/cron-job-that-will-never-execute)

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 18, 2013

Ashis Kumar Panigrahy commented

Thanks Phill,
Will check this and let you know. Thanks for the suggestion.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 18, 2013

Ashis Kumar Panigrahy commented

Hi Phil,
I tried with @Profile and its working fine. Thanks for the idea.
Thanks & Regards,
Ashis Kumar panigrahy

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 18, 2013

Phil Webb commented

Excellent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.