Package DE_EPAGES::Trigger::API::Object::BatchProcessHandler
Abstract class for processing queued objects.
This class collect items via method addItem($object) and the
processing is startet by processItemList(), which calls
implementation depending processItem() method. If at construction
time parameter 'BatchSize' is specified, than processing is automatically
triggered the moment countItems() >= batchSize() is true
see t/BatchProcessHandler.t for subclassing, injecting options/objects example
see DE_EPAGES::Trigger::API::BatchProcessor - how to create event
based batch processing.
see t/BatchProcessor2.t for an example
Example |
package MyCartridge::Hooks::ProductBatchHandler; use parent qw( DE_EPAGES::Trigger::API::Object::BatchProcessHandler ); #must be implemented sub processItem { my $self = shift; my ($hProduct) = @_; # do something with each object return 'process Product ' . $hProduct->{ProductID} . "\n"; } sub processItemList { my $self = shift; my $products = $self->items(); my $text; foreach my $hProduct (@$products) { $text .= $self->processItem($hProduct); } print $text; # remove processed items from the list $self->cleanupItems(); } |
Functions
addItem
Add item to queue for later processing. If 'BatchSize' is set, this method checks item count and calls processItemList() automatically if countItems() >= batchSize() is true.
Syntax |
$Handler->addItem( $Item ); |
Input |
|
batchSize
Number of items stored by the handler (n), which will trigger processItems() automatically. The handler stores up to n-1 items whithout starting processing. Returns 0 if 'BatchSize' is was not set in new.
Syntax |
$BatchSize = $Handler->batchSize(); |
Return |
|
cleanupItems
Empties the list of items. Baware this method don't distinguish bewteen processed or unprocessed items.
Syntax |
$Handler->cleanupItems(); |
countItems
Number of items stored for processing
Syntax |
$count = $Handler->countItems(); |
Return |
|
items
Returns list of items, accumulated by addItem, for this handler.
Syntax |
$aItemList = $Handler->items(); |
Return |
|
new
You may pass optional parameter 'BatchSize' to define the number of items queued before processItemList() is called automatically.
Syntax |
my $Handler = DE_EPAGES::Trigger::API::Object::BatchProcessHandler->new( {BatchSize => 50} ); my $Handler = DE_EPAGES::Trigger::API::Object::BatchProcessHandler->new(); |
Input |
|
Return |
|
packageName
Returns the package name.
Syntax |
$PackageName = $Handler->packageName(); |
Return |
|
processItem
Abstract, must be implemented!
processItem is called by processItemList to handle each object
separately. If you want control over the whole list prcoessing you must
override method processItemList.
Syntax |
$Handler->processItem($Item); |
Input |
|
processItemList
Passes each item to processItem($hItem), after all items are processed the items are flushed by calling method cleanupItems(). Override this method to handle more than one object at once. You must delete processed items by calling cleanupItems() if you reimplement this method.
Syntax |
$Handler->processItemList(); |