Search in sources :

Example 1 with ActionInvoker

use of com.axway.ats.agent.core.action.ActionInvoker in project ats-framework by Axway.

the class AbstractActionTask method checkForUnusedProvidedParameters.

/**
     * Calculate how many times a parameter is:
     *      - present in all data providers
     *      - used in all action invokers
     *
     * Log a warning message if a parameter present in the data providers more time than it is used by the action invokers
     */
private void checkForUnusedProvidedParameters() {
    // get how many providers provide values for each parameter
    Map<String, Integer> paramsInProvidersMap = new HashMap<String, Integer>();
    for (ParameterDataProvider dataProvider : dataProviders) {
        String paramName = dataProvider.getParameterName();
        if (paramsInProvidersMap.get(paramName) != null) {
            paramsInProvidersMap.put(paramName, paramsInProvidersMap.get(paramName) + 1);
        } else {
            paramsInProvidersMap.put(paramName, 1);
        }
    }
    // get how many times each parameter is used in all actions
    Map<String, Integer> paramsInActionsMap = new HashMap<String, Integer>();
    for (ActionInvoker actionInvoker : actionInvokers) {
        for (String paramName : actionInvoker.getActionMethodParameterNames()) {
            if (paramsInActionsMap.get(paramName) != null) {
                paramsInActionsMap.put(paramName, paramsInActionsMap.get(paramName) + 1);
            } else {
                paramsInActionsMap.put(paramName, 1);
            }
        }
    }
    // warn for unused provided parameters
    for (Entry<String, Integer> paramEntry : paramsInProvidersMap.entrySet()) {
        int timesInProviders = paramEntry.getValue();
        //we iterate on the paramsInProviders map, so we need to make
        //sure that a parameter with this name is present in the actions at all
        int timesInActions = 0;
        if (paramsInActionsMap.containsKey(paramEntry.getKey())) {
            timesInActions = paramsInActionsMap.get(paramEntry.getKey());
        }
        //as this is not a fatal error and can be ignored
        if (timesInProviders > timesInActions) {
            log.warn("'" + paramEntry.getKey() + "' parameter is provided by " + timesInProviders + " data providers while it is used in only " + timesInActions + " actions");
        }
    // (timesInProviders < timesInActions) is OK for example when you want
    // to use constant in one of the action invocations.
    }
}
Also used : ActionInvoker(com.axway.ats.agent.core.action.ActionInvoker) ParameterDataProvider(com.axway.ats.agent.core.threading.data.ParameterDataProvider) HashMap(java.util.HashMap)

Example 2 with ActionInvoker

use of com.axway.ats.agent.core.action.ActionInvoker in project ats-framework by Axway.

the class AbstractActionTask method invokeActions.

/**
     * Invoke one iteration of all actions in the queue
     * 
     * @throws InterruptedException
     */
