Package org.cyclos.impl.utils.tasks
Class EntityBatchPollingTask<E extends IEntity>
- java.lang.Object
-
- org.cyclos.impl.AbstractServerComponent
-
- org.cyclos.impl.AbstractGlobalServerComponent
-
- org.cyclos.impl.utils.tasks.AbstractTask
-
- org.cyclos.impl.utils.tasks.AbstractPollingTask
-
- org.cyclos.impl.utils.tasks.EntityBatchPollingTask<E>
-
- All Implemented Interfaces:
Runnable,PollingTask
- Direct Known Subclasses:
AbstractTransactionFeedbackPollingTask,AdExpiredNotificationPollingTask,AdInterestNotificationPollingTask,CleanDirtyBalancesPollingTask,ExpireExternalPaymentPollingTask,ExpirePendingAuthorizationPaymentPollingTask,ExpireVouchersPollingTask,InstallmentProcessingPollingTask,InternetAddressResolutionPollingTask,MailingProcessingPollingTask,MailProcessingPollingTask,MessageProcessingPollingTask,MoveUsersToGroupPollingTask,NotificationProcessingPollingTask,NotifyVoucherBuyingAboutToExpirePollingTask,NotifyVouchersAboutToExpirePollingTask,PaymentRequestProcessingPollingTask,PurgeUnconfirmedUsersPollingTask,TicketProcessingPollingTask
public abstract class EntityBatchPollingTask<E extends IEntity> extends AbstractPollingTask
APollingTaskwhich processes entities with the given workflow:- Calls, within a transaction,
nextIds(), which should return the identifiers of the next objects to process; - If null or an empty list is returned, the current execution is over. Otherwise:
- Calls
#process(Object), loading one entity at a time. That method should actually perform the task over the given object; - Regardless of whether the processing of the object has failed, invokes
#finish(Object, Throwable)in a new transaction. That method should mark the object as processed, so it should never be returned again bynextIds().
-
-
Field Summary
Fields Modifier and Type Field Description static intBATCH_SIZEThe maximum expected size ofnextIds().protected Class<E>entityType-
Fields inherited from class org.cyclos.impl.utils.tasks.AbstractTask
errorLogService, invokerHandler, loggingHandler
-
Fields inherited from class org.cyclos.impl.AbstractGlobalServerComponent
rawEntityManagerHandler
-
Fields inherited from class org.cyclos.impl.AbstractServerComponent
accountHandler, configurationHandler, dataTranslationHandler, notificationHandler, profileFieldHandler, transactionHandler, translationHandler
-
-
Constructor Summary
Constructors Constructor Description EntityBatchPollingTask(Class<E> entityType)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voiddoRun()Subclasses must implement this method to do the task.protected Efind(Long id)protected abstract voidfinish(E entity, Throwable error)Must be implemented in order to mark the given entity as finished.protected voidgenerateErrorLog(Long networkId, String stackTrace)Generates an error log which is bound to network with the given id.protected voidgenerateErrorLog(Long networkId, Throwable error)protected booleanlogProcessingErrors(E entity)Returns whether a log should be generated when an entity processing failsprotected abstract Collection<Long>nextIds()Must be implemented in order to return the ids of the next entities to be processed, without actually processing them.protected voidonFinish()Callback invoked when the task has finishedprotected abstract voidprocess(E entity)Must be implemented in order to actually process the given entity, without change it's state to already processedprotected NetworkresolveNetwork(E entity)May be overridden in order to resolve the correct network for the given entity.protected booleanshouldGenerateErrorLog()The error log onEntityBatchPollingTasks is handled in another method, which knows which entity has failedprotected booleanshouldGenerateErrorLog(Long networkId, Throwable error)protected booleanshouldSkipFinish(Long id, Throwable error)Returns whether the entity with the given id, having the given error whenprocess(IEntity)was executed, needs or not thefinish(IEntity, Throwable)method.-
Methods inherited from class org.cyclos.impl.utils.tasks.AbstractPollingTask
dateFromDelay, getFirstExecution, getInitialSleepSeconds, getNextExecution, getSecondsUntilNextDay, getSecondsUntilNextHour, getSleepSeconds, getStorage, initialize, isExclusiveInCluster, run
-
Methods inherited from class org.cyclos.impl.utils.tasks.AbstractTask
getLogConfiguration, getName, logName, logTaskError, logTaskSuccess, toString
-
Methods inherited from class org.cyclos.impl.AbstractGlobalServerComponent
getBaseEntityManagerHandler, getEntityManager
-
Methods inherited from class org.cyclos.impl.AbstractServerComponent
dataTranslationProxy, delete, detach, find, flush, from, getApplicationContext, getFormatter, getFormatter, getFormatter, getLogger, getRemoteAddress, getSessionData, mailContentBuilder, message, message, persist, processBatch, processBatch, refresh, remove, selectFrom, subQuery, update
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.cyclos.impl.utils.tasks.PollingTask
getName
-
-
-
-
Field Detail
-
BATCH_SIZE
public static final int BATCH_SIZE
The maximum expected size ofnextIds(). Doesn't mean that this many entities will be processed in a single transaction, as they are processed one at a time (in separated transactions).- See Also:
- Constant Field Values
-
-
Method Detail
-
doRun
protected final void doRun()
Description copied from class:AbstractPollingTaskSubclasses must implement this method to do the task.- Specified by:
doRunin classAbstractPollingTask
-
finish
protected abstract void finish(E entity, Throwable error) throws Throwable
Must be implemented in order to mark the given entity as finished. Even when {@link #process(IEntity))} fails, this method is invoked. Runs in another transaction, passing the error ifprocess(IEntity)yielded an error.- Throws:
Throwable
-
generateErrorLog
protected final void generateErrorLog(Long networkId, String stackTrace)
Generates an error log which is bound to network with the given id. May be overridden to no-op if subclasses don't want an error log
-
logProcessingErrors
protected boolean logProcessingErrors(E entity)
Returns whether a log should be generated when an entity processing fails
-
nextIds
protected abstract Collection<Long> nextIds() throws Throwable
Must be implemented in order to return the ids of the next entities to be processed, without actually processing them. Returning null or an empty list means there is no more data for now, and will make this task sleep- Throws:
Throwable
-
onFinish
protected void onFinish()
Callback invoked when the task has finished
-
process
protected abstract void process(E entity) throws Throwable
Must be implemented in order to actually process the given entity, without change it's state to already processed- Throws:
Throwable
-
resolveNetwork
protected Network resolveNetwork(E entity)
May be overridden in order to resolve the correct network for the given entity. By default, if the entity is aBaseEntity, return its network, otherwise, null
-
shouldGenerateErrorLog
protected final boolean shouldGenerateErrorLog()
The error log onEntityBatchPollingTasks is handled in another method, which knows which entity has failed- Overrides:
shouldGenerateErrorLogin classAbstractTask
-
shouldSkipFinish
protected boolean shouldSkipFinish(Long id, Throwable error)
Returns whether the entity with the given id, having the given error whenprocess(IEntity)was executed, needs or not thefinish(IEntity, Throwable)method. If this method returns true, finish() is not executed
-
-