Search in sources :

Example 11 with ITransactionalStorage

use of nl.nn.adapterframework.core.ITransactionalStorage in project iaf by ibissource.

the class ReceiverBase method moveInProcessToError.

private void moveInProcessToError(String originalMessageId, String correlationId, String message, Date receivedDate, String comments, Object rawMessage, TransactionDefinition txDef) {
    cachePoisonMessageId(originalMessageId);
    ISender errorSender = getErrorSender();
    ITransactionalStorage errorStorage = getErrorStorage();
    if (errorSender == null && errorStorage == null) {
        log.debug(getLogPrefix() + "has no errorSender or errorStorage, will not move message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to errorSender/errorStorage");
        return;
    }
    throwEvent(RCV_MESSAGE_TO_ERRORSTORE_EVENT);
    log.debug(getLogPrefix() + "moves message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to errorSender/errorStorage");
    TransactionStatus txStatus = null;
    try {
        txStatus = txManager.getTransaction(txDef);
    } catch (Exception e) {
        log.error(getLogPrefix() + "Exception preparing to move input message with id [" + originalMessageId + "] to error sender", e);
        // no use trying again to send message on errorSender, will cause same exception!
        return;
    }
    try {
        if (errorSender != null) {
            errorSender.sendMessage(correlationId, message);
        }
        Serializable sobj;
        if (rawMessage instanceof Serializable) {
            sobj = (Serializable) rawMessage;
        } else {
            try {
                sobj = new MessageWrapper(rawMessage, getListener());
            } catch (ListenerException e) {
                log.error(getLogPrefix() + "could not wrap non serializable message for messageId [" + originalMessageId + "]", e);
                sobj = message;
            }
        }
        if (hideRegex != null) {
            if (getHideMethod().equalsIgnoreCase("FIRSTHALF")) {
                message = Misc.hideFirstHalf(message, hideRegex);
            } else {
                message = Misc.hideAll(message, hideRegex);
            }
            sobj = message;
        }
        if (errorStorage != null) {
            errorStorage.storeMessage(originalMessageId, correlationId, receivedDate, comments, null, sobj);
        }
        txManager.commit(txStatus);
    } catch (Exception e) {
        log.error(getLogPrefix() + "Exception moving message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to error sender, original message: [" + message + "]", e);
        try {
            if (!txStatus.isCompleted()) {
                txManager.rollback(txStatus);
            }
        } catch (Exception rbe) {
            log.error(getLogPrefix() + "Exception while rolling back transaction for message  with id [" + originalMessageId + "] correlationId [" + correlationId + "], original message: [" + message + "]", rbe);
        }
    }
}
Also used : Serializable(java.io.Serializable) ListenerException(nl.nn.adapterframework.core.ListenerException) ISender(nl.nn.adapterframework.core.ISender) TransactionStatus(org.springframework.transaction.TransactionStatus) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage)

Example 12 with ITransactionalStorage

use of nl.nn.adapterframework.core.ITransactionalStorage in project iaf by ibissource.

the class JobDef method cleanupDatabase.

