Search code examples
javaplayframeworkhttprequestjobs

playframework: Request.current in Job


I use playframework, and I am struggling with a concurrent task:

I have a module in which methods use Request.current (module FbGraph). I need to access these methods from Job, but Job runs on another thread. In this thread, Request.current returns null, and because of this, the module does not work.

Can I pass the value of the current request to Job? Will Request.current return the same value as in the main thread? Or do I need to fix something in the module, and keep the current request value within it?


Solution

  • You need to explicitly pass the request object to your Job class. Suppose you have the following job class:

    public class MyJob extends Job<Object> {
       private Request request;
       public MyJob() {
          request = Request.current();
       }
       public void doJob() {
          // here you use the request object
       }
    }
    

    And use your MyJob:

    public static void myAction() {
       ...
       new MyJob().now();
       ...
    }
    

    For other modules that use Request.current() you will need to either check if you can pass request object to that module, or use that module in your controller action thread.