Search in sources :

Example 1 with RunState

use of nl.nn.adapterframework.util.RunState in project iaf by ibissource.

the class Adapter method processMessageWithExceptions.

@Override
public PipeLineResult processMessageWithExceptions(String messageId, Message message, PipeLineSession pipeLineSession) throws ListenerException {
    PipeLineResult result = new PipeLineResult();
    long startTime = System.currentTimeMillis();
    boolean processingSuccess = true;
    // prevent executing a stopped adapter
    // the receivers should implement this, but you never now....
    RunState currentRunState = getRunState();
    if (currentRunState != RunState.STARTED && currentRunState != RunState.STOPPING) {
        String msgAdapterNotOpen = "Adapter [" + getName() + "] in state [" + currentRunState + "], cannot process message";
        throw new ListenerException(new ManagedStateException(msgAdapterNotOpen));
    }
    incNumOfMessagesInProcess(startTime);
    String lastNDC = ThreadContext.peek();
    String newNDC = "mid [" + messageId + "]";
    boolean ndcChanged = !newNDC.equals(lastNDC);
    try {
        if (ndcChanged) {
            ThreadContext.push(newNDC);
        }
        if (StringUtils.isNotEmpty(composedHideRegex)) {
            IbisMaskingLayout.addToThreadLocalReplace(composedHideRegex);
        }
        StringBuilder additionalLogging = new StringBuilder();
        String xPathLogKeys = (String) pipeLineSession.get("xPathLogKeys");
        if (StringUtils.isNotEmpty(xPathLogKeys)) {
            StringTokenizer tokenizer = new StringTokenizer(xPathLogKeys, ",");
            while (tokenizer.hasMoreTokens()) {
                String logName = tokenizer.nextToken();
                String xPathResult = (String) pipeLineSession.get(logName);
                additionalLogging.append(" and ");
                additionalLogging.append(logName);
                additionalLogging.append(" [" + xPathResult + "]");
            }
        }
        String format = "Adapter [%s] received message [%s] with messageId [%s]";
        if (msgLog.isEnabled(MSGLOG_LEVEL_TERSE)) {
            String messageOrSize = (isMsgLogHidden()) ? "SIZE=" + getFileSizeAsBytes(message) : message.toString();
            msgLog.log(MSGLOG_LEVEL_TERSE, String.format(format, getName(), messageOrSize, messageId) + additionalLogging);
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format(format, getName(), message, messageId) + additionalLogging);
        } else if (log.isInfoEnabled()) {
            log.info(String.format("Adapter [%s] received message with messageId [%s]" + additionalLogging, getName(), messageId));
        }
        if (Message.isEmpty(message) && isReplaceNullMessage()) {
            log.debug("Adapter [" + getName() + "] replaces null message with messageId [" + messageId + "] by empty message");
            message = new Message("");
        }
        result = pipeline.process(messageId, message, pipeLineSession);
        String duration;
        if (msgLogHumanReadable) {
            duration = Misc.getAge(startTime);
        } else {
            duration = Misc.getDurationInMs(startTime);
        }
        String exitCode = ", exit-code [" + result.getExitCode() + "]";
        String format2 = "Adapter [%s] messageId [%s] duration [%s] got exit-state [%s]" + (result.getExitCode() != 0 ? exitCode : "") + " and result [%s] from PipeLine";
        if (msgLog.isEnabled(MSGLOG_LEVEL_TERSE)) {
            String resultOrSize = (isMsgLogHidden()) ? "SIZE=" + getFileSizeAsBytes(result.getResult()) : result.toString();
            msgLog.log(MSGLOG_LEVEL_TERSE, String.format(format2, getName(), messageId, duration, result.getState(), resultOrSize));
        }
        if (log.isDebugEnabled()) {
            log.debug(String.format(format2, getName(), messageId, duration, result.getState(), result.getResult()));
        }
        return result;
    } catch (Throwable t) {
        ListenerException e;
        if (t instanceof ListenerException) {
            e = (ListenerException) t;
        } else {
            e = new ListenerException(t);
        }
        processingSuccess = false;
        incNumOfMessagesInError();
        addErrorMessageToMessageKeeper("error processing message with messageId [" + messageId + "]: ", e);
        throw e;
    } finally {
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        // reset the InProcess fields, and increase processedMessagesCount
        decNumOfMessagesInProcess(duration, processingSuccess);
        if (log.isDebugEnabled()) {
            // for performance reasons
            log.debug("Adapter: [" + getName() + "] STAT: Finished processing message with messageId [" + messageId + "] exit-state [" + result.getState() + "] started " + DateUtils.format(new Date(startTime), DateUtils.FORMAT_FULL_GENERIC) + " finished " + DateUtils.format(new Date(endTime), DateUtils.FORMAT_FULL_GENERIC) + " total duration: " + duration + " msecs");
        } else {
            log.info("Adapter [" + getName() + "] completed message with messageId [" + messageId + "] with exit-state [" + result.getState() + "]");
        }
        IbisMaskingLayout.removeThreadLocalReplace();
        if (ndcChanged) {
            ThreadContext.pop();
        }
        if (ThreadContext.getDepth() == 0) {
            ThreadContext.removeStack();
        }
    }
}
Also used : RunState(nl.nn.adapterframework.util.RunState) StringTokenizer(java.util.StringTokenizer) Message(nl.nn.adapterframework.stream.Message) Date(java.util.Date)

