use of org.apache.synapse.message.store.impl.commons.StorableMessage in project wso2-synapse by wso2.
the class JmsConsumer method receive.
public MessageContext receive() {
boolean connectionSuccess = checkAndTryConnect();
if (!connectionSuccess) {
throw new SynapseException(idString + "Error while connecting to JMS provider. " + MessageProcessorConstants.STORE_CONNECTION_ERROR);
}
try {
Message message = consumer.receive(1);
if (message == null) {
return null;
}
if (!(message instanceof ObjectMessage)) {
logger.warn("JMS Consumer " + getId() + " did not receive a javax.jms.ObjectMessage");
// we just discard this message as we only store Object messages via JMS Message store
message.acknowledge();
return null;
}
ObjectMessage msg = (ObjectMessage) message;
String messageId = msg.getStringProperty(Constants.OriginalMessageID);
if (!(msg.getObject() instanceof StorableMessage)) {
logger.warn("JMS Consumer " + getId() + " did not receive a valid message.");
message.acknowledge();
return null;
}
// create a ,essage context back from the stored message
StorableMessage storableMessage = (StorableMessage) msg.getObject();
org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
MessageContext synapseMc = store.newSynapseMc(axis2Mc);
synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
// cache the message
updateCache(message, synapseMc, messageId, false);
if (logger.isDebugEnabled()) {
logger.debug(getId() + " Received MessageId:" + messageId + " priority:" + message.getJMSPriority());
}
return synapseMc;
} catch (JMSException e) {
logger.error("Cannot fetch messages from Store " + store.getName());
updateCache(null, null, "", true);
cleanup();
/* try connecting and receiving again. Try to connect will happen configured number of times
and give up with a SynapseException */
return receive();
}
}
use of org.apache.synapse.message.store.impl.commons.StorableMessage in project wso2-synapse by wso2.
the class JmsProducer method storeMessage.
public boolean storeMessage(MessageContext synCtx) {
if (synCtx == null) {
return false;
}
if (!checkConnection()) {
logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
store.setCachedProducer(null);
return false;
}
StorableMessage message = MessageConverter.toStorableMessage(synCtx);
boolean error = false;
Throwable throwable = null;
try {
ObjectMessage objectMessage = session.createObjectMessage(message);
objectMessage.setStringProperty(OriginalMessageID, synCtx.getMessageID());
setPriority(producer, objectMessage, message);
setJmsProducerProperties(producer, synCtx);
setJmsMessageProperties(objectMessage, synCtx);
setTransportHeaders(objectMessage, synCtx);
producer.send(objectMessage);
if (session.getTransacted()) {
session.commit();
}
} catch (JMSException e) {
throwable = e;
error = true;
isConnectionError = true;
try {
if (session.getTransacted()) {
session.rollback();
}
} catch (JMSException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Fail to rollback message [" + synCtx.getMessageID() + "] from the message store " + ":" + store.getName(), ex);
}
}
} catch (Throwable t) {
throwable = t;
error = true;
try {
if (session.getTransacted()) {
session.rollback();
}
} catch (JMSException e) {
if (logger.isDebugEnabled()) {
logger.debug("Fail to rollback message [" + synCtx.getMessageID() + "] from the message store " + ":" + store.getName(), e);
}
}
}
if (error) {
String errorMsg = getId() + ". Ignored MessageID : " + synCtx.getMessageID() + ". Could not store message to store [" + store.getName() + "]. Error:" + throwable.getLocalizedMessage();
logger.error(errorMsg, throwable);
try {
store.closeWriteConnection();
} catch (JMSException e) {
logger.error("Error while closing connection to store " + store.getName(), e);
}
connection = null;
if (logger.isDebugEnabled()) {
logger.debug(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
}
return false;
}
if (logger.isDebugEnabled()) {
logger.debug(getId() + ". Stored MessageID : " + synCtx.getMessageID());
}
store.enqueued();
return true;
}
use of org.apache.synapse.message.store.impl.commons.StorableMessage 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.commons.StorableMessage 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;
}
use of org.apache.synapse.message.store.impl.commons.StorableMessage 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