Search in sources :

Example 1 with MessageWrapper

use of nl.nn.adapterframework.receivers.MessageWrapper in project iaf by ibissource.

the class JdbcListener method getRawMessage.

protected Object getRawMessage(Connection conn, Map threadContext) throws ListenerException {
    boolean inTransaction = false;
    try {
        inTransaction = JtaUtil.inTransaction();
    } catch (Exception e) {
        log.warn(getLogPrefix() + "could not determing XA transaction status, assuming not in XA transaction: " + e.getMessage());
        inTransaction = false;
    }
    try {
        if (!inTransaction) {
            execute(conn, getStartLocalTransactionQuery());
        }
        String query = preparedSelectQuery;
        try {
            Statement stmt = null;
            try {
                stmt = conn.createStatement();
                stmt.setFetchSize(1);
                ResultSet rs = null;
                try {
                    if (trace && log.isDebugEnabled())
                        log.debug("executing query for [" + query + "]");
                    rs = stmt.executeQuery(query);
                    if (!rs.next()) {
                        return null;
                    }
                    Object result;
                    String key = rs.getString(getKeyField());
                    if (StringUtils.isNotEmpty(getMessageField())) {
                        String message;
                        if ("clob".equalsIgnoreCase(getMessageFieldType())) {
                            message = JdbcUtil.getClobAsString(rs, getMessageField(), false);
                        } else {
                            if ("blob".equalsIgnoreCase(getMessageFieldType())) {
                                message = JdbcUtil.getBlobAsString(rs, getMessageField(), getBlobCharset(), false, isBlobsCompressed(), isBlobSmartGet(), false);
                            } else {
                                message = rs.getString(getMessageField());
                            }
                        }
                        // log.debug("building wrapper for key ["+key+"], message ["+message+"]");
                        MessageWrapper mw = new MessageWrapper();
                        mw.setId(key);
                        mw.setText(message);
                        result = mw;
                    } else {
                        result = key;
                    }
                    return result;
                } finally {
                    if (rs != null) {
                        rs.close();
                    }
                }
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
        } catch (Exception e) {
            throw new ListenerException(getLogPrefix() + "caught exception retrieving message using query [" + query + "]", e);
        }
    } finally {
        if (!inTransaction) {
            execute(conn, getCommitLocalTransactionQuery());
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) IMessageWrapper(nl.nn.adapterframework.core.IMessageWrapper) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SQLException(java.sql.SQLException) ListenerException(nl.nn.adapterframework.core.ListenerException)

Example 2 with MessageWrapper

use of nl.nn.adapterframework.receivers.MessageWrapper in project iaf by ibissource.

the class MessageStoreListener method getRawMessage.

@Override
public Object getRawMessage(Map threadContext) throws ListenerException {
    Object rawMessage = super.getRawMessage(threadContext);
    if (rawMessage != null && sessionKeys != null) {
        MessageWrapper messageWrapper = (MessageWrapper) rawMessage;
        StrTokenizer strTokenizer = StrTokenizer.getCSVInstance().reset(messageWrapper.getText());
        messageWrapper.setText((String) strTokenizer.next());
        int i = 0;
        while (strTokenizer.hasNext()) {
            threadContext.put(sessionKeysList.get(i), strTokenizer.next());
            i++;
        }
    }
    return rawMessage;
}
Also used : MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) StrTokenizer(org.apache.commons.lang.text.StrTokenizer)

Example 3 with MessageWrapper

use of nl.nn.adapterframework.receivers.MessageWrapper in project iaf by ibissource.

the class PullingIfsaProviderListener method getRawMessage.

/**
 * Retrieves messages to be processed by the server, implementing an IFSA-service, but does no processing on it.
 */
public Object getRawMessage(Map threadContext) throws ListenerException {
    Object result = null;
    QueueSession session = null;
    QueueReceiver receiver = null;
    threadContext.remove(THREAD_CONTEXT_ORIGINAL_RAW_MESSAGE_KEY);
    try {
        session = getSession(threadContext);
        try {
            receiver = getReceiver(threadContext, session);
            result = receiver.receive(getTimeOut());
            while (result == null && canGoOn() && !JtaUtil.inTransaction()) {
                result = receiver.receive(getTimeOut());
            }
        } catch (Exception e) {
            throw new ListenerException(getLogPrefix(), e);
        } finally {
            releaseReceiver(receiver);
        }
    } finally {
        if (sessionNeedsToBeSavedForAfterProcessMessage(result)) {
            threadContext.put(THREAD_CONTEXT_SESSION_KEY, session);
        } else {
            releaseSession(session);
        }
    }
    if (result instanceof IFSAPoisonMessage) {
        IFSAHeader header = ((IFSAPoisonMessage) result).getIFSAHeader();
        String source;
        try {
            source = header.getIFSA_Source();
        } catch (Exception e) {
            source = "unknown due to exeption:" + e.getMessage();
        }
        String msg = getLogPrefix() + "received IFSAPoisonMessage " + "source [" + source + "]" + "content [" + ToStringBuilder.reflectionToString((IFSAPoisonMessage) result) + "]";
        log.warn(msg);
    }
    try {
        if ((result instanceof IFSATextMessage || result instanceof IFSAPoisonMessage) && JtaUtil.inTransaction()) {
            threadContext.put(THREAD_CONTEXT_ORIGINAL_RAW_MESSAGE_KEY, result);
            result = new MessageWrapper(result, this);
        }
    } catch (Exception e) {
        throw new ListenerException("cannot wrap non serialzable message in wrapper", e);
    }
    return result;
}
Also used : IFSAHeader(com.ing.ifsa.IFSAHeader) ListenerException(nl.nn.adapterframework.core.ListenerException) QueueReceiver(javax.jms.QueueReceiver) INamedObject(nl.nn.adapterframework.core.INamedObject) MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) IMessageWrapper(nl.nn.adapterframework.core.IMessageWrapper) QueueSession(javax.jms.QueueSession) IfsaException(nl.nn.adapterframework.extensions.ifsa.IfsaException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) JMSException(javax.jms.JMSException) IFSAPoisonMessage(com.ing.ifsa.IFSAPoisonMessage) IFSATextMessage(com.ing.ifsa.IFSATextMessage)

Example 4 with MessageWrapper

use of nl.nn.adapterframework.receivers.MessageWrapper in project iaf by ibissource.

the class EsbUtils method receiveMessageAndMoveToErrorStorage.

public static String receiveMessageAndMoveToErrorStorage(EsbJmsListener esbJmsListener, JdbcTransactionalStorage errorStorage) {
    String result = null;
    PoolingConnectionFactory jmsConnectionFactory = null;
    PoolingDataSource jdbcDataSource = null;
    BitronixTransactionManager btm = null;
    javax.jms.Connection jmsConnection = null;
    try {
        jmsConnectionFactory = getPoolingConnectionFactory(esbJmsListener);
        if (jmsConnectionFactory != null) {
            jdbcDataSource = getPoolingDataSource(errorStorage);
            if (jdbcDataSource != null) {
                String instanceNameLc = AppConstants.getInstance().getString("instance.name.lc", null);
                String logDir = AppConstants.getInstance().getString("log.dir", null);
                TransactionManagerServices.getConfiguration().setServerId(instanceNameLc + ".tm");
                TransactionManagerServices.getConfiguration().setLogPart1Filename(logDir + File.separator + instanceNameLc + "-btm1.tlog");
                TransactionManagerServices.getConfiguration().setLogPart2Filename(logDir + File.separator + instanceNameLc + "-btm2.tlog");
                btm = TransactionManagerServices.getTransactionManager();
                jmsConnection = jmsConnectionFactory.createConnection();
                Session jmsSession = null;
                MessageConsumer jmsConsumer = null;
                java.sql.Connection jdbcConnection = null;
                btm.begin();
                log.debug("started transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                try {
                    jmsSession = jmsConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
                    String queueName = esbJmsListener.getPhysicalDestinationShortName();
                    Queue queue = jmsSession.createQueue(queueName);
                    jmsConsumer = jmsSession.createConsumer(queue);
                    jmsConnection.start();
                    long timeout = 30000;
                    log.debug("looking for message on queue [" + queueName + "] with timeout of [" + timeout + "] msec");
                    Message rawMessage = jmsConsumer.receive(timeout);
                    if (rawMessage == null) {
                        log.debug("no message found on queue [" + queueName + "]");
                    } else {
                        String id = rawMessage.getJMSMessageID();
                        log.debug("found message on queue [" + queueName + "] with messageID [" + id + "]");
                        Serializable sobj = null;
                        if (rawMessage != null) {
                            if (rawMessage instanceof Serializable) {
                                sobj = (Serializable) rawMessage;
                            } else {
                                try {
                                    sobj = new MessageWrapper(rawMessage, esbJmsListener);
                                } catch (ListenerException e) {
                                    log.error("could not wrap non serializable message for messageId [" + id + "]", e);
                                    if (rawMessage instanceof TextMessage) {
                                        TextMessage textMessage = (TextMessage) rawMessage;
                                        sobj = textMessage.getText();
                                    } else {
                                        sobj = rawMessage.toString();
                                    }
                                }
                            }
                        }
                        jdbcConnection = jdbcDataSource.getConnection();
                        result = errorStorage.storeMessage(jdbcConnection, id, id, new Date(System.currentTimeMillis()), "moved message", null, sobj);
                    }
                    log.debug("committing transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                    btm.commit();
                } catch (Exception e) {
                    if (btm.getCurrentTransaction() != null) {
                        log.debug("rolling back transaction [" + btm.getCurrentTransaction().getGtrid() + "]");
                        btm.rollback();
                    }
                    log.error("exception on receiving message and moving to errorStorage", e);
                } finally {
                    if (jdbcConnection != null) {
                        jdbcConnection.close();
                    }
                    if (jmsConnection != null) {
                        jmsConnection.stop();
                    }
                    if (jmsConsumer != null) {
                        jmsConsumer.close();
                    }
                    if (jmsSession != null) {
                        jmsSession.close();
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("exception on receiving message and moving to errorStorage", e);
    } finally {
        if (jmsConnection != null) {
            try {
                jmsConnection.close();
            } catch (JMSException e) {
                log.warn("exception on closing connection", e);
            }
        }
        if (jmsConnectionFactory != null) {
            jmsConnectionFactory.close();
        }
        if (jdbcDataSource != null) {
            jdbcDataSource.close();
        }
        if (btm != null) {
            btm.shutdown();
        }
    }
    return result;
}
Also used : PoolingDataSource(bitronix.tm.resource.jdbc.PoolingDataSource) MessageConsumer(javax.jms.MessageConsumer) Serializable(java.io.Serializable) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) BitronixTransactionManager(bitronix.tm.BitronixTransactionManager) Date(java.util.Date) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) ListenerException(nl.nn.adapterframework.core.ListenerException) JmsException(nl.nn.adapterframework.jms.JmsException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) PoolingConnectionFactory(bitronix.tm.resource.jms.PoolingConnectionFactory) ListenerException(nl.nn.adapterframework.core.ListenerException) MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

MessageWrapper (nl.nn.adapterframework.receivers.MessageWrapper)4 ListenerException (nl.nn.adapterframework.core.ListenerException)3 SQLException (java.sql.SQLException)2 JMSException (javax.jms.JMSException)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 IMessageWrapper (nl.nn.adapterframework.core.IMessageWrapper)2 BitronixTransactionManager (bitronix.tm.BitronixTransactionManager)1 PoolingDataSource (bitronix.tm.resource.jdbc.PoolingDataSource)1 PoolingConnectionFactory (bitronix.tm.resource.jms.PoolingConnectionFactory)1 IFSAHeader (com.ing.ifsa.IFSAHeader)1 IFSAPoisonMessage (com.ing.ifsa.IFSAPoisonMessage)1 IFSATextMessage (com.ing.ifsa.IFSATextMessage)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Date (java.util.Date)1 Message (javax.jms.Message)1 MessageConsumer (javax.jms.MessageConsumer)1