Search in sources :

Example 1 with RunStateEnum

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

the class ShowConfigurationStatus method mapAdapter.

private Map<String, Object> mapAdapter(Adapter adapter) {
    Map<String, Object> adapterInfo = new HashMap<String, Object>();
    Configuration config = adapter.getConfiguration();
    String adapterName = adapter.getName();
    adapterInfo.put("name", adapterName);
    adapterInfo.put("description", adapter.getDescription());
    adapterInfo.put("configuration", config.getName());
    // replace low line (x'5f') by asterisk (x'2a) so it's sorted before any digit and letter
    String nameUC = StringUtils.upperCase(StringUtils.replace(adapterName, "_", "*"));
    adapterInfo.put("nameUC", nameUC);
    RunStateEnum adapterRunState = adapter.getRunState();
    adapterInfo.put("started", adapterRunState.equals(RunStateEnum.STARTED));
    String state = adapterRunState.toString().toLowerCase().replace("*", "");
    adapterInfo.put("state", state);
    adapterInfo.put("configured", adapter.configurationSucceeded());
    adapterInfo.put("upSince", adapter.getStatsUpSinceDate().getTime());
    Date lastMessage = adapter.getLastMessageDateDate();
    adapterInfo.put("lastMessage", (lastMessage == null) ? null : lastMessage.getTime());
    adapterInfo.put("messagesInProcess", adapter.getNumOfMessagesInProcess());
    adapterInfo.put("messagesProcessed", adapter.getNumOfMessagesProcessed());
    adapterInfo.put("messagesInError", adapter.getNumOfMessagesInError());
    return adapterInfo;
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RunStateEnum(nl.nn.adapterframework.util.RunStateEnum) Date(java.util.Date)

Example 2 with RunStateEnum

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

the class Adapter method stopRunning.

/**
 * Stop the <code>Adapter</code> and close all elements like receivers,
 * Pipeline, pipes etc.
 * The adapter
 * will call the <code>IReceiver</code> to <code>stopListening</code>
 * <p>Also the <code>PipeLine.close()</code> method will be called,
 * closing alle registered pipes. </p>
 * @see IReceiver#stopRunning
 * @see PipeLine#stop
 */
public void stopRunning() {
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Thread.currentThread().setName("stopping Adapter " + getName());
            try {
                // See also ReceiverBase.stopRunning()
                synchronized (runState) {
                    RunStateEnum currentRunState = getRunState();
                    if (currentRunState.equals(RunStateEnum.STARTING) || currentRunState.equals(RunStateEnum.STOPPING) || currentRunState.equals(RunStateEnum.STOPPED)) {
                        String msg = "currently in state [" + currentRunState + "], ignoring stop() command";
                        warn(msg);
                        return;
                    }
                    runState.setRunState(RunStateEnum.STOPPING);
                }
                log.debug("Adapter [" + name + "] is stopping receivers");
                Iterator<IReceiver> it = receivers.iterator();
                while (it.hasNext()) {
                    IReceiver receiver = it.next();
                    receiver.stopRunning();
                }
                // IPullingListeners might still be running, see also
                // comment in method ReceiverBase.tellResourcesToStop()
                it = receivers.iterator();
                while (it.hasNext()) {
                    IReceiver receiver = (IReceiver) it.next();
                    while (receiver.getRunState() != RunStateEnum.STOPPED) {
                        log.debug("Adapter [" + getName() + "] waiting for receiver [" + receiver.getName() + "] to stop");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            log.warn("Interrupted waiting for start threads to end", e);
                        }
                    }
                    log.info("Adapter [" + getName() + "] successfully stopped receiver [" + receiver.getName() + "]");
                }
                int currentNumOfMessagesInProcess = getNumOfMessagesInProcess();
                if (currentNumOfMessagesInProcess > 0) {
                    String msg = "Adapter [" + name + "] is being stopped while still processing " + currentNumOfMessagesInProcess + " messages, waiting for them to finish";
                    warn(msg);
                }
                waitForNoMessagesInProcess();
                log.debug("Adapter [" + name + "] is stopping pipeline");
                pipeline.stop();
                // Set the adapter uptime to 0 as the adapter is stopped.
                statsUpSince = 0;
                runState.setRunState(RunStateEnum.STOPPED);
                getMessageKeeper().add("Adapter stopped");
            } catch (Throwable t) {
                error(true, "got error stopping Adapter", t);
                runState.setRunState(RunStateEnum.ERROR);
            } finally {
                configuration.removeStopAdapterThread(this);
            }
        }

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

