Search in sources :

Example 1 with Statement

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

the class JDBCMessageStore method remove.

/**
 * Remove the message with given msg_id
 *
 * @param msgId - message ID
 * @return - removed message context
 */
@Override
public MessageContext remove(String msgId) throws SynapseException {
    MessageContext result;
    boolean cleaningState = false;
    try {
        if (cleaningFlag.get()) {
            try {
                removeLock.lock();
                cleaningState = true;
            } catch (Exception ie) {
                logger.error("Message Cleanup lock released unexpectedly", ie);
            }
        }
        result = get(msgId);
        List<Statement> statements = removeMessageStatement(msgId);
        processNonResultingStatement(statements);
    } catch (Exception e) {
        throw new SynapseException("Removing message with id = " + msgId + " failed !", e);
    } finally {
        if (cleaningState) {
            removeLock.unlock();
        }
    }
    return result;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

Example 2 with Statement

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

the class JDBCMessageStore method get.

/**
 * Get the message at given position
 * Only can be done with MYSQL, and no use-case in current implementation
 *
 * @param position - position of the message , starting value is 0
 * @return Message Context of position th row or if failed return null
 */
@Override
public MessageContext get(int position) {
    if (position < 0) {
        throw new IllegalArgumentException("Index:" + position + " out of table bound");
    }
    // Gets the minimum value of the sub-table which contains indexId values greater than given position
    // ('position' has minimum of 0 while indexId has minimum of 1)
    Statement statement = new Statement("SELECT message FROM " + jdbcConfiguration.getTableName() + " ORDER BY indexId ASC LIMIT ?,1 ") {

        @Override
        public List<Map> getResult(ResultSet resultSet) throws SQLException {
            return messageContentResultSet(resultSet, this.getStatement());
        }
    };
    statement.addParameter(position);
    return getResultMessageContextFromDatabase(statement);
}
Also used : Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Statement

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

the class JDBCMessageStore method clear.

/**
 * Delete all entries from table
 */
@Override
public void clear() {
    try {
        logger.warn(getNameString() + "deleting all entries");
        removeLock.lock();
        cleanUpOfferLock.lock();
        cleaningFlag.set(true);
        Statement statement = new Statement("DELETE FROM " + jdbcConfiguration.getTableName()) {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                throw new UnsupportedOperationException();
            }
        };
        List<Statement> statements = new ArrayList<>();
        statements.add(statement);
        processNonResultingStatement(statements);
    } catch (Exception e) {
        logger.error("Clearing store failed !", e);
    } finally {
        cleaningFlag.set(false);
        removeLock.unlock();
        cleanUpOfferLock.unlock();
    }
}
Also used : Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

Example 4 with Statement

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

the class JDBCMessageStore method processNonResultingStatement.

/**
 * Process statements that do not give a ResultSet
 *
 * @param statements - Statement to process
 * @return - Success or Failure of the process
 */
private boolean processNonResultingStatement(List<Statement> statements) throws SynapseException {
    Connection connection = null;
    boolean result;
    PreparedStatement preparedStatement = null;
    try {
        connection = jdbcConfiguration.getConnection();
        connection.setAutoCommit(false);
        for (Statement statement : statements) {
            preparedStatement = connection.prepareStatement(statement.getStatement());
            int index = 1;
            for (Object param : statement.getParameters()) {
                if (param instanceof String) {
                    preparedStatement.setString(index, (String) param);
                } else if (param instanceof Long) {
                    preparedStatement.setLong(index, (Long) param);
                } else if (param instanceof StorableMessage) {
                    // Serialize the object into byteArray and update the statement
                    preparedStatement.setBytes(index, serialize(param));
                }
                index++;
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Executing statement:" + preparedStatement);
            }
            preparedStatement.execute();
        }
        connection.commit();
        result = true;
    } catch (SQLException | IOException e) {
        rollback(connection, "deleting message");
        throw new SynapseException("Processing Statement failed against DataSource : " + jdbcConfiguration.getDSName(), e);
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                logger.error("Error while closing prepared statement", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error("Error while closing connection", e);
            }
        }
    }
    return result;
}
Also used : SynapseException(org.apache.synapse.SynapseException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) SQLException(java.sql.SQLException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 5 with Statement

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

the class JDBCMessageStore method store.

/**
 * Add a message to the end of the table. If fetching success return true else false
 *
 * @param messageContext message to insert
 * @return -  success/failure of fetching
 */
public boolean store(MessageContext messageContext) throws SynapseException {
    if (messageContext == null) {
        logger.error("Message is null, can't store into database");
        return false;
    }
    boolean cleaningState = false;
    try {
        if (cleaningFlag.get()) {
            try {
                cleanUpOfferLock.lock();
                cleaningState = true;
            } catch (Exception e) {
                logger.error("Message Cleanup lock released unexpectedly", e);
            }
        }
        ArrayList<Statement> statements = new ArrayList<>();
        Statement statement = getStoreMessageStatement(messageContext, null);
        statements.add(statement);
        return processNonResultingStatement(statements);
    } catch (Exception e) {
        throw new SynapseException("Error while creating StorableMessage", e);
    } finally {
        if (cleaningState) {
            cleanUpOfferLock.unlock();
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

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