views:

35

answers:

7

Hi all! I have next task: I need to load the same file into my web app several times, for example - twice a day! Suppose in that file I have information, that changes, and I need to load this info into my app to change the statistics for example.

How can I load file several times (twice an hour, or twice a day)? What should I use? Is any algorithm to do that?

I am not allowed to use external libraries like Quartz Scheduler. So I need to do it with Thread and/or Timer. Can anybody give me some example or algorithm how to do it. Where can I create the entry point to my Thread, can I do it in managed bean or I need some sort of filter/listener/servlet. I works with jsf and richFaces. Maybe in this technologies there are some algorithms to solve my problem.

Any ideas?

Thanks very much for help!

A: 

how about this:

http://www.quartz-scheduler.org/overview/index.html

mkoryak
A: 

I'd use the Quartz Scheduler. I don't know the architecture of your app so I can't really say if Spring configuration or just code configuration is better. You could use a SimpleTrigger, or even a CronTrigger if you want a more expressive scheduling.

extraneon
+1  A: 

Check java.util.Timer, it should be just enough for what you need

bobah
A: 

I would expose entry point to 'load' functionality as a servlet in your webapp and then use external scheduler (cron on Unix, Scheduled Tasks on Windows) to call that servlet via wget or any other command line http client.

This approach has advantages of not depending on any third party libraries (adding Qurtz just for one task seems like overkill for me) and also has flexibilty of changing schedule without touching your code as well as triggering 'load' manually if desired.

maximdim
A: 

Hi all! I am not allowed to use external libraries like Quartz Scheduler. So I need to do it with Thread and/or Timer. Can anybody give me some example or algorithm how to do it. Where can I create the entry point to my Thread, can I do it in managed bean or I need some sort of filter/listener/servlet. I works with jsf and richFaces. Maybe in this technologies there are some algorithms to solve my problem.

Any ideas?

Thanks very much for help!

Elena
Please update the question, not in an answer:) I just did that for you.
extraneon
+2  A: 

If you cannot use a scheduler, then use a servlet and Timer.

In this article it is described how to do that. It's exactly what you need.

extraneon
A: 

Thanks all for help - I do this task with help of Timer, TimerTask and ServletContextListener:

        servletContext = event.getServletContext();

        // create the timer and timer task objects
        Timer timer = new Timer();
        // get a calendar to initialize the start time
        Date startTime = Calendar.getInstance().getTime();

        List<Company> companies = CompanyUtils.getInstance().getCompanies();
        if (companies.size() == 0)
            return;

        for (int i = 0; i < companies.size(); i++) 
        {
            FileUpdater task = new FileUpdater(companies.get(i).getUrl());

            // schedule the task to run hourly
            timer.scheduleAtFixedRate(task, startTime, companies.get(i).getUpdatePeriod());
        }

        // save our timer for later use
        servletContext.setAttribute("timer", timer);

Once more thanks!

With best wishes!

Elena