Example 3 with RunStateEnum

use of nl.nn.adapterframework.util.RunStateEnum 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 IReceiver#startRunning()
 * @see Adapter#run
 */
public void startRunning() {
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            Thread.currentThread().setName("starting Adapter " + getName());
            try {
                // See also ReceiverBase.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(RunStateEnum.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) {
                    RunStateEnum currentRunState = getRunState();
                    if (!currentRunState.equals(RunStateEnum.STOPPED)) {
                        String msg = "currently in state [" + currentRunState + "], ignoring start() command";
                        warn(msg);
                        return;
                    }
                    runState.setRunState(RunStateEnum.STARTING);
                }
                // start the pipeline
                try {
                    log.debug("Adapter [" + getName() + "] is starting pipeline");
                    pipeline.start();
                } catch (PipeStartException pre) {
                    error(true, "got error starting PipeLine", pre);
                    runState.setRunState(RunStateEnum.ERROR);
                    return;
                }
                // Update the adapter uptime.
                statsUpSince = System.currentTimeMillis();
                // as from version 3.0 the adapter is started,
                // regardless of receivers are correctly started.
                runState.setRunState(RunStateEnum.STARTED);
                getMessageKeeper().add("Adapter [" + getName() + "] up and running");
                log.info("Adapter [" + getName() + "] up and running");
                // starting receivers
                Iterator<IReceiver> it = receivers.iterator();
                while (it.hasNext()) {
                    IReceiver receiver = it.next();
                    receiver.startRunning();
                }
            } catch (Throwable t) {
                error(true, "got error starting Adapter", t);
                runState.setRunState(RunStateEnum.ERROR);
            } finally {
                configuration.removeStartAdapterThread(this);
            }
        }

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

Example 4 with RunStateEnum

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

the class Adapter method processMessageWithExceptions.