Example 2 with RunState

use of nl.nn.adapterframework.util.RunState in project iaf by ibissource.

the class Adapter method startRunning.

/**
 * Start the adapter. The thread-name will be set to the adapter's name.
 * The run method, called by t.start(), will call the startRunning method
 * of the IReceiver. The Adapter will be a new thread, as this interface
 * extends the <code>Runnable</code> interface. The actual starting is done
 * in the <code>run</code> method.
 * @see Receiver#startRunning()
 */
@Override
public void startRunning() {
    switch(getRunState()) {
        case STARTING:
        case EXCEPTION_STARTING:
        case STARTED:
        case STOPPING:
        case EXCEPTION_STOPPING:
            log.warn("cannot start adapter [" + getName() + "] that is stopping, starting or already started");
            return;
        default:
            break;
    }
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Thread.currentThread().setName("starting Adapter " + getName());
            try {
                // See also Receiver.startRunning()
                if (!configurationSucceeded) {
                    log.error("configuration of adapter [" + getName() + "] did not succeed, therefore starting the adapter is not possible");
                    warn("configuration did not succeed. Starting the adapter [" + getName() + "] is not possible");
                    runState.setRunState(RunState.ERROR);
                    return;
                }
                if (configuration.isUnloadInProgressOrDone()) {
                    log.error("configuration of adapter [" + getName() + "] unload in progress or done, therefore starting the adapter is not possible");
                    warn("configuration unload in progress or done. Starting the adapter [" + getName() + "] is not possible");
                    return;
                }
                synchronized (runState) {
                    RunState currentRunState = getRunState();
                    if (currentRunState != RunState.STOPPED) {
                        String msg = "currently in state [" + currentRunState + "], ignoring start() command";
                        warn(msg);
                        return;
                    }
                    runState.setRunState(RunState.STARTING);
                }
                // start the pipeline
                try {
                    log.debug("Adapter [" + getName() + "] is starting pipeline");
                    pipeline.start();
                } catch (PipeStartException pre) {
                    addErrorMessageToMessageKeeper("got error starting PipeLine", pre);
                    runState.setRunState(RunState.ERROR);
                    return;
                }
                // Update the adapter uptime.
                statsUpSince = System.currentTimeMillis();
                // as from version 3.0 the adapter is started,
                // regardless of receivers are correctly started.
                // this allows the use of test-pipeline without (running) receivers
                runState.setRunState(RunState.STARTED);
                getMessageKeeper().add("Adapter [" + getName() + "] up and running");
                log.info("Adapter [" + getName() + "] up and running");
                // starting receivers
                for (Receiver<?> receiver : receivers) {
                    receiver.startRunning();
                }
            } catch (Throwable t) {
                addErrorMessageToMessageKeeper("got error starting Adapter", t);
                runState.setRunState(RunState.ERROR);
            } finally {
                configuration.removeStartAdapterThread(this);
            }
        }

        @Override
        public String toString() {
            return getName();
        }
    };
    configuration.addStartAdapterThread(runnable);
    taskExecutor.execute(runnable);
}
Also used : RunState(nl.nn.adapterframework.util.RunState)