protected final void invokeActions() throws InterruptedException {
    UserActionsMonitoringAgent userActionsMonitoringAgent = UserActionsMonitoringAgent.getInstance(caller);
    if (log.isDebugEnabled()) {
        log.debug("Starting '" + queueName + "' queue for " + (++nIterations) + "th time");
    }
    //generate the input arguments for all action invokers
    generateInputArguments();
    long queueDuration = 0;
    long actionStartTimestamp = 0;
    long actionEndTimestamp = 0;
    if (this.itManager != null) {
        // inform a new iteration is starting now
        this.itManager.setIterationStartTime(this, System.currentTimeMillis());
    }
    try {
        for (int i = 0; i < actionInvokers.size(); i++) {
            // start cycling all actions in this iteration
            ActionInvoker actionInvoker = actionInvokers.get(i);
            Object actionClassInstance = actionClassInstances.get(i);
            ActionMethod actionMethod = actionInvoker.getActionMethod();
            final String actionName = actionInvoker.getActionName();
            final String transferUnit = actionMethod.getTransferUnit();
            // read some settings
            final boolean registerActionExecution = actionMethod.isRegisterActionExecution();
            final boolean registerActionExecutionInQueueExecutionTime = actionMethod.isRegisterActionExecutionInQueueExecutionTime();
            final boolean isTemplateActionMethod = actionMethod instanceof TemplateActionMethod;
            final boolean logCheckpoints = !isTemplateActionMethod || REGISTER_FULL_AND_NET_ACTION_TIME_FOR_TEMPLATE_ACTIONS;
            // checkpoint name. For template actions by default only network time is tracked.
            // Here "-full" adds total action processing including XML (de)serializations, parameterization
            String checkpointName;
            if (!isTemplateActionMethod) {
                checkpointName = actionName;
            } else {
                checkpointName = actionName + "-full";
            }
            // start a checkpoint
            userActionsMonitoringAgent.actionStarted(actionName);
            if (registerActionExecution) {
                actionStartTimestamp = System.currentTimeMillis();
                if (logCheckpoints && !isLoggingInBatchMode) {
                    log.startCheckpoint(checkpointName, transferUnit, actionStartTimestamp);
                }
            }
            // invoke the current action
            Object actionReturnedResult = null;
            try {
                actionReturnedResult = actionInvoker.invoke(actionClassInstance);
            } catch (Exception e) {
                // the action failed - end the checkpoint
                if (registerActionExecution) {
                    if (logCheckpoints) {
                        if (isLoggingInBatchMode) {
                            log.insertCheckpoint(checkpointName, actionStartTimestamp, 0, 0, transferUnit, CheckpointResult.FAILED);
                        } else {
                            log.endCheckpoint(checkpointName, 0, CheckpointResult.FAILED);
                        }
                    }
                    QueueExecutionStatistics.getInstance().registerActionExecutionResult(queueName, actionName, false);
                }
                // re-throw the exception
                throw e;
            } finally {
                userActionsMonitoringAgent.actionEnded(actionName);
            }
            // the action passed
            if (registerActionExecution) {
                actionEndTimestamp = System.currentTimeMillis();
                long responseTimeMs = actionEndTimestamp - actionStartTimestamp;
                long transferSize = 0;
                if (transferUnit.length() > 0) {
                    transferSize = (Long) actionReturnedResult;
                }
                if (logCheckpoints) {
                    if (isLoggingInBatchMode) {
                        log.insertCheckpoint(checkpointName, actionStartTimestamp, responseTimeMs, transferSize, transferUnit, CheckpointResult.PASSED);
                    } else {
                        log.endCheckpoint(checkpointName, transferSize, CheckpointResult.PASSED, actionEndTimestamp);
                    }
                }
                if (registerActionExecutionInQueueExecutionTime) {
                    if (isTemplateActionMethod) {
                        // add net time in queue instead of full processing time
                        if (actionReturnedResult instanceof CompositeResult) {
                            CompositeResult res = (CompositeResult) actionReturnedResult;
                            responseTimeMs = res.getReqRespNetworkTime();
                        }
                    }
                    queueDuration += responseTimeMs;
                }
                QueueExecutionStatistics.getInstance().registerActionExecutionResult(queueName, actionName, true);
            }
        }
    // end cycling all actions in this iteration
    } catch (Exception e) {
        // We are particularly interested if the thread was interrupted
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof InterruptedException) {
            // the thread was interrupted
            if (this.timedOut) {
                // the thread was interrupted due to timeout, log the timeout and go to next iteration
                log.error("Iteration timed out in " + this.timedOutSeconds + " seconds - skipping to next iteration");
                // reset our flag as we will start another iteration
                this.timedOut = false;
                this.timedOutSeconds = 0;
            } else {
                // the thread interrupted, but not due to timeout, maybe the user cancelled the queue
                this.externallyInterrupted = true;
                throw (InterruptedException) cause;
            }
        } else {
            // some kind of generic exception has occurred
            log.error("Exception caught during invocation - skipping to next iteration", e);
        }
        //continue to the next iteration
        return;
    } finally {
        if (this.itManager != null) {
            this.itManager.clearIterationStartTime();
        }
    }
    if (registerActionsInQueueExecutionTime) {
        log.insertCheckpoint(ATS_ACTION__QUEUE_EXECUTION_TIME, queueDuration, CheckpointResult.PASSED);
    }
}
Also used : CompositeResult(com.axway.ats.agent.core.templateactions.CompositeResult) ActionMethod(com.axway.ats.agent.core.action.ActionMethod) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) NoSuchActionException(com.axway.ats.agent.core.exceptions.NoSuchActionException) NoCompatibleMethodFoundException(com.axway.ats.agent.core.exceptions.NoCompatibleMethodFoundException) NoSuchComponentException(com.axway.ats.agent.core.exceptions.NoSuchComponentException) ActionExecutionException(com.axway.ats.agent.core.exceptions.ActionExecutionException) TemplateActionMethod(com.axway.ats.agent.core.action.TemplateActionMethod) ActionInvoker(com.axway.ats.agent.core.action.ActionInvoker) UserActionsMonitoringAgent(com.axway.ats.agent.core.monitoring.UserActionsMonitoringAgent)

Aggregations

ActionInvoker (com.axway.ats.agent.core.action.ActionInvoker)2 ActionMethod (com.axway.ats.agent.core.action.ActionMethod)1 TemplateActionMethod (com.axway.ats.agent.core.action.TemplateActionMethod)1 ActionExecutionException (com.axway.ats.agent.core.exceptions.ActionExecutionException)1 NoCompatibleMethodFoundException (com.axway.ats.agent.core.exceptions.NoCompatibleMethodFoundException)1 NoSuchActionException (com.axway.ats.agent.core.exceptions.NoSuchActionException)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 UserActionsMonitoringAgent (com.axway.ats.agent.core.monitoring.UserActionsMonitoringAgent)1 CompositeResult (com.axway.ats.agent.core.templateactions.CompositeResult)1 ParameterDataProvider (com.axway.ats.agent.core.threading.data.ParameterDataProvider)1 HashMap (java.util.HashMap)1