public PipeLineResult processMessageWithExceptions(String messageId, String message, IPipeLineSession pipeLineSession) throws ListenerException {
    PipeLineResult result = new PipeLineResult();
    long startTime = System.currentTimeMillis();
    // prevent executing a stopped adapter
    // the receivers should implement this, but you never now....
    RunStateEnum currentRunState = getRunState();
    if (!currentRunState.equals(RunStateEnum.STARTED) && !currentRunState.equals(RunStateEnum.STOPPING)) {
        String msgAdapterNotOpen = "Adapter [" + getName() + "] in state [" + currentRunState + "], cannot process message";
        throw new ListenerException(new ManagedStateException(msgAdapterNotOpen));
    }
    incNumOfMessagesInProcess(startTime);
    String lastNDC = NDC.peek();
    String newNDC = "cid [" + messageId + "]";
    boolean ndcChanged = !newNDC.equals(lastNDC);
    if (ndcChanged) {
        NDC.push(newNDC);
    }
    if (StringUtils.isNotEmpty(composedHideRegex)) {
        LogUtil.setThreadHideRegex(composedHideRegex);
    }
    // if (isRequestReplyLogging()) {
    StringBuilder additionalLogging = new StringBuilder();
    String xPathLogKeys = (String) pipeLineSession.get("xPathLogKeys");
    if (xPathLogKeys != null && 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 logMsg = "Adapter [" + name + "] received message [" + message + "] with messageId [" + messageId + "]" + additionalLogging;
    if (isMsgLogTerseEnabled()) {
        if (isMsgLogHidden()) {
            String logMessage = "Adapter [" + name + "] received message [SIZE=" + getFileSizeAsBytes(message) + "] with messageId [" + messageId + "]" + additionalLogging;
            msgLog.info(logMessage);
        } else {
            msgLog.info(logMsg);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(logMsg);
    } else {
        logMsg = "Adapter [" + name + "] received message with messageId [" + messageId + "]" + additionalLogging;
        log.info(logMsg);
    }
    try {
        if (message == null && isReplaceNullMessage()) {
            log.debug("Adapter [" + getName() + "] replaces null message with messageId [" + messageId + "] by empty message");
            message = "";
        }
        result = pipeline.process(messageId, message, pipeLineSession);
        String durationString = Misc.getAge(startTime);
        logMsg = "Adapter [" + getName() + "] messageId [" + messageId + "] duration [" + durationString + "] got exit-state [" + result.getState() + "] and result [" + result.toString() + "] from PipeLine";
        if (isMsgLogTerseEnabled()) {
            if (isMsgLogHidden()) {
                msgLog.info("Adapter [" + getName() + "] messageId [" + messageId + "] duration [" + durationString + "] got exit-state [" + result.getState() + "] and result [SIZE=" + getFileSizeAsBytes(result.toString()) + "] from PipeLine");
            } else {
                msgLog.info(logMsg);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug(logMsg);
        }
        return result;
    } catch (Throwable t) {
        ListenerException e;
        if (t instanceof ListenerException) {
            e = (ListenerException) t;
        } else {
            e = new ListenerException(t);
        }
        incNumOfMessagesInError();
        error(false, "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);
        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() + "]");
        }
        if (ndcChanged) {
            NDC.pop();
        }
        LogUtil.removeThreadHideRegex();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) RunStateEnum(nl.nn.adapterframework.util.RunStateEnum) Date(java.util.Date)

Example 5 with RunStateEnum

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

the class ReceiverBase method stopRunning.

public void stopRunning() {
    // See also Adapter.stopRunning()
    synchronized (runState) {
        RunStateEnum currentRunState = getRunState();
        if (currentRunState.equals(RunStateEnum.STARTING) || currentRunState.equals(RunStateEnum.STOPPING) || currentRunState.equals(RunStateEnum.STOPPED)) {
            String msg = "receiver currently in state [" + currentRunState + "], ignoring stop() command";
            warn(msg);
            return;
        }
        runState.setRunState(RunStateEnum.STOPPING);
    }
    tellResourcesToStop();
    NDC.remove();
}
Also used : RunStateEnum(nl.nn.adapterframework.util.RunStateEnum)

Aggregations

RunStateEnum (nl.nn.adapterframework.util.RunStateEnum)12 IReceiver (nl.nn.adapterframework.core.IReceiver)4 Date (java.util.Date)3 IAdapter (nl.nn.adapterframework.core.IAdapter)3 ReceiverBase (nl.nn.adapterframework.receivers.ReceiverBase)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Configuration (nl.nn.adapterframework.configuration.Configuration)2 IbisContext (nl.nn.adapterframework.configuration.IbisContext)2 Adapter (nl.nn.adapterframework.core.Adapter)2 HasPhysicalDestination (nl.nn.adapterframework.core.HasPhysicalDestination)2 HasSender (nl.nn.adapterframework.core.HasSender)2 IListener (nl.nn.adapterframework.core.IListener)2 ISender (nl.nn.adapterframework.core.ISender)2 IThreadCountControllable (nl.nn.adapterframework.core.IThreadCountControllable)2 ITransactionalStorage (nl.nn.adapterframework.core.ITransactionalStorage)2 EsbJmsListener (nl.nn.adapterframework.extensions.esb.EsbJmsListener)2 RestListener (nl.nn.adapterframework.http.RestListener)2 JmsListenerBase (nl.nn.adapterframework.jms.JmsListenerBase)2