Search in sources :

Example 1 with MediatorWorker

use of org.apache.synapse.mediators.MediatorWorker in project wso2-synapse by wso2.

the class EnqueueMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog log = getLog(synCtx);
    if (log.isTraceOrDebugEnabled()) {
        log.traceOrDebug("Start: enqueue mediator");
    }
    assert executorName != null : "executor name shouldn't be null";
    PriorityExecutor executor = synCtx.getConfiguration().getPriorityExecutors().get(executorName);
    if (executor == null) {
        log.auditWarn("Cannot find executor " + executorName + ". Using existing thread for mediation");
        Mediator m = synCtx.getSequence(sequenceName);
        if (m != null && m instanceof SequenceMediator) {
            return m.mediate(synCtx);
        } else {
            handleException("Sequence cannot be found : " + sequenceName, synCtx);
            return false;
        }
    }
    Mediator m = synCtx.getSequence(sequenceName);
    if (m != null && m instanceof SequenceMediator) {
        MediatorWorker worker = new MediatorWorker(m, synCtx);
        try {
            // execute with the given priority
            executor.execute(worker, priority);
        } catch (RejectedExecutionException ex) {
            // if RejectedExecutionException, jump to fault handler
            handleException("Unable to process message in priority executor " + executorName + " with priority " + priority + ". Thread pool exhausted.", synCtx);
        }
        // with the nio transport, this causes the listener not to write a 202
        // Accepted response, as this implies that Synapse does not yet know if
        // a 202 or 200 response would be written back.
        ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
        if (log.isTraceOrDebugEnabled()) {
            log.traceOrDebug("End: enqueue mediator");
        }
        return true;
    } else {
        handleException("Sequence cannot be found : " + sequenceName, synCtx);
        return false;
    }
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) MediatorWorker(org.apache.synapse.mediators.MediatorWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with MediatorWorker

use of org.apache.synapse.mediators.MediatorWorker in project wso2-synapse by wso2.

the class Axis2SynapseEnvironment method injectAsync.

public void injectAsync(final MessageContext synCtx, SequenceMediator seq) {
    if (log.isDebugEnabled()) {
        log.debug("Injecting MessageContext for asynchronous mediation using the : " + (seq.getName() == null ? "Anonymous" : seq.getName()) + " Sequence");
    }
    if (RuntimeStatisticCollector.isStatisticsEnabled()) {
        OpenEventCollector.reportFlowAsynchronousEvent(synCtx);
    }
    synCtx.setEnvironment(this);
    executorService.execute(new MediatorWorker(seq, synCtx));
}
Also used : MediatorWorker(org.apache.synapse.mediators.MediatorWorker)

Example 3 with MediatorWorker

use of org.apache.synapse.mediators.MediatorWorker in project wso2-synapse by wso2.

the class Axis2SynapseEnvironment method injectInbound.

/**
 * Used by inbound polling endpoints to inject the message to synapse engine
 *
 * @param synCtx message context
 * @param sequential whether message should be injected in sequential manner
 *                   without spawning new threads
 * @return Boolean - Indicate if were able to inject the message
 * @throws SynapseException
 *             - in case error occured during the mediation
 */
public boolean injectInbound(final MessageContext synCtx, SequenceMediator seq, boolean sequential) throws SynapseException {
    String inboundName = null;
    boolean isStatisticsEnabled = RuntimeStatisticCollector.isStatisticsEnabled();
    AspectConfiguration inboundAspectConfiguration = null;
    Integer statisticReportingIndex = null;
    if (log.isDebugEnabled()) {
        log.debug("Injecting MessageContext for inbound mediation using the : " + (seq.getName() == null ? "Anonymous" : seq.getName()) + " Sequence");
    }
    /*
         * If the method is invoked by the inbound endpoint
         * Then check for the endpoint name and then set the Log Appender Content
         */
    if (synCtx.getProperty(SynapseConstants.INBOUND_ENDPOINT_NAME) != null) {
        InboundEndpoint inboundEndpoint = synCtx.getConfiguration().getInboundEndpoint((String) synCtx.getProperty(SynapseConstants.INBOUND_ENDPOINT_NAME));
        if (inboundEndpoint != null) {
            CustomLogSetter.getInstance().setLogAppender(inboundEndpoint.getArtifactContainerName());
            if (inboundEndpoint.getAspectConfiguration() != null) {
                // inboundStatistics = inboundEndpoint.getAspectConfiguration().isStatisticsEnable();
                inboundAspectConfiguration = inboundEndpoint.getAspectConfiguration();
            }
            inboundName = (String) synCtx.getProperty(SynapseConstants.INBOUND_ENDPOINT_NAME);
        }
    }
    synCtx.setEnvironment(this);
    if (!invokeHandlers(synCtx)) {
        return false;
    }
    if (!sequential) {
        try {
            if (isStatisticsEnabled) {
                statisticReportingIndex = OpenEventCollector.reportEntryEvent(synCtx, inboundName, inboundAspectConfiguration, ComponentType.INBOUNDENDPOINT);
            }
            executorServiceInbound.execute(new MediatorWorker(seq, synCtx));
            return true;
        } catch (RejectedExecutionException re) {
            // If the pool is full complete the execution with the same thread
            log.warn("Inbound worker pool has reached the maximum capacity and will be processing current message sequentially.");
        } finally {
            if (isStatisticsEnabled) {
                CloseEventCollector.tryEndFlow(synCtx, inboundName, ComponentType.INBOUNDENDPOINT, statisticReportingIndex, false);
            }
        }
    }
    // reached max level
    if (isStatisticsEnabled) {
        statisticReportingIndex = OpenEventCollector.reportEntryEvent(synCtx, inboundName, inboundAspectConfiguration, ComponentType.INBOUNDENDPOINT);
    }
    try {
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.acquireMediationFlowLock();
            debugManager.advertiseMediationFlowStartPoint(synCtx);
        }
        seq.mediate(synCtx);
        return true;
    } catch (SynapseException syne) {
        if (!synCtx.getFaultStack().isEmpty()) {
            log.warn("Executing fault handler due to exception encountered");
            ((FaultHandler) synCtx.getFaultStack().pop()).handleFault(synCtx, syne);
            return true;
        } else {
            log.warn("Exception encountered but no fault handler found - message dropped");
            throw syne;
        }
    } catch (Exception e) {
        String msg = "Unexpected error executing task/async inject";
        log.error(msg, e);
        if (synCtx.getServiceLog() != null) {
            synCtx.getServiceLog().error(msg, e);
        }
        if (!synCtx.getFaultStack().isEmpty()) {
            log.warn("Executing fault handler due to exception encountered");
            ((FaultHandler) synCtx.getFaultStack().pop()).handleFault(synCtx, e);
            return true;
        } else {
            log.warn("Exception encountered but no fault handler found - message dropped");
            throw new SynapseException("Exception encountered but no fault handler found - message dropped", e);
        }
    } catch (Throwable e) {
        String msg = "Unexpected error executing inbound/async inject, message dropped";
        log.error(msg, e);
        if (synCtx.getServiceLog() != null) {
            synCtx.getServiceLog().error(msg, e);
        }
        throw new SynapseException(msg, e);
    } finally {
        if (isStatisticsEnabled) {
            CloseEventCollector.tryEndFlow(synCtx, inboundName, ComponentType.INBOUNDENDPOINT, statisticReportingIndex, false);
        }
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.advertiseMediationFlowTerminatePoint(synCtx);
            debugManager.releaseMediationFlowLock();
        }
    }
}
Also used : InboundEndpoint(org.apache.synapse.inbound.InboundEndpoint) SynapseException(org.apache.synapse.SynapseException) AspectConfiguration(org.apache.synapse.aspects.AspectConfiguration) SynapseDebugManager(org.apache.synapse.debug.SynapseDebugManager) MediatorWorker(org.apache.synapse.mediators.MediatorWorker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SynapseException(org.apache.synapse.SynapseException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

MediatorWorker (org.apache.synapse.mediators.MediatorWorker)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Mediator (org.apache.synapse.Mediator)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseLog (org.apache.synapse.SynapseLog)1 AspectConfiguration (org.apache.synapse.aspects.AspectConfiguration)1 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 SynapseDebugManager (org.apache.synapse.debug.SynapseDebugManager)1 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)1 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)1 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)1