Search in sources :

Example 1 with TransactionListenerAdapter

use of org.alfresco.util.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class ScheduledPersistedActionServiceImpl method addToScheduler.

/**
 * Builds up the Quartz details, and adds it to the Quartz
 *  scheduler when the transaction completes.
 * We have to wait for the transaction to finish, otherwise
 *  Quartz may end up trying and failing to load the details
 *  of a job that hasn't been committed to the repo yet!
 */
protected void addToScheduler(ScheduledPersistedActionImpl schedule) {
    // Wrap it up in Quartz bits
    final JobDetail details = buildJobDetail(schedule);
    final Trigger trigger = schedule.asTrigger();
    // As soon as the transaction commits, add it
    AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

        @Override
        public void afterCommit() {
            // Schedule it with Quartz
            try {
                scheduler.scheduleJob(details, trigger);
            } catch (SchedulerException e) {
                // Probably means scheduler is shutting down
                log.warn(e);
            }
        }
    });
}
Also used : JobDetail(org.quartz.JobDetail) TransactionListenerAdapter(org.alfresco.util.transaction.TransactionListenerAdapter) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 2 with TransactionListenerAdapter

use of org.alfresco.util.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class AbstractNodeDAOImpl method bindFixAssocAndCollectLostAndFound.

private void bindFixAssocAndCollectLostAndFound(final Pair<Long, NodeRef> lostNodePair, final String lostName, final Long assocId, final boolean orphanChild) {
    // Remember the items already deleted in inner transactions
    final Set<Pair<Long, NodeRef>> lostNodePairs = TransactionalResourceHelper.getSet(KEY_LOST_NODE_PAIRS);
    final Set<Long> deletedAssocs = TransactionalResourceHelper.getSet(KEY_DELETED_ASSOCS);
    AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

        @Override
        public void afterRollback() {
            afterCommit();
        }

        @Override
        public void afterCommit() {
            if (transactionService.getAllowWrite()) {
                // New transaction
                RetryingTransactionCallback<Void> callback = new RetryingTransactionCallback<Void>() {

                    public Void execute() throws Throwable {
                        if (assocId == null) {
                            // 'child' with missing parent assoc => collect lost+found orphan child
                            if (lostNodePairs.add(lostNodePair)) {
                                collectLostAndFoundNode(lostNodePair, lostName);
                                logger.error("ALF-13066: Orphan child node has been re-homed under lost_found: " + lostNodePair);
                            }
                        } else {
                            // collect lost+found orphan child
                            if (deletedAssocs.add(assocId)) {
                                // Can't use caching version or may hit infinite loop
                                deleteChildAssoc(assocId);
                                logger.error("ALF-12358: Deleted node - removed child assoc: " + assocId);
                            }
                            if (orphanChild && lostNodePairs.add(lostNodePair)) {
                                collectLostAndFoundNode(lostNodePair, lostName);
                                logger.error("ALF-12358: Orphan child node has been re-homed under lost_found: " + lostNodePair);
                            }
                        }
                        return null;
                    }
                };
                transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true);
            }
        }
    });
}
Also used : TransactionListenerAdapter(org.alfresco.util.transaction.TransactionListenerAdapter) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Pair(org.alfresco.util.Pair)

Example 3 with TransactionListenerAdapter

use of org.alfresco.util.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class MailActionExecuter method executeImpl.

/**
 * Send an email message
 *
 * @throws org.alfresco.error.AlfrescoRuntimeException
 */
