Search in sources :

Example 21 with MessageContext

use of org.apache.synapse.MessageContext 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;
}
Also used : SynapseException(org.apache.synapse.SynapseException) ByteArrayInputStream(java.io.ByteArrayInputStream) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 22 with MessageContext

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

the class ResequenceMessageStore method getNextMessage.

/**
 * Will get the next message belonging to a sequence.
 *
 * @return the next message in the sequence.
 */
private MessageContext getNextMessage() {
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        String tableName = getJdbcConfiguration().getTableName();
        String selectMessageStatement = "SELECT message FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "= ?";
        Statement statement = new Statement(selectMessageStatement) {

            @Override
            public List<Map> getResult(ResultSet resultSet) throws SQLException {
                return messageContentResultSet(resultSet, this.getStatement());
            }
        };
        statement.addParameter(nextSequenceId);
        List<Map> processedRows = getProcessedRows(statement);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            nextSequenceId++;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned for sequence " + nextSequenceId);
            }
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Sequences not returned from DB, next sequence will be:" + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with MessageContext

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

the class ResequenceMessageStore method getMessageWithMinimumSequence.

/**
 * <p>
 * Retrieve the minimum sequence number of the available sequence ids in the DB
 * </p>
 * <p>
 * This operation should call when there's a gap and a timeout occurs.
 * </p>
 * <p>
 * <b>Note : </b> This operation would reset the "nextSequenceId" to the minimum sequence id generated from the
 * DB.
 * </p>
 *
 * @return the message context of the next sequence
 */
private MessageContext getMessageWithMinimumSequence() {
    String tableName = getJdbcConfiguration().getTableName();
    String selectMinimumSequenceIdStatement = "SELECT message,seq_id FROM " + tableName + " WHERE " + ResequenceMessageStoreConstants.SEQ_ID + "=(SELECT min(" + ResequenceMessageStoreConstants.SEQ_ID + ")" + " from " + tableName + ")";
    Statement stmt = new Statement(selectMinimumSequenceIdStatement) {

        @Override
        public List<Map> getResult(ResultSet resultSet) throws SQLException {
            return getMessageWithMinimumId(resultSet, this.getStatement());
        }
    };
    MessageContext msg = null;
    final int firstRowIndex = 0;
    try {
        List<Map> processedRows = getProcessedRows(stmt);
        if (!processedRows.isEmpty()) {
            msg = getMessageContext(processedRows, firstRowIndex);
            long sequenceId = getSequenceId(processedRows, firstRowIndex);
            nextSequenceId = sequenceId + 1;
            if (log.isTraceEnabled()) {
                log.trace("Message with id " + msg.getMessageID() + " returned as the minimum, the minimum " + "sequence " + "will be marked as " + nextSequenceId);
            }
        }
    } catch (SynapseException ex) {
        throw new SynapseException("Error while peek the message", ex);
    }
    return msg;
}
Also used : SynapseException(org.apache.synapse.SynapseException) Statement(org.apache.synapse.message.store.impl.jdbc.util.Statement) ResultSet(java.sql.ResultSet) MessageContext(org.apache.synapse.MessageContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with MessageContext

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

the class ResequenceMessageStore method peek.

/**
 * {@inheritDoc}
 */
@Override
public MessageContext peek() throws SynapseException {
    MessageContext msg;
    if (!hasStarted) {
        nextSequenceId = readStartId() + 1;
        hasStarted = true;
    }
    msg = getNextMessage();
    if (null == msg && !shouldWait()) {
        msg = getMessageWithMinimumSequence();
    }
    if (null != msg) {
        long currentSequenceId = nextSequenceId - 1;
        String messageId = msg.getMessageID();
        sequenceIdMapper.put(messageId, currentSequenceId);
        if (nextElapsedTime > 0) {
            nextElapsedTime = System.currentTimeMillis() + gapTimeoutInterval;
        }
        if (log.isDebugEnabled()) {
            log.debug("Message with sequence " + currentSequenceId + " and message id " + messageId + " will be returned to the processor.");
            log.debug("Next elapsed time would be marked as:" + nextElapsedTime);
        }
    }
    return msg;
}
Also used : MessageContext(org.apache.synapse.MessageContext)

Example 25 with MessageContext

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

the class TemplateMessageExecutor method execute.

public void execute() {
    // Inject the message into SynapseEnvironment
    MessageContext mc = synapseEnvironment.createMessageContext();
    mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
    synapseEnvironment.injectAsync(mc, seqMed);
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) MessageContext(org.apache.synapse.MessageContext)

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