Search in sources :

Example 6 with Statement

use of org.apache.synapse.message.store.impl.jdbc.util.Statement in project wso2-synapse by wso2.

the class JDBCMessageStore method removeMessageStatement.

/**
 * Statement to remove the message once a response is received.
 *
 * @param msgId message id of the statement which should be removed.
 * @return the sql remove message statement.
 */
protected List<Statement> removeMessageStatement(String msgId) {
    ArrayList<Statement> statements = new ArrayList<>();
    Statement statement = new Statement("DELETE FROM " + jdbcConfiguration.getTableName() + " WHERE msg_id=?") {

        @Override
        public List<Map> getResult(ResultSet resultSet) throws SQLException {
            throw new UnsupportedOperationException();
        }
    };
    statement.addParameter(msgId);
    statements.add(statement);
    return statements;
}
Also used : Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Statement

use of org.apache.synapse.message.store.impl.jdbc.util.Statement in project wso2-synapse by wso2.

the class JDBCMessageStore method peek.

/**
 * Select and return the first element in current table
 *
 * @return - Select and return the first element from the table
 */
public MessageContext peek() throws SynapseException {
    MessageContext msg;
    try {
        Statement statement = new Statement("SELECT message FROM " + jdbcConfiguration.getTableName() + " WHERE indexId=(SELECT min(indexId) from " + jdbcConfiguration.getTableName() + ")") {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                return messageContentResultSet(resultSet, this.getStatement());
            }
        };
        msg = getResultMessageContextFromDatabase(statement);
    } catch (SynapseException se) {
        throw new SynapseException("Error while peek the message", se);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with Statement

use of org.apache.synapse.message.store.impl.jdbc.util.Statement in project wso2-synapse by wso2.

the class JDBCMessageStore method getStoreMessageStatement.

/**
 * <p>
 * Generates the statement to store message in database.
 * </p>
 * <p>
 * If the sequence id is specified the corresponding sequence id will be stored, sequence id will be specified if
 * re-sequence message store is being used. In other times this value will be null.
 * </p>
 *
 * @param messageContext the content of the message.
 * @param sequenceId        the sequence id of the message (optional).
 * @return SQL statement for insertion of value to store.
 * @throws StoreException at an event there's an exception when generating the statement.
 * @see org.apache.synapse.message.store.impl.resequencer.ResequenceMessageStore
 */
protected Statement getStoreMessageStatement(MessageContext messageContext, Long sequenceId) throws StoreException {
    StorableMessage persistentMessage = MessageConverter.toStorableMessage(messageContext);
    String msgId = persistentMessage.getAxis2message().getMessageID();
    Statement statement;
    if (null == sequenceId) {
        String insertMessageStatement = "INSERT INTO " + jdbcConfiguration.getTableName() + " (msg_id,message) VALUES (?,?)";
        statement = new Statement(insertMessageStatement) {

            @Override
            public List<Map> getResult(ResultSet resultSet) {
                throw new UnsupportedOperationException();
            }
        };
        statement.addParameter(msgId);
        statement.addParameter(persistentMessage);
    } else {
        String insertMessageWithSequenceIdStatement = "INSERT INTO " + jdbcConfiguration.getTableName() + " (msg_id,seq_id,message) VALUES (?,?,?)";
        statement = new Statement(insertMessageWithSequenceIdStatement) {

            @Override
            public List<Map> getResult(ResultSet resultSet) {
                throw new UnsupportedOperationException();
            }
        };
        statement.addParameter(msgId);
        statement.addParameter(sequenceId);
        statement.addParameter(persistentMessage);
    }
    return statement;
}
Also used : StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with Statement

use of org.apache.synapse.message.store.impl.jdbc.util.Statement in project wso2-synapse by wso2.

the class ResequenceMessageStore method getNextMessage.

/**
 * Will get the next message belonging to a sequence.
 *
 * @return the next message in the sequence.
 */
private MessageContext getNextMessage() {
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        String tableName = getJdbcConfiguration().getTableName();
        String selectMessageStatement = "SELECT message FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "= ?";
        Statement statement = new Statement(selectMessageStatement) {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                return messageContentResultSet(resultSet, this.getStatement());
            }
        };
        statement.addParameter(nextSequenceId);
        List<Map> processedRows = getProcessedRows(statement);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            nextSequenceId++;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned for sequence " + nextSequenceId);
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Sequences not returned from DB, next sequence will be:" + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with Statement

use of org.apache.synapse.message.store.impl.jdbc.util.Statement in project wso2-synapse by wso2.

the class ResequenceMessageStore method getMessageWithMinimumSequence.

/**
 * <p>
 * Retrieve the minimum sequence number of the available sequence ids in the DB
 * </p>
 * <p>
 * This operation should call when there's a gap and a timeout occurs.
 * </p>
 * <p>
 * <b>Note : </b> This operation would reset the "nextSequenceId" to the minimum sequence id generated from the
 * DB.
 * </p>
 *
 * @return the message context of the next sequence
 */
private MessageContext getMessageWithMinimumSequence() {
    String tableName = getJdbcConfiguration().getTableName();
    String selectMinimumSequenceIdStatement = "SELECT message,seq_id FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "=(SELECT min(" + ResequenceMessageStoreConstants.SEQ_ID + ")" + " from " + tableName + ")";
    Statement stmt = new Statement(selectMinimumSequenceIdStatement) {

        @Override
        public List<Map> getResult(ResultSet resultSet) throws SQLException {
            return getMessageWithMinimumId(resultSet, this.getStatement());
        }
    };
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        List<Map> processedRows = getProcessedRows(stmt);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            long sequenceId = getSequenceId(processedRows, firstRowIndex);
            nextSequenceId = sequenceId + 1;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned as the minimum, the minimum " + "sequence " + "will be marked as " + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Statement (org.apache.synapse.message.store.impl.jdbc.util.Statement)16 ResultSet (java.sql.ResultSet)12 PreparedStatement (java.sql.PreparedStatement)11 HashMap (java.util.HashMap)11 Map (java.util.Map)11 SynapseException (org.apache.synapse.SynapseException)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 SQLException (java.sql.SQLException)5 MessageContext (org.apache.synapse.MessageContext)5 NoSuchElementException (java.util.NoSuchElementException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)3 Connection (java.sql.Connection)2 StorableMessage (org.apache.synapse.message.store.impl.commons.StorableMessage)2 List (java.util.List)1