@Override
protected void executeImpl(final Action ruleAction, final NodeRef actionedUponNodeRef) {
    // Prepare our messages before the commit, in-case of deletes
    MimeMessageHelper[] messages = null;
    if (validNodeRefIfPresent(actionedUponNodeRef)) {
        messages = prepareEmails(ruleAction, actionedUponNodeRef);
    }
    final MimeMessageHelper[] finalMessages = messages;
    // Send out messages
    if (finalMessages != null) {
        if (sendAfterCommit(ruleAction)) {
            AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

                @Override
                public void afterCommit() {
                    RetryingTransactionHelper helper = serviceRegistry.getTransactionService().getRetryingTransactionHelper();
                    helper.doInTransaction(new RetryingTransactionCallback<Void>() {

                        @Override
                        public Void execute() throws Throwable {
                            for (MimeMessageHelper message : finalMessages) {
                                sendEmail(ruleAction, message);
                            }
                            return null;
                        }
                    }, false, true);
                }
            });
        } else {
            for (MimeMessageHelper message : finalMessages) {
                if (message != null) {
                    sendEmail(ruleAction, message);
                }
            }
        }
    }
}
Also used : TransactionListenerAdapter(org.alfresco.util.transaction.TransactionListenerAdapter) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 4 with TransactionListenerAdapter

use of org.alfresco.util.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class ActionTrackingServiceImpl method recordActionComplete.

private void recordActionComplete(final ActionImpl action) {
    if (logger.isDebugEnabled() == true) {
        logger.debug("Action " + action + " has completed execution");
    }
    // Mark it as having worked
    action.setExecutionEndDate(new Date());
    action.setExecutionStatus(ActionStatus.Completed);
    action.setExecutionFailureMessage(null);
    // Do we need to update the persisted details?
    if (action.getNodeRef() != null && nodeService.exists(action.getNodeRef())) {
        // Make sure we re-fetch the latest action details and save
        // this version back into the repository
        // (That way, if someone has a reference to the
        // action and plays with it, we still save the
        // correct information)
        final Date startedAt = action.getExecutionStartDate();
        final Date endedAt = action.getExecutionEndDate();
        final NodeRef actionNode = action.getNodeRef();
        AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

            public void afterCommit() {
                transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

                    public Object execute() throws Throwable {
                        // Update the action as the system user
                        return AuthenticationUtil.runAs(new RunAsWork<Action>() {

                            public Action doWork() throws Exception {
                                // between when it loaded running and now
                                if (!nodeService.exists(actionNode)) {
                                    // Persisted node has gone, nothing to update
                                    return null;
                                }
                                // Grab the latest version of the action
                                ActionImpl action = (ActionImpl) runtimeActionService.createAction(actionNode);
                                // Update it
                                action.setExecutionStatus(ActionStatus.Completed);
                                action.setExecutionFailureMessage(null);
                                action.setExecutionStartDate(startedAt);
                                action.setExecutionEndDate(endedAt);
                                runtimeActionService.saveActionImpl(actionNode, action);
                                // All done
                                return action;
                            }
                        }, AuthenticationUtil.SYSTEM_USER_NAME);
                    }
                }, false, true);
            }
        });
    }
    // Remove it from the cache, as it's finished
    String key = generateCacheKey(action);
    executingActionsCache.remove(key);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) TransactionListenerAdapter(org.alfresco.util.transaction.TransactionListenerAdapter) Action(org.alfresco.service.cmr.action.Action) CancellableAction(org.alfresco.service.cmr.action.CancellableAction) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Date(java.util.Date) ActionServiceTransientException(org.alfresco.service.cmr.action.ActionServiceTransientException) UnknownHostException(java.net.UnknownHostException)

Example 5 with TransactionListenerAdapter

use of org.alfresco.util.transaction.TransactionListenerAdapter in project alfresco-repository by Alfresco.

the class ActionTrackingServiceImpl method recordActionFailure.

/**
 * Schedule the recording of the action failure to occur in another
 * transaction
 */