private void cleanupDatabase(IbisManager ibisManager) {
    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formattedDate = formatter.format(date);
    List<String> jmsRealmNames = new ArrayList<String>();
    for (Configuration configuration : ibisManager.getConfigurations()) {
        List<JobDef> scheduledJobs = configuration.getScheduledJobs();
        for (JobDef jobdef : configuration.getScheduledJobs()) {
            if (jobdef.getLocker() != null) {
                String jmsRealmName = jobdef.getLocker().getJmsRealName();
                if (!jmsRealmNames.contains(jmsRealmName)) {
                    jmsRealmNames.add(jmsRealmName);
                }
            }
        }
    }
    for (IAdapter adapter : ibisManager.getRegisteredAdapters()) {
        if (adapter instanceof Adapter) {
            PipeLine pipeLine = ((Adapter) adapter).getPipeLine();
            if (pipeLine != null) {
                for (IPipe pipe : pipeLine.getPipes()) {
                    if (pipe instanceof IExtendedPipe) {
                        IExtendedPipe extendedPipe = (IExtendedPipe) pipe;
                        if (extendedPipe.getLocker() != null) {
                            String jmsRealmName = extendedPipe.getLocker().getJmsRealName();
                            if (!jmsRealmNames.contains(jmsRealmName)) {
                                jmsRealmNames.add(jmsRealmName);
                            }
                        }
                    }
                }
            }
        }
    }
    for (Iterator iter = jmsRealmNames.iterator(); iter.hasNext(); ) {
        String jmsRealmName = (String) iter.next();
        setJmsRealm(jmsRealmName);
        DirectQuerySender qs;
        qs = (DirectQuerySender) ibisManager.getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
        qs.setJmsRealm(jmsRealmName);
        String deleteQuery;
        if (qs.getDatabaseType() == DbmsSupportFactory.DBMS_MSSQLSERVER) {
            deleteQuery = "DELETE FROM IBISLOCK WHERE EXPIRYDATE < CONVERT(datetime, '" + formattedDate + "', 120)";
        } else {
            deleteQuery = "DELETE FROM IBISLOCK WHERE EXPIRYDATE < TO_TIMESTAMP('" + formattedDate + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        setQuery(deleteQuery);
        qs = null;
        executeQueryJob(ibisManager);
    }
    List messageLogs = new ArrayList();
    for (IAdapter iadapter : ibisManager.getRegisteredAdapters()) {
        Adapter adapter = (Adapter) iadapter;
        PipeLine pipeline = adapter.getPipeLine();
        for (int i = 0; i < pipeline.getPipes().size(); i++) {
            IPipe pipe = pipeline.getPipe(i);
            if (pipe instanceof MessageSendingPipe) {
                MessageSendingPipe msp = (MessageSendingPipe) pipe;
                if (msp.getMessageLog() != null) {
                    ITransactionalStorage transactionStorage = msp.getMessageLog();
                    if (transactionStorage instanceof JdbcTransactionalStorage) {
                        JdbcTransactionalStorage messageLog = (JdbcTransactionalStorage) transactionStorage;
                        String jmsRealmName = messageLog.getJmsRealName();
                        String expiryDateField = messageLog.getExpiryDateField();
                        String tableName = messageLog.getTableName();
                        String keyField = messageLog.getKeyField();
                        String typeField = messageLog.getTypeField();
                        MessageLogObject mlo = new MessageLogObject(jmsRealmName, tableName, expiryDateField, keyField, typeField);
                        if (!messageLogs.contains(mlo)) {
                            messageLogs.add(mlo);
                        }
                    }
                }
            }
        }
    }
    for (Iterator iter = messageLogs.iterator(); iter.hasNext(); ) {
        MessageLogObject mlo = (MessageLogObject) iter.next();
        setJmsRealm(mlo.getJmsRealmName());
        DirectQuerySender qs;
        qs = (DirectQuerySender) ibisManager.getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
        qs.setJmsRealm(mlo.getJmsRealmName());
        String deleteQuery;
        if (qs.getDatabaseType() == DbmsSupportFactory.DBMS_MSSQLSERVER) {
            deleteQuery = "DELETE FROM " + mlo.getTableName() + " WHERE " + mlo.getKeyField() + " IN (SELECT " + mlo.getKeyField() + " FROM " + mlo.getTableName() + " WITH (rowlock,updlock,readpast) WHERE " + mlo.getTypeField() + " IN ('" + JdbcTransactionalStorage.TYPE_MESSAGELOG_PIPE + "','" + JdbcTransactionalStorage.TYPE_MESSAGELOG_RECEIVER + "') AND " + mlo.getExpiryDateField() + " < CONVERT(datetime, '" + formattedDate + "', 120))";
        } else {
            deleteQuery = "DELETE FROM " + mlo.getTableName() + " WHERE " + mlo.getTypeField() + " IN ('" + JdbcTransactionalStorage.TYPE_MESSAGELOG_PIPE + "','" + JdbcTransactionalStorage.TYPE_MESSAGELOG_RECEIVER + "') AND " + mlo.getExpiryDateField() + " < TO_TIMESTAMP('" + formattedDate + "', 'YYYY-MM-DD HH24:MI:SS')";
        }
        qs = null;
        setQuery(deleteQuery);
        setQueryTimeout(900);
        executeQueryJob(ibisManager);
    }
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) MessageSendingPipe(nl.nn.adapterframework.pipes.MessageSendingPipe) ArrayList(java.util.ArrayList) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) Adapter(nl.nn.adapterframework.core.Adapter) IAdapter(nl.nn.adapterframework.core.IAdapter) Date(java.util.Date) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage) JdbcTransactionalStorage(nl.nn.adapterframework.jdbc.JdbcTransactionalStorage) Iterator(java.util.Iterator) IExtendedPipe(nl.nn.adapterframework.core.IExtendedPipe) ArrayList(java.util.ArrayList) List(java.util.List) PipeLine(nl.nn.adapterframework.core.PipeLine) SimpleDateFormat(java.text.SimpleDateFormat) IAdapter(nl.nn.adapterframework.core.IAdapter) IPipe(nl.nn.adapterframework.core.IPipe)

