2

I created below class to check the scheduling of ManagedScheduledExecutorService class. I am printing message in console.

public class URLHealthProcessor  implements Runnable{
          
    @Override
    public void run() throws Exception {
        
        System.out.println("inside the run method");
}      
 }

I created below class to call this class.

public class URLHealthResource {
    
    @Resource(lookup="java:comp/DefaultManagedScheduledExecutorService")
    private ManagedScheduledExecutorService scheduledExecutorService;
    
    
    public String checkHealthOfApp()
    {
     ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
        ZonedDateTime nextRun = now.withHour(2).withMinute(36).withSecond(0);
        if(now.compareTo(nextRun) > 0)
            nextRun = nextRun.plusMinutes(5);

        Duration duration = Duration.between(now, nextRun);
        long initalDelay = duration.getSeconds();
        
        System.out.println("I am inside checkHealthOfApp method");

        scheduledExecutorService.scheduleAtFixedRate(new URLHealthProcessor(),
            initalDelay,
            TimeUnit.DAYS.toSeconds(1),
            TimeUnit.SECONDS);   
}
}

Actually at a schedule time it is not running.

Do I need to add this class in some container or somewhere else? After creating above classes I restarted webserver but scheduler is not picking the job. basically I want to know how checkHealthOfApp() method will be called in web application.

When I created same taks using ScheduleExecutorService(Java SE) and I ran main method after that job was picking automatically at schedule time.

Do i need to trigger above job somehow manually? I need to create web application so I am using ManagedScheduledExecutorService.

I am new to java. As for as I know I dont need to create main in web application. container loads classes automatically. but here it is not calling method at its time.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 2
    following example will solve your problem. https://www.baeldung.com/spring-scheduled-tasks – sakirow Aug 07 '20 at 14:05
  • thank you @sakirow . Actully I work at oracle . here we are not allowed to use third party like spring or any other framework –  Aug 07 '20 at 14:08
  • 1
    Yet again you forgot to tell the server used. Yet again there's the strange occurrence of [spring] tag while completely nothing in your question is related to Spring. Even in above comment you told that you can't use it. But still you're expecting answers from Spring experts by putting the [spring] tag. Why? In any case, based on one of your previous questions you're actually using Tomcat and not JEE. See abovelinked duplicate for proper ways for both JEE and Tomcat. – BalusC Aug 07 '20 at 23:24
  • @BalusC Please dont mark duplicate. I am really struggling. I put this question 10 hours back and after that I am continuously reading to resolve it. I am using glassfish server. I also checked with tomcat but both did not work. –  Aug 07 '20 at 23:31
  • 2
    The abovelinked duplicate contains proper ways for both JEE and Tomcat. The code in your question doesn't match any of these. Fix that first based on the answer in the duplicate and then rinse and repeat. There's absolutely no need of copypasting the answer of that duplicate here into your question. – BalusC Aug 07 '20 at 23:32
  • 1
    @BalusC I just read it completely. I think it will work. I will try to implement it. thank you so much Balu. –  Aug 07 '20 at 23:36
  • @BalusC the class which you are using ScheduledExecutorService belongs to JavaSE. I need to use Java EE ManagedScheduledExecutorService class. for that I need JNDI. how can I configure JNDI in tomcat server? –  Aug 09 '20 at 20:04
  • 1
    Tomcat is not a JEE server. In JEE, use `@Schedule` (it uses under the covers the `ManagedScheduledExecutorService` and friends without you needing to worry about configuration and all). In Tomcat you need to manage it manually with `ScheduledExecutorService` as demonstrated in the duplicate. – BalusC Aug 09 '20 at 21:24

0 Answers0