1

I need to create a thread, which everyday checks whether I have to create the tasks for this user or not. I know to create and run java threads by using main(). But how to run it in web application. Seriously I searched a lot and didn't get any answer for running in web application. I have few questions regarding this.

1 How my thread will initially start and from where it will run?

2 Do I need to define my thread in any XML file ?

This is my thread

public class TaskGenerationThread implements Runnable {


@Override
public void run(){
    System.out.println("callled at "+ new Date());
    /*try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }*/
    /*List<Users> listOfCA = complianceUserService.getAllCA();
    if(listOfCA !=null && !listOfCA.isEmpty()){
        Users ca = complianceUserService.fetchUserByUserId(1).get(0);
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();         
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)); //getting first day of month
        Date nextMonthFirstDay = calendar.getTime();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); //getting last day of month
        Date nextMonthLastDay = calendar.getTime();
        taskGeneratorService.generateCronJobTaskForCompanyCompliance(nextMonthFirstDay,nextMonthLastDay,ca);
    }*/

}

}

I did this way. Implemented ServletContextListener and passed my Thread object. But didnt work

public class ThreadImplementation implements ServletContextListener{

ScheduledExecutorService  listChecker =null;

@Override
public void contextInitialized(ServletContextEvent sce){


    listChecker = Executors.newScheduledThreadPool(1);
    listChecker.scheduleAtFixedRate(new TaskGenerationThread(), 01, 01, TimeUnit.SECONDS); 

}

@Override
public void contextDestroyed(ServletContextEvent sce) {

        if (listChecker != null) {
            listChecker.shutdownNow();
            try {
                listChecker.awaitTermination(60, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }   



 }

 }

Any help will be highly appreciated. Thank you

1 Answer 1

1

Write a class that

implements ServletContextListener

override

public void contextInitialized(ServletContextEvent sce) 

and kick off some scheduler from here

e.g.

listChecker = Executors.newScheduledThreadPool(1);
listChecker.scheduleAtFixedRate(filechecker, 60, 60, TimeUnit.SECONDS); 

note this works for Tomcat

Your contextDestroyed should be something like

@Override
public void contextDestroyed(ServletContextEvent sce) {

    log.info("Scheduler entered contextDestroyed");

    if (listChecker != null) {
        listChecker.shutdownNow();
        log.info("waiting [60 seconds] for collector threads to finsih");
        try {
            listChecker.awaitTermination(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }   

    log.info("Scheduler finished contextDestroyed");
}

And of course you need to add it to web.xml

<listener>
    <listener-class>
             myPackage.Scheduler 
        </listener-class>
   </listener>
Sign up to request clarification or add additional context in comments.

7 Comments

In my example above I am just using an java.util.concurrent.ScheduledExecutorService and passing in a Runnable class filechecker
You shouldn't really be extending Thread instead implement Runnable
changed to Runnable. But not yet working . Am I missing something ? :(
some thing like this
<listener-class>com.uforic.compliance.spring.task.Scheduler </listener-class>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.