1

I have a web application, I need to run a backgroung process which will hit a web-service, after getting the response it will wait for few seconds(say 30) then again hit the service. The response data can vary from very less to very large, so i dont want to call the processagain untill i am finished with processing of data. So, its a recursive call with a time delay. How i intend to do is:

  1. Add a ContextListener to web app.

  2. On contextIntialized() method , call invokeWebService() i.e. arbitary method to hit web service.

  3. invokeWebService will look like:

    invokeWebService()
    {
    
    //make request
    
    //hit service
    
    //get response
    
    //process response
    
    timeDelayInSeconds(30);
    
    //recursive call
    invokeWebService();
    
    }
    

Pls. suggest whether I am doing it right. Or go with threads or schedulers. Pls. answer with sample codes.

shashankaholic
  • 4,122
  • 3
  • 25
  • 28

3 Answers3

3

You could use a ScheduledExecutorService, which is part of the standard JDK since 1.5:

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            invokeWebService();
        }
    };

    scheduler.scheduleAtFixedRate(r, 0, 30, TimeUnit.SECONDS);
assylias
  • 321,522
  • 82
  • 660
  • 783
1

It is not recursive but repeated. You have two choice here:

  • Use a Timer and a TimerTask with scheduleAtFixedRate
  • Use Quartz with a repeated schedule.

In quartz, you can create a repeated schedule with this code:

TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(30))
                .build()
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Its not repeated, since i dont want to move forward until i processes response of web service. – shashankaholic Apr 16 '12 at 07:24
  • Yes it is repeated. The way you are doing it now will at some point reach a stackOverFlow because you are invoking your code recursively but you are never exiting the recursion. Go go the repeated way to do exactly what you want – Guillaume Polet Apr 16 '12 at 07:27
  • WHat exactly repeatSecondlyForever() do. – shashankaholic Apr 16 '12 at 07:33
  • Quartz is a job scheduling framework. For each Job you define two things: the Job and the Trigger (when it will be performed, repeatedly or not, etc...). The line above will simply create a trigger that will repeat the Job over and over every 30 seconds (until you stop the application) – Guillaume Polet Apr 16 '12 at 07:42
  • `ScheduledThreadPoolExecutor` should be favoured over `Timer` when using Java 1.5+. – assylias Apr 16 '12 at 09:03
1

From what I am getting, waiting sort of implies hanging, which I do not really think is a good idea. I would recommend you use something such as Quartz and run your method at whatever interval you wish.

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application

Tutorials can be accessed here.

As stated in here you can do something like so:

JobDetail existingJobDetail = sched.getJobDetail(jobName, jobGroup);
    if (existingJobDetail != null) {
        List<JobExecutionContext> currentlyExecutingJobs = (List<JobExecutionContext>) sched.getCurrentlyExecutingJobs();
        for (JobExecutionContext jec : currentlyExecutingJobs) {
            if(existingJobDetail.equals(jec.getJobDetail())) {
                //String message = jobName + " is already running.";
                //log.info(message);
                //throw new JobExecutionException(message,false);
            }
        }
        //sched.deleteJob(jobName, jobGroup); if you want to delete the scheduled but not-currently-running job
    }
Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • I tried with scheduler, problem is say process time varies from 2 to 30 minutes depending upon response data. If i take trigger time as 30 minutes, it will be too much for process and if i take 2, next process runs before fisrt has been completed. – shashankaholic Apr 16 '12 at 07:29
  • @shashankaholic: Quartz seems to provide some mechanism to do what you need. – npinti Apr 16 '12 at 07:33
  • Thanks.Can you tell me if annotation @DisallowConcurrentExecution over JOb class will help me here. If not, where exactly i need to implement the code you citated. – shashankaholic Apr 16 '12 at 07:52
  • According to this: http://quartz-scheduler.org/documentation/quartz-2.x/examples/Example4 you need to put it at the very beginning of your class decleration. – npinti Apr 16 '12 at 07:57