Search in sources :

Example 6 with StorableMessage

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;
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) MessageContext(org.apache.synapse.MessageContext) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with StorableMessage

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) AMQBasicProperties(com.rabbitmq.client.impl.AMQBasicProperties) Channel(com.rabbitmq.client.Channel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) AMQP(com.rabbitmq.client.AMQP) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

StorableMessage (org.apache.synapse.message.store.impl.commons.StorableMessage)7 IOException (java.io.IOException)4 MessageContext (org.apache.synapse.MessageContext)3 SynapseException (org.apache.synapse.SynapseException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (org.apache.synapse.message.store.impl.jdbc.util.Statement)2 AMQP (com.rabbitmq.client.AMQP)1 Channel (com.rabbitmq.client.Channel)1 GetResponse (com.rabbitmq.client.GetResponse)1 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)1 AMQBasicProperties (com.rabbitmq.client.impl.AMQBasicProperties)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1