ePages 6.17.35 - DE_EPAGES/Trigger/API/Object/BatchProcessHandler.pm

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
batchSize
cleanupItems
countItems
items
new
packageName
processItem
processItemList

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
$Item (type depends on impl of processItem($item))
item for processing

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
$BatchSize (integer)
number of items this handler can store before auto
processing, else 0

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
$count (integer)
current count of unprocessed items

items

Returns list of items, accumulated by addItem, for this handler.

Syntax
$aItemList = $Handler->items();
Return
$aItemList (ref.array)
list of items stored by this handler

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
$hOptions (ref.hash)
  • BatchSize - integer > 1 (optional)
Return
$object (object)
new batch process handler

packageName

Returns the package name.

Syntax
$PackageName = $Handler->packageName();
Return
$PackageName (string)
name of the package

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
$Item (scalar)
item for processing, see addItem

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();