Developing the writer for your custom work queue

In your custom work queue class, override the findTargets method that your custom work queue class inherits from WorkQueueBase to provide your specific logic for a writer task.

Important: Do not operate on any of the units of work in a batch within your writer logic. Otherwise, process history statistics for the batch will be incorrect.

In the findTargets method logic, return a query builder iterator with the results you want as targets for the work items in a batch. PolicyCenter uses the iterator to write the work items to the work queue. The findTargets method is a template method for which you specify the target entity type, the same type for which you make a query builder object.

The following example code is a writer for a work queue that operates on Activity instances. The query selects activities that have not been viewed for five days or more and returns the iterator.

override function findTargets (): Iterator  <Activity> {

  // Query the target type and apply conditions: activities not viewed for 5 days or more
  var targetsQuery = Query.make(Activity)
  targetsQuery.compare(Activity#LastViewedDate, LessThanOrEquals, java.util.Date.Today.addBusinessDays(-5))

  return targetsQuery.select().iterator()

}

Returning an empty iterator

Generally, if your writer returns an iterator with items found by the query, PolicyCenter creates a ProcessHistory instance for the batch run. Sometimes the query in your writer finds no qualifying items. If your writer returns an empty iterator, PolicyCenter does not create a process history for that batch processing run.

Writing custom work item types

Suppose that you are creating a custom work queue class and decide not to use the StandardWorkItem work item and instead define a custom work item type. In you create your own class, you must override the createWorkItem method inherited from WorkQueueBase to write new work items to your custom work queue table. The createWorkItem method has two parameters.

  • target – An object reference to an entity instance in the iterator returned from the findTargets method.
  • safeBundle – A transaction bundle provided by PolicyCenter to manage updates to the work queue table.

Your method implementation must create a new work item of the custom work item type that you defined. Pass the safeBundle parameter in the new work item statement. Then, assign the target parameter to the target unit of work field that you defined for you custom work item type. If you defined additional fields, assign values to them, as well.

The return type for the createWorkItem method is any entity type that implements the WorkItem delegate. Return your new custom work item type.

The following Gosu example code creates a new custom work item that has a single custom field, an Activity instance. The implementation gives the target field in the parameter list the name activity to clarify the code. The custom work item type is MyWorkItem.

override function createWorkItem (activity : Activity, safeBundle : Bundle) : MyWorkItem {
  var customWorkItem = new MyWorkItem(safeBundle)
  customWorkItem.Activity = activity
  return customWorkItem
}