Implementing Jobs
This information covers implementing jobs in java. If you want to schedule execution of scripts, please see our Scripting Jobs topic.
SchedulableJob Interface
Implementing jobs in Espresso couldn't be easier. All functionality in Espresso is exposed in an interface requiring implementation of a single method. com.carfey.ops.job.SchedulableJob's contract is simply public void execute(Context context) throws Exception.
In your implementation, the single method would do whatever work is required and is free to throw any Exception that is then handled by Espresso. If you have no parameterization or any dependency on runtime information, that's all you need do. Perhaps you are able to just call some existing class that provides all the functionality that you need to schedule. Here's an example you can follow.
import com.carfey.ops.job.Context;
import com.carfey.ops.job.SchedulableJob;
public class MyScheduledJob implements SchedulableJob {
public void execute(Context context) throws Exception {
MyExistingFunction function = new MyExistingFunction();
function.go();
}
}
Parameterization
If you would like to parameterize jobs, you can define parameters or use custom parameters. Custom parameters are just added during configuration whereas defined parameters are specified in the class using our annotation. This makes configuration of your jobs in the web admin app support validation of types and provision of mandatory parameters. In this example, the job will now require provision of a url parameter of type String, and optionally supports a param name for saving the results and a boolean to determine compression on/off.
import com.carfey.ops.job.param.Configuration;
import com.carfey.ops.job.param.Parameter;
import com.carfey.ops.job.param.Type;
@Configuration(knownParameters={
@Parameter(name="url", required=true, type=Type.STRING),
@Parameter(name="saveResultsParam", required=false, type=Type.STRING),
@Parameter(name="compressResults", required=false, type=Type.BOOLEAN)
})
public class MyScheduledJob implements SchedulableJob {
If you are running parameterized jobs, these parameters are very easy to access. Both defined and custom parameters are accessed in the same way. Example:
public void execute(Context context) throws Exception {
JobConfig config = context.getConfig();
String url = config.getString("url");
MyExistingFunction function = new MyExistingFunction();
function.setUrl(url);
function.go();
}
Here are all the available methods on JobConfig for retrieving your named parameters.
- java.lang.Boolean getBoolean(java.lang.String name)
- java.util.List<java.lang.Boolean> getBooleanList(java.lang.String name)
- java.math.BigDecimal getDecimal(java.lang.String name)
- java.util.List<java.math.BigDecimal> getDecimalList(java.lang.String name)
- java.lang.Integer getInt(java.lang.String name)
- java.util.List<java.lang.Integer> getIntList(java.lang.String name)
- java.lang.Long getLong(java.lang.String name)
- java.util.List<java.lang.Long> getLongList(java.lang.String name)
- java.lang.String getString(java.lang.String name)
- java.util.List<java.lang.String> getStringList(java.lang.String name)
Job Results
Espresso also allows for storing information about your job execution. This information is then available for use in chained/resubmitted jobs. Note this example that both evaluates source job information and saves state from its own execution.
public void execute(Context context) throws Exception {
String resultsParamName = context.getConfig().getString("saveResultsParam");
Map<String, List<Object>> sourceJobResults = context.getSourceJobResults();
List<Object> oldResultsList = sourceJobResults.get(resultsParamName);
String oldResults = (String) oldResultsList.get(0);
... job execution ...
context.saveJobResult(resultsParamName, oldResults + newResults);
}
The signatures for the two methods used for retrieving and storing results:
- java.util.Map<java.lang.String,java.util.List<java.lang.Object>> getSourceJobResults()
- void saveJobResult(java.lang.String name, java.lang.Object value)