I have developed a spring boot project.
It uses #EnableScheduling and #Scheduled annotations to schedule task. I have tried both ways to schedule my function.
1)
#Scheduled(initialDelayString = "${scheduler.initialDelay}", fixedDelayString = "${scheduler.fixedDelay}")
2)
#Scheduled(cron = "0 0 1 * * *")
In both ways when I run my code locally, It gets run perfectly fine. But when I deploy my code to AWS instance using auto scaling group, spring application gets start, but after that nothing is happening. I don't even see any logs or error about scheduler. Application is running but scheduler is not getting invoked.
It seems very strange to me. And since its working perfectly very well in local, it is difficult to debug also.
I had a similar issue, I believe its to do with the Springboot default threadpool for scheduling. It worked for me when i altered the below setting in application.properties / yaml
spring.task.scheduling.pool.size=20
Related
I wrote below scheduler which runs everyday midnight 12 am. This has to restart the spring boot web application ( self ) and it is working as expected most of the time.
But once in a week ( approximately), application shutdown happens successfully, but not starting up.
Because this is failing intermittently, I have no clue why this code failing.
In my eclipse IDE environment, it works almost everytime. ( I changed the scheduler to run every 5 mins)
#Service
final class AutoRestartScheduler {
#Autowired
private RestartEndpoint restartEndpoint; //private to protect outside access.
final Logger logger = LoggerFactory.getLogger(AutoRestartScheduler.class);
#Scheduled(cron = "0 0 0 * * *", zone="America/Los_Angeles") //everyday mid-night 12 AM PST
public void restartApp(){
logger.info("Going to restart Tomcat, programmatically.");
logger.info("restarting MyPollerApplication...");
restartEndpoint.restart();
}
}
NOTE:
I am NOT using below property in configuration, because I am NOT using Actuator's /restart endpoint but Spring's Scheduler.
management.endpoint.restart.enabled=true
Since you don't see your own log messages when it fails to restart, that tells us that the application isn't going through the restart code that you're showing; maybe something else is causing the application to shut down.
From the log message you show ("Going to restart Tomcat"), I suspect you might misunderstand what RestartEndpoint really does. It restarts the Spring ApplicationContext, not the JVM or OS process, and possibly not the Tomcat instance that's hosting the application. Take a look at the JavaDoc and source code for that class to make sure it's actually doing what you think it's doing, and that you're using it correctly. Consider enabling Spring's own logging for this class at DEBUG level to see more about what's going on.
We are using Quartz Scheduler in our Spring Boot Application.
There is a scenario for which we want to disable it.
We have tried using #ConditionalOnProperty(name = Constant.QUARTZ_ENABLED) option on our class.
We have defined quartz.enabled = false property in application.properties file.
However when we run on Tomcat server locally on Spring Tool Suite it is working as expected (meaning it is not invoking the quartz scheduler).
But when same code is deployed on Weblogic, the scheduler is still running.
Should we try putting #ConditionalOnProperty(name = Constant.QUARTZ_ENABLED) annotation at method level. specifically the method where trigger is used?
We have gone through the options mentioned on various forums but still not reached the solution.
Is there any way we can
1. Stop the scheduler from starting
Or
2. Stop the Job from getting triggered
Thanks in advance.
I have created one quartz job trigger for the spring boot application. Now in case of application restart, I want the quartz trigger to continue its execution from the same point when the application was restarted. I was looking for some solution over the internet and found PersistJobDataAfterExecution annotation in quartz. I want to know how this annotation works and how it can be used in Java?
I'd like to run a Spring Boot service without any of the controller-related stuff. I'd like it to just run a scheduled task every hour and do work if needed. I'm wanting to use Spring Boot, because I already know how to set the Hibernate ORM up, and I'm re-using a lot of the same repositories as another Spring Boot service. So, I spun up a new Spring Boot project and left out the start-web package.
The main issue I'm running into is that despite having a scheduled task set up, the service starts and immediately quits without running the scheduled task. In my head, I imagined the service kind of just sitting there, running, waiting for the time to trigger the scheduled job I have configured and kind of just sleeping until then. Are my expectations bad, or do I just have it misconfigured?
I've solved the problem. It was a configuration issue causing the Spring Boot app to not recognize the presence of required configuration values in the application.properties file.
You can either remove spring-boot-starter-web from your dependencies or in your main class you can configure the SpringApplicationBuilder to not include the web server.
new SpringApplicationBuilder(YourApplication.class)
.web(WebApplicationType.NONE)
.run(args);
Use #EnableScheduling in main class and application won't shuts down.
I am using J2EE with Jboss server. I am trying to finding a way to invoke sendEmail api in my code every month.
#GET
#Path("/sendEmail")
#Transactional
public String test(){
I want to invoke this test api which can be accessed using web-browser http://localhost:8181/api/calc/sendEmail
I found some ways to do this:
https://cloud.google.com/appengine/docs/java/config/cron
https://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/
Using cron job looks intuitive way to do this but I find it difficult to search resources to find a way to schedule invoking of the APIs using it.
Please point me to some resources where I can find a way to do so by just adding single dependency for this purpose in pom.xml
You can use Spring Framework for this.
Something like this:
#Scheduled(cron = "0 0 12 1 1/1 ? *")
public void doScheduledWork() {
Check the following link: Spring cron expression for every day 1:01:am
When the application is deployed in multiple boxes or multiple pods(in case of kubernetes) then in each pods the cron expression gets set.
This results in the cron getting triggered n(number of pods) times which results in error case. Better option is to call the cron from external monolith configuration systems by calling through an api.
Applying on a method with the annotation is apt if application runs in only one box(pure monolith).