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;
}
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);
}
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();
}
}
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;
}
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();
}
}
}
Aggregations