Search in sources :

Example 16 with MessageContext

use of org.apache.synapse.MessageContext 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();
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) ObjectMessage(javax.jms.ObjectMessage) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) Message(javax.jms.Message) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) MessageContext(org.apache.synapse.MessageContext)

Example 17 with MessageContext

use of org.apache.synapse.MessageContext 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;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) SynapseException(org.apache.synapse.SynapseException) IOException(java.io.IOException)

Example 18 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class JDBCMessageStore method remove.

/**
 * Removes the first element from table
 *
 * @return MessageContext - first message context
 * @throws NoSuchElementException if there was not element to be removed.
 */
@Override
public MessageContext remove() throws NoSuchElementException {
    MessageContext messageContext = peek();
    messageContext = remove(messageContext.getMessageID());
    if (messageContext != null) {
        return messageContext;
    } else {
        throw new NoSuchElementException("First element not found and remove failed !");
    }
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) NoSuchElementException(java.util.NoSuchElementException)

Example 19 with MessageContext

use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.

the class JDBCMessageStore method getResultMessageContextFromDatabase.

/**
 * Process a given SQL statement.
 *
 * @param statement - Statement to process.
 * @return - MessageContext which will hold the content of the message.
 */
private MessageContext getResultMessageContextFromDatabase(Statement statement) throws SynapseException {
    List<Map> processedRows = getProcessedRows(statement);
    final int firstRowIndex = 0;
    MessageContext messageContext = null;
    if (processedRows.size() > 0) {
        Map messageContentRow = processedRows.get(firstRowIndex);
        messageContext = (MessageContext) messageContentRow.get(MESSAGE_COLUMN_NAME);
        if (logger.isDebugEnabled()) {
            logger.debug("Number of rows processed:" + processedRows + " calling the statement " + statement.getStatement());
            logger.debug("Message content with mid:" + messageContext.getMessageID() + " will be returned");
        }
    }
    return messageContext;
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with MessageContext

use of org.apache.synapse.MessageContext 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;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MessageContext (org.apache.synapse.MessageContext)220 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)86 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)54 SynapseException (org.apache.synapse.SynapseException)29 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)26 ArrayList (java.util.ArrayList)24 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)24 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)18 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 OMElement (org.apache.axiom.om.OMElement)17 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)17 HashMap (java.util.HashMap)16 Mediator (org.apache.synapse.Mediator)16 TestMessageContext (org.apache.synapse.TestMessageContext)16 Map (java.util.Map)15 Properties (java.util.Properties)15 Test (org.junit.Test)15 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)14 QName (javax.xml.namespace.QName)13