Example 3 with RunState

use of nl.nn.adapterframework.util.RunState in project iaf by ibissource.

the class Receiver method stopRunning.

// after successfully closing all resources the state should be set to stopped
@Override
public void stopRunning() {
    // See also Adapter.stopRunning() and PullingListenerContainer.ControllerTask
    synchronized (runState) {
        RunState currentRunState = getRunState();
        if (currentRunState == RunState.STARTING) {
            log.warn("receiver currently in state [" + currentRunState + "], ignoring stop() command");
            return;
        } else if (currentRunState == RunState.STOPPING || currentRunState == RunState.STOPPED) {
            log.info("receiver already in state [" + currentRunState + "]");
            return;
        }
        if (currentRunState == RunState.EXCEPTION_STARTING && getListener() instanceof IPullingListener) {
            // Nothing ever started, directly go to stopped
            runState.setRunState(RunState.STOPPING);
            closeAllResources();
            // Clean up receiver ThreadContext
            ThreadContext.removeStack();
            // Prevent tellResourcesToStop from being called
            return;
        }
        if (currentRunState != RunState.ERROR) {
            // Don't change the runstate when in ERROR
            runState.setRunState(RunState.STOPPING);
        }
    }
    tellResourcesToStop();
    // Clean up receiver ThreadContext
    ThreadContext.removeStack();
}
Also used : RunState(nl.nn.adapterframework.util.RunState) IPullingListener(nl.nn.adapterframework.core.IPullingListener)

Example 4 with RunState

use of nl.nn.adapterframework.util.RunState in project iaf by ibissource.

the class Receiver method startRunning.

@Override
public void startRunning() {
    try {
        // may only be executed when the adapter is started.
        if (adapter != null) {
            RunState adapterRunState = adapter.getRunState();
            if (adapterRunState != RunState.STARTED) {
                log.warn(getLogPrefix() + "on adapter [" + adapter.getName() + "] was tried to start, but the adapter is in state [" + adapterRunState + "]. Ignoring command.");
                adapter.getMessageKeeper().add("ignored start command on [" + getName() + "]; adapter is in state [" + adapterRunState + "]");
                return;
            }
        }
        // See also Adapter.startRunning()
        if (!configurationSucceeded) {
            log.error("configuration of receiver [" + getName() + "] did not succeed, therefore starting the receiver is not possible");
            warn("configuration did not succeed. Starting the receiver [" + getName() + "] is not possible");
            runState.setRunState(RunState.ERROR);
            return;
        }
        if (adapter.getConfiguration().isUnloadInProgressOrDone()) {
            log.error("configuration of receiver [" + getName() + "] unload in progress or done, therefore starting the receiver is not possible");
            warn("configuration unload in progress or done. Starting the receiver [" + getName() + "] is not possible");
            return;
        }
        synchronized (runState) {
            RunState currentRunState = getRunState();
            if (currentRunState != RunState.STOPPED && currentRunState != RunState.ERROR && configurationSucceeded()) {
                // stopped OR in error after configuring the receiver
                if (currentRunState == RunState.STARTING || currentRunState == RunState.STARTED) {
                    log.info("already in state [" + currentRunState + "]");
                } else {
                    log.warn("currently in state [" + currentRunState + "], ignoring start() command");
                }
                return;
            }
            runState.setRunState(RunState.STARTING);
        }
        openAllResources();
        // Don't log that it's ready before it's ready!?
        info("starts listening");
        runState.setRunState(RunState.STARTED);
        resetNumberOfExceptionsCaughtWithoutMessageBeingReceived();
    } catch (Throwable t) {
        error("error occured while starting", t);
        runState.setRunState(RunState.EXCEPTION_STARTING);
        // Close potential dangling resources, don't change state here..
        closeAllResources();
    }
}
Also used : RunState(nl.nn.adapterframework.util.RunState)

Example 5 with RunState