Example 13 with ITransactionalStorage

use of nl.nn.adapterframework.core.ITransactionalStorage in project iaf by ibissource.

the class ReceiverBase method retryMessage.

public void retryMessage(String messageId) throws ListenerException {
    if (getErrorStorage() == null) {
        throw new ListenerException(getLogPrefix() + "has no errorStorage, cannot retry messageId [" + messageId + "]");
    }
    PlatformTransactionManager txManager = getTxManager();
    // TransactionStatus txStatus = txManager.getTransaction(TXNEW);
    IbisTransaction itx = new IbisTransaction(txManager, TXNEW_PROC, "receiver [" + getName() + "]");
    TransactionStatus txStatus = itx.getStatus();
    Map threadContext = new HashMap();
    Object msg = null;
    try {
        try {
            ITransactionalStorage errorStorage = getErrorStorage();
            msg = errorStorage.getMessage(messageId);
            processRawMessage(getListener(), msg, threadContext, -1, true);
        } catch (Throwable t) {
            txStatus.setRollbackOnly();
            throw new ListenerException(t);
        } finally {
            txManager.commit(txStatus);
        }
    } catch (ListenerException e) {
        txStatus = txManager.getTransaction(TXNEW_CTRL);
        try {
            if (msg instanceof Serializable) {
                String correlationId = (String) threadContext.get(IPipeLineSession.businessCorrelationIdKey);
                String receivedDateStr = (String) threadContext.get(IPipeLineSession.tsReceivedKey);
                if (receivedDateStr == null) {
                    log.warn(getLogPrefix() + IPipeLineSession.tsReceivedKey + " is unknown, cannot update comments");
                } else {
                    Date receivedDate = DateUtils.parseToDate(receivedDateStr, DateUtils.FORMAT_FULL_GENERIC);
                    errorStorage.deleteMessage(messageId);
                    errorStorage.storeMessage(messageId, correlationId, receivedDate, "after retry: " + e.getMessage(), null, (Serializable) msg);
                }
            } else {
                log.warn(getLogPrefix() + "retried message is not serializable, cannot update comments");
            }
        } catch (SenderException e1) {
            txStatus.setRollbackOnly();
            log.warn(getLogPrefix() + "could not update comments in errorStorage", e1);
        } finally {
            txManager.commit(txStatus);
        }
        throw e;
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Date(java.util.Date) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage) ListenerException(nl.nn.adapterframework.core.ListenerException) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) INamedObject(nl.nn.adapterframework.core.INamedObject) SenderException(nl.nn.adapterframework.core.SenderException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with ITransactionalStorage

use of nl.nn.adapterframework.core.ITransactionalStorage in project iaf by ibissource.

the class MessageSendingPipe method stop.

public void stop() {
    if (StringUtils.isEmpty(getStubFileName())) {
        log.info(getLogPrefix(null) + "is closing");
        try {
            getSender().close();
        } catch (SenderException e) {
            log.warn(getLogPrefix(null) + "exception closing sender", e);
        }
        if (getListener() != null) {
            try {
                log.info(getLogPrefix(null) + "is closing; closing listener");
                getListener().close();
            } catch (ListenerException e) {
                log.warn(getLogPrefix(null) + "Exception closing listener", e);
            }
        }
    }
    ITransactionalStorage messageLog = getMessageLog();
    if (messageLog != null) {
        try {
            messageLog.close();
        } catch (Exception e) {
            log.warn(getLogPrefix(null) + "Exception closing messageLog", e);
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) SenderException(nl.nn.adapterframework.core.SenderException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ParameterException(nl.nn.adapterframework.core.ParameterException) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage)

Example 15 with ITransactionalStorage

use of nl.nn.adapterframework.core.ITransactionalStorage in project iaf by ibissource.

the class ShowConfigurationStatus method mapAdapterReceivers.

private ArrayList<Object> mapAdapterReceivers(Adapter adapter, boolean showPendingMsgCount) {
    ArrayList<Object> receivers = new ArrayList<Object>();
    Iterator<?> recIt = adapter.getReceiverIterator();
    if (recIt.hasNext()) {
        while (recIt.hasNext()) {
            IReceiver receiver = (IReceiver) recIt.next();
            Map<String, Object> receiverInfo = new HashMap<String, Object>();
            RunStateEnum receiverRunState = receiver.getRunState();
            receiverInfo.put("started", receiverRunState.equals(RunStateEnum.STARTED));
            receiverInfo.put("state", receiverRunState.toString().toLowerCase().replace("*", ""));
            receiverInfo.put("name", receiver.getName());
            receiverInfo.put("class", ClassUtils.nameOf(receiver));
            Map<String, Object> messages = new HashMap<String, Object>(3);
            messages.put("received", receiver.getMessagesReceived());
            messages.put("retried", receiver.getMessagesRetried());
            messages.put("rejected", receiver.getMessagesRejected());
            receiverInfo.put("messages", messages);
            ISender sender = null;
            if (receiver instanceof ReceiverBase) {
                ReceiverBase rb = (ReceiverBase) receiver;
                IListener listener = rb.getListener();
                receiverInfo.put("listenerClass", ClassUtils.nameOf(listener));
                if (listener instanceof HasPhysicalDestination) {
                    String pd = ((HasPhysicalDestination) rb.getListener()).getPhysicalDestinationName();
                    receiverInfo.put("listenerDestination", pd);
                }
                if (listener instanceof HasSender) {
                    sender = ((HasSender) listener).getSender();
                }
                // receiverInfo.put("hasInprocessStorage", ""+(rb.getInProcessStorage()!=null));
                ITransactionalStorage ts;
                ts = rb.getErrorStorage();
                receiverInfo.put("hasErrorStorage", (ts != null));
                if (ts != null) {
                    try {
                        if (showCountErrorStore) {
                            receiverInfo.put("errorStorageCount", ts.getMessageCount());
                        } else {
                            receiverInfo.put("errorStorageCount", "?");
                        }
                    } catch (Exception e) {
                        log.warn(e);
                        receiverInfo.put("errorStorageCount", "error");
                    }
                }
                ts = rb.getMessageLog();
                receiverInfo.put("hasMessageLog", (ts != null));
                if (ts != null) {
                    try {
                        if (showCountMessageLog) {
                            receiverInfo.put("messageLogCount", ts.getMessageCount());
                        } else {
                            receiverInfo.put("messageLogCount", "?");
                        }
                    } catch (Exception e) {
                        log.warn(e);
                        receiverInfo.put("messageLogCount", "error");
                    }
                }
                boolean isRestListener = (listener instanceof RestListener);
                receiverInfo.put("isRestListener", isRestListener);
                if (isRestListener) {
                    RestListener rl = (RestListener) listener;
                    receiverInfo.put("restUriPattern", rl.getRestUriPattern());
                    receiverInfo.put("isView", (rl.isView() == null ? false : rl.isView()));
                }
                if ((listener instanceof JmsListenerBase) && showPendingMsgCount) {
                    JmsListenerBase jlb = (JmsListenerBase) listener;
                    JmsMessageBrowser jmsBrowser;
                    if (StringUtils.isEmpty(jlb.getMessageSelector())) {
                        jmsBrowser = new JmsMessageBrowser();
                    } else {
                        jmsBrowser = new JmsMessageBrowser(jlb.getMessageSelector());
                    }
                    jmsBrowser.setName("MessageBrowser_" + jlb.getName());
                    jmsBrowser.setJmsRealm(jlb.getJmsRealName());
                    jmsBrowser.setDestinationName(jlb.getDestinationName());
                    jmsBrowser.setDestinationType(jlb.getDestinationType());
                    String numMsgs;
                    try {
                        int messageCount = jmsBrowser.getMessageCount();
                        numMsgs = String.valueOf(messageCount);
                    } catch (Throwable t) {
                        log.warn(t);
                        numMsgs = "?";
                    }
                    receiverInfo.put("pendingMessagesCount", numMsgs);
                }
                boolean isEsbJmsFFListener = false;
                if (listener instanceof EsbJmsListener) {
                    EsbJmsListener ejl = (EsbJmsListener) listener;
                    if (ejl.getMessageProtocol().equalsIgnoreCase("FF")) {
                        isEsbJmsFFListener = true;
                    }
                    if (showPendingMsgCount) {
                        String esbNumMsgs = EsbUtils.getQueueMessageCount(ejl);
                        if (esbNumMsgs == null) {
                            esbNumMsgs = "?";
                        }
                        receiverInfo.put("esbPendingMessagesCount", esbNumMsgs);
                    }
                }
                receiverInfo.put("isEsbJmsFFListener", isEsbJmsFFListener);
            }
            if (receiver instanceof HasSender) {
                ISender rsender = ((HasSender) receiver).getSender();
                if (rsender != null) {
                    // this sender has preference, but avoid overwriting listeners sender with null
                    sender = rsender;
                }
            }
            if (sender != null) {
                receiverInfo.put("senderName", sender.getName());
                receiverInfo.put("senderClass", ClassUtils.nameOf(sender));
                if (sender instanceof HasPhysicalDestination) {
                    String pd = ((HasPhysicalDestination) sender).getPhysicalDestinationName();
                    receiverInfo.put("senderDestination", pd);
                }
            }
            if (receiver instanceof IThreadCountControllable) {
                IThreadCountControllable tcc = (IThreadCountControllable) receiver;
                if (tcc.isThreadCountReadable()) {
                    receiverInfo.put("threadCount", tcc.getCurrentThreadCount());
                    receiverInfo.put("maxThreadCount", tcc.getMaxThreadCount());
                }
                if (tcc.isThreadCountControllable()) {
                    receiverInfo.put("threadCountControllable", "true");
                }
            }
            receivers.add(receiverInfo);
        }
    }
    return receivers;
}
Also used : ReceiverBase(nl.nn.adapterframework.receivers.ReceiverBase) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IListener(nl.nn.adapterframework.core.IListener) ArrayList(java.util.ArrayList) EsbJmsListener(nl.nn.adapterframework.extensions.esb.EsbJmsListener) HasSender(nl.nn.adapterframework.core.HasSender) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage) RestListener(nl.nn.adapterframework.http.RestListener) JmsListenerBase(nl.nn.adapterframework.jms.JmsListenerBase) IReceiver(nl.nn.adapterframework.core.IReceiver) JmsMessageBrowser(nl.nn.adapterframework.jms.JmsMessageBrowser) RunStateEnum(nl.nn.adapterframework.util.RunStateEnum) ISender(nl.nn.adapterframework.core.ISender) IThreadCountControllable(nl.nn.adapterframework.core.IThreadCountControllable) HasPhysicalDestination(nl.nn.adapterframework.core.HasPhysicalDestination)

Aggregations

ITransactionalStorage (nl.nn.adapterframework.core.ITransactionalStorage)17 ISender (nl.nn.adapterframework.core.ISender)8 IPipe (nl.nn.adapterframework.core.IPipe)7 ReceiverBase (nl.nn.adapterframework.receivers.ReceiverBase)7 HashMap (java.util.HashMap)6 HasPhysicalDestination (nl.nn.adapterframework.core.HasPhysicalDestination)6 IListener (nl.nn.adapterframework.core.IListener)6 LinkedHashMap (java.util.LinkedHashMap)5 IAdapter (nl.nn.adapterframework.core.IAdapter)5 PipeRunException (nl.nn.adapterframework.core.PipeRunException)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)4 Adapter (nl.nn.adapterframework.core.Adapter)4 PipeLine (nl.nn.adapterframework.core.PipeLine)4 SenderException (nl.nn.adapterframework.core.SenderException)4 MessageSendingPipe (nl.nn.adapterframework.pipes.MessageSendingPipe)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Configuration (nl.nn.adapterframework.configuration.Configuration)3