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;
}
}
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));
}
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();
}
}
}
Aggregations