use of nl.nn.adapterframework.util.RunState in project iaf by ibissource.

the class RecoverAdaptersJob method execute.

@Override
public void execute(IbisManager ibisManager) {
    int countAdapter = 0;
    int countAdapterStateStarted = 0;
    int countReceiver = 0;
    int countReceiverStateStarted = 0;
    for (Adapter adapter : ibisManager.getRegisteredAdapters()) {
        countAdapter++;
        RunState adapterRunState = adapter.getRunState();
        boolean startAdapter = false;
        if (adapterRunState == RunState.ERROR) {
            // if not previously configured, there is no point in trying to do this again.
            log.debug("trying to recover adapter [" + adapter.getName() + "]");
            if (!adapter.configurationSucceeded()) {
                // This should only happen once, so only try to (re-)configure if it failed in the first place!
                try {
                    adapter.configure();
                } catch (ConfigurationException e) {
                    // log the warning and do nothing, it couldn't configure before, it still can't...
                    log.warn("error configuring adapter [" + adapter.getName() + "] while trying to recover", e);
                }
            }
            if (adapter.configurationSucceeded()) {
                // if configure has succeeded and adapter was in state ERROR try to auto (re-)start the adapter
                startAdapter = adapter.isAutoStart();
            }
            log.debug("finished recovering adapter [" + adapter.getName() + "]");
        }
        String message = "adapter [" + adapter.getName() + "] has state [" + adapterRunState + "]";
        if (adapterRunState == RunState.STARTED) {
            countAdapterStateStarted++;
            heartbeatLog.info(message);
        } else if (adapterRunState == RunState.ERROR) {
            heartbeatLog.error(message);
        } else {
            heartbeatLog.warn(message);
        }
        for (Receiver<?> receiver : adapter.getReceivers()) {
            countReceiver++;
            RunState receiverRunState = receiver.getRunState();
            if (adapterRunState == RunState.STARTED && receiverRunState == RunState.ERROR && receiver.configurationSucceeded()) {
                // Only try to (re-)start receivers in a running adapter. Receiver configure is done in Adapter.configure
                log.debug("trying to recover receiver [" + receiver.getName() + "] of adapter [" + adapter.getName() + "]");
                receiver.startRunning();
                log.debug("finished recovering receiver [" + receiver.getName() + "] of adapter [" + adapter.getName() + "]");
            }
            receiverRunState = receiver.getRunState();
            message = "receiver [" + receiver.getName() + "] of adapter [" + adapter.getName() + "] has state [" + receiverRunState + "]";
            if (receiverRunState == RunState.STARTED) {
                countReceiverStateStarted++;
                heartbeatLog.info(message);
            } else if (receiverRunState == RunState.ERROR) {
                heartbeatLog.error(message);
            } else {
                heartbeatLog.warn(message);
            }
        }
        if (startAdapter) {
            // can only be true if adapter was in error before and AutoStart is enabled
            // ASync startup can still cause the Adapter to end up in an ERROR state
            adapter.startRunning();
        }
    }
    heartbeatLog.info("[" + countAdapterStateStarted + "/" + countAdapter + "] adapters and [" + countReceiverStateStarted + "/" + countReceiver + "] receivers have state [" + RunState.STARTED + "]");
}
Also used : RunState(nl.nn.adapterframework.util.RunState) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Adapter(nl.nn.adapterframework.core.Adapter)

Aggregations

RunState (nl.nn.adapterframework.util.RunState)12 HashMap (java.util.HashMap)5 Configuration (nl.nn.adapterframework.configuration.Configuration)5 Adapter (nl.nn.adapterframework.core.Adapter)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 PermitAll (javax.annotation.security.PermitAll)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Status (javax.ws.rs.core.Response.Status)3 Date (java.util.Date)2 ListenerException (nl.nn.adapterframework.core.ListenerException)2 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1 TransformerException (javax.xml.transform.TransformerException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 IbisContext (nl.nn.adapterframework.configuration.IbisContext)1 HasPhysicalDestination (nl.nn.adapterframework.core.HasPhysicalDestination)1 HasSender (nl.nn.adapterframework.core.HasSender)1