use of org.apache.synapse.message.store.impl.commons.StorableMessage in project wso2-synapse by wso2.
the class RabbitMQConsumer method receive.
public MessageContext receive() {
if (!checkConnection()) {
if (!reconnect()) {
if (logger.isDebugEnabled()) {
logger.debug(getId() + " cannot receive message from store. Can not reconnect.");
}
return null;
} else {
logger.info(getId() + " reconnected to store.");
isReceiveError = false;
}
}
// setting channel
if (channel != null) {
if (!channel.isOpen()) {
if (!setChannel()) {
logger.info(getId() + " unable to create the channel.");
return null;
}
}
} else {
if (!setChannel()) {
logger.info(getId() + " unable to create the channel.");
return null;
}
}
// receive messages
try {
GetResponse delivery = null;
delivery = channel.basicGet(queueName, false);
if (delivery != null) {
// deserilizing message
StorableMessage storableMessage = null;
ByteArrayInputStream bis = new ByteArrayInputStream(delivery.getBody());
ObjectInput in = new ObjectInputStream(bis);
try {
storableMessage = (StorableMessage) in.readObject();
} catch (ClassNotFoundException e) {
logger.error(getId() + "unable to read the stored message" + e);
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
bis.close();
in.close();
org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
MessageContext synapseMc = store.newSynapseMc(axis2Mc);
synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
updateCache(delivery, synapseMc, null, false);
if (logger.isDebugEnabled()) {
logger.debug(getId() + " Received MessageId:" + delivery.getProps().getMessageId());
}
return synapseMc;
}
} catch (ShutdownSignalException sse) {
logger.error(getId() + " connection error when receiving messages" + sse);
} catch (IOException ioe) {
logger.error(getId() + " connection error when receiving messages" + ioe);
}
return null;
}
use of org.apache.synapse.message.store.impl.commons.StorableMessage in project wso2-synapse by wso2.
the class RabbitMQProducer method storeMessage.
public boolean storeMessage(MessageContext synCtx) {
if (synCtx == null) {
return false;
}
if (connection == null) {
if (logger.isDebugEnabled()) {
logger.error(getId() + " cannot proceed. RabbitMQ Connection is null.");
}
logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
return false;
}
StorableMessage message = MessageConverter.toStorableMessage(synCtx);
boolean error = false;
Throwable throwable = null;
Channel channel = null;
try {
// Serializing message
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutput objOut = new ObjectOutputStream(os);
objOut.writeObject(message);
byte[] byteForm = os.toByteArray();
objOut.close();
os.close();
// building AMQP message
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
builder.messageId(synCtx.getMessageID());
builder.deliveryMode(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode());
builder.priority(message.getPriority(DEFAULT_PRIORITY));
channel = connection.createChannel();
if (exchangeName == null) {
channel.basicPublish("", queueName, builder.build(), byteForm);
} else {
channel.basicPublish(exchangeName, queueName, builder.build(), byteForm);
}
} catch (IOException e) {
throwable = e;
error = true;
isConnectionError = true;
} catch (Throwable t) {
throwable = t;
error = true;
} finally {
if (channel != null && channel.isOpen())
try {
channel.close();
} catch (TimeoutException e) {
logger.error("Error when closing connection,TimeoutException" + synCtx.getMessageID() + ". ", e);
} catch (IOException e) {
logger.error("Error when closing connection" + synCtx.getMessageID() + ". " + 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);
store.closeProducerConnection();
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;
}
Aggregations