How do I pass parameters to Jobs in Play! Framework 1.2.x?

java, playframework, playframework-1.x, promise

Solution

You should:

- Create `private fields` in your ImportCSV class to hold the params required for the job

- Initialize these fieds in your Job class constructor (`this.param1 = param1`)

- Call `new ImportCSV(param1, param2)` to initialize your object

- Access the private fields in your doJobWithResult() method using `this.param1`

The creation of the Promise is good, you will end with:

Promise<String> recordcount = new ImportCSV(param1, param2).now();
String records = await(recordcount);

If you have problems with the constructor of your Job class please update your question and add some code.

Problem

I have a piece of my Play! (1.2.4) application that loads data in from a CSV file and this works fine, but it takes a while, wo I've been trying to farm it off to a Job via the Promise mechanism. The trouble is, there doesn't seem to be a way to pass input parameters (the CSV file and a string for the type of file), because doJobWithResult is an override of a parameter-less method. Job looks like :- ``` public class ImportCSV extends Job<string> { public String doJobWithResult() { do stuff... return my_string; ``` } and is called like :- ``` Promise<String> recordcount = new ImportCSV().now(); String records = await(recordcount); ``` I tried creating a constructor in the Job class that takes those parameters, but then it doesn't trigger when called via the now() method. Any suggestions on how I can pass the data needed to actually perform the job asynchronously?

Original source