use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class JDBCMessageStore method getProcessedRows.
/**
* Will return the list of processed message rows.
*
* @param statement the statement executed in the DB.
* @return the rows which contains the column data wrapped inside a map.
*/
protected List<Map> getProcessedRows(Statement statement) {
Connection con = null;
ResultSet rs = null;
PreparedStatement ps = null;
List<Map> elements;
try {
con = jdbcConfiguration.getConnection();
ps = con.prepareStatement(statement.getStatement());
int index = 1;
for (Object param : statement.getParameters()) {
if (param instanceof String) {
ps.setString(index, (String) param);
} else if (param instanceof Long) {
ps.setLong(index, (Long) param);
} else if (param instanceof Integer) {
ps.setInt(index, (Integer) param);
}
index++;
}
rs = ps.executeQuery();
elements = statement.getResult(rs);
} catch (SQLException e) {
throw new SynapseException("Processing Statement failed : " + statement.getStatement() + " against DataSource : " + jdbcConfiguration.getDSName(), e);
} finally {
close(con, ps, rs);
}
return elements;
}
use of org.apache.synapse.SynapseException 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.SynapseException 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();
}
}
}
use of org.apache.synapse.SynapseException 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;
}
use of org.apache.synapse.SynapseException in project wso2-synapse by wso2.
the class JDBCMessageStore method deserializeMessage.
/**
* Will convert the byte[] message to store-able message.
*
* @param msgObj serialized message read from the database.
* @return converted message context.
*/
protected MessageContext deserializeMessage(byte[] msgObj) {
MessageContext messageContext = null;
if (msgObj != null) {
ObjectInputStream ios = null;
try {
// Convert back to MessageContext and add to list
ios = new ObjectInputStream(new ByteArrayInputStream(msgObj));
Object msg = ios.readObject();
if (msg instanceof StorableMessage) {
StorableMessage jdbcMsg = (StorableMessage) msg;
org.apache.axis2.context.MessageContext axis2Mc = this.newAxis2Mc();
MessageContext synapseMc = this.newSynapseMc(axis2Mc);
messageContext = MessageConverter.toMessageContext(jdbcMsg, axis2Mc, synapseMc);
}
} catch (IOException e) {
throw new SynapseException("Error reading object input stream", e);
} catch (ClassNotFoundException e) {
throw new SynapseException("Could not find the class", e);
} finally {
closeStream(ios);
}
} else {
throw new SynapseException("Retrieved Object is null");
}
return messageContext;
}
Aggregations