0

I have a job that runs on the hour. I would like to either call a Controller's Action method like this:

public class MyJob extends Job {

    @Override
    public void doJob() throws Exception {
        MyController.someActionMethod();
    }
}

or call a URL directly.

Any ideas if this is possible from within a Job?

If I call a Controller's action method directly, I get this:

NullPointerException occured : null

play.exceptions.JavaExecutionException
    at play.jobs.Job.call(Job.java:155)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
    at play.classloading.enhancers.ControllersEnhancer$ControllerInstrumentation.isActionCallAllowed(ControllersEnhancer.java:187)
    at controllers.MyController.someActionMethod(MyController.java)
    at fun.job.MyJob.doJob(MyJob.java:10)
    at play.jobs.Job.doJobWithResult(Job.java:50)
    at play.jobs.Job.call(Job.java:146)
    ... 7 more
digiarnie
  • 22,305
  • 31
  • 78
  • 126

2 Answers2

2

You can use the WS helper class. For example,

HttpResponse response = WS.url("http://www.yahoo.com/").get()
String html = response.getString(); // or...
Document xml = response.getXml();

What you are doing above would try to do a browser redirect to the URL associated with a controller's action, which doesn't make sense from the context of an asynchronous job, where, among other reasons, there is no browser to process the redirect.

Lawrence McAlpin
  • 2,745
  • 20
  • 24
  • I tried this, and it seems to work OK for an external URL (like Yahoo), but not for a URL on the same server, i.e. to call my own controller action method, as the OP described. Stepping through the job in the debugger shows that it hangs in the call to `get()`, and eventually times out after 60 seconds. Any ideas? – Will Hains Aug 30 '13 at 03:46
1

Why are you trying to do that? Maybe a better way to achieve the same thing is extract the logic executed by the action into a new class/method and than call it in both controller and job.

marcospereira
  • 12,045
  • 3
  • 46
  • 52
  • I am using the PDF module to PDF a template. The current version of the PDF module (0.7) requires an HTTP request. I want to perform PDF'ing every 15 minutes hence the use of a Job. However, I need access to a request to get the PDF module to work. – digiarnie Dec 13 '11 at 01:46