public void recordActionFailure(Action action, final Throwable exception) {
    if (logger.isDebugEnabled() == true) {
        if (exception instanceof ActionCancelledException) {
            logger.debug("Will shortly record completed cancellation of action " + action);
        } else if (exception instanceof ActionServiceTransientException) {
            logger.debug("Will shortly record transient failure of action " + action);
        } else {
            logger.debug("Will shortly record failure of action " + action + " due to " + exception.getMessage());
        }
    }
    // Record when it finished
    ((ActionImpl) action).setExecutionEndDate(new Date());
    // Record it as Failed or Cancelled, depending on the exception
    if (exception instanceof ActionCancelledException) {
        ((ActionImpl) action).setExecutionStatus(ActionStatus.Cancelled);
        ((ActionImpl) action).setExecutionFailureMessage(null);
    } else if (exception instanceof ActionServiceTransientException) {
        ((ActionImpl) action).setExecutionStatus(ActionStatus.Declined);
        ((ActionImpl) action).setExecutionFailureMessage(exception.getMessage());
    } else {
        ((ActionImpl) action).setExecutionStatus(ActionStatus.Failed);
        ((ActionImpl) action).setExecutionFailureMessage(exception.getMessage());
    }
    // Remove it from the cache, as it's no longer running
    String key = generateCacheKey(action);
    executingActionsCache.remove(key);
    // Do we need to update the persisted details?
    if (action.getNodeRef() != null) {
        // Take a local copy of the details
        // (That way, if someone has a reference to the
        // action and plays with it, we still save the
        // correct information)
        final String actionId = action.getId();
        final Date startedAt = action.getExecutionStartDate();
        final Date endedAt = action.getExecutionEndDate();
        final String message = action.getExecutionFailureMessage();
        final NodeRef actionNode = action.getNodeRef();
        // Have the details updated on the action as soon
        // as the transaction has finished rolling back
        AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {

            public void afterRollback() {
                transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Object>() {

                    public Object execute() throws Throwable {
                        // Update the action as the system user
                        return AuthenticationUtil.runAs(new RunAsWork<Action>() {

                            public Action doWork() throws Exception {
                                // Grab the latest version of the
                                // action
                                ActionImpl action = (ActionImpl) runtimeActionService.createAction(actionNode);
                                // Update it
                                if (exception instanceof ActionCancelledException) {
                                    action.setExecutionStatus(ActionStatus.Cancelled);
                                    action.setExecutionFailureMessage(null);
                                } else {
                                    action.setExecutionStatus(ActionStatus.Failed);
                                    action.setExecutionFailureMessage(exception.getMessage());
                                }
                                action.setExecutionStartDate(startedAt);
                                action.setExecutionEndDate(endedAt);
                                runtimeActionService.saveActionImpl(actionNode, action);
                                if (logger.isDebugEnabled() == true) {
                                    logger.debug("Recorded failure of action " + actionId + ", node " + actionNode + " due to " + message);
                                }
                                // All done
                                return action;
                            }
                        }, AuthenticationUtil.SYSTEM_USER_NAME);
                    }
                }, false, true);
            }
        });
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) TransactionListenerAdapter(org.alfresco.util.transaction.TransactionListenerAdapter) Action(org.alfresco.service.cmr.action.Action) CancellableAction(org.alfresco.service.cmr.action.CancellableAction) ActionServiceTransientException(org.alfresco.service.cmr.action.ActionServiceTransientException) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) Date(java.util.Date) ActionServiceTransientException(org.alfresco.service.cmr.action.ActionServiceTransientException) UnknownHostException(java.net.UnknownHostException)

Aggregations

TransactionListenerAdapter (org.alfresco.util.transaction.TransactionListenerAdapter)6 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)5 UnknownHostException (java.net.UnknownHostException)2 Date (java.util.Date)2 Action (org.alfresco.service.cmr.action.Action)2 ActionServiceTransientException (org.alfresco.service.cmr.action.ActionServiceTransientException)2 CancellableAction (org.alfresco.service.cmr.action.CancellableAction)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 UserTransaction (javax.transaction.UserTransaction)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 BaseSpringTest (org.alfresco.util.BaseSpringTest)1 Pair (org.alfresco.util.Pair)1 Test (org.junit.Test)1 JobDetail (org.quartz.JobDetail)1 SchedulerException (org.quartz.SchedulerException)1 Trigger (org.quartz.Trigger)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1