Search in sources :

Example 1 with PriorityExecutor

use of org.apache.synapse.commons.executors.PriorityExecutor 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 PriorityExecutor

use of org.apache.synapse.commons.executors.PriorityExecutor in project wso2-synapse by wso2.

the class PriorityExecutorDeployer method updateSynapseArtifact.

@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("PriorityExecutor update from file : " + fileName + " has started");
    }
    try {
        PriorityExecutor e = PriorityExecutorFactory.createExecutor(SynapseConstants.SYNAPSE_NAMESPACE, artifactConfig, true, properties);
        if (e == null) {
            handleSynapseArtifactDeploymentError("PriorityExecutor update failed. The artifact " + "defined in the file: " + fileName + " is not a valid executor.");
            return null;
        }
        e.setFileName(new File(fileName).getName());
        if (log.isDebugEnabled()) {
            log.debug("Executor: " + e.getName() + " has been built from the file: " + fileName);
        }
        e.init();
        PriorityExecutor existingExecutor = getSynapseConfiguration().getPriorityExecutors().get(existingArtifactName);
        if (existingArtifactName.equals(e.getName())) {
            getSynapseConfiguration().updatePriorityExecutor(existingArtifactName, e);
        } else {
            // The user has changed the name of the executor
            // We should add the updated executor as a new executor and remove the old one
            getSynapseConfiguration().addPriorityExecutor(e.getName(), e);
            getSynapseConfiguration().removeExecutor(existingArtifactName);
            log.info("Executor: " + existingArtifactName + " has been undeployed");
        }
        waitForCompletion();
        existingExecutor.destroy();
        log.info("PriorityExecutor: " + e.getName() + " has been updated from the file: " + fileName);
        return e.getName();
    } catch (DeploymentException e) {
        handleSynapseArtifactDeploymentError("Error while updating the executor from the " + "file: " + fileName);
    } catch (AxisFault e) {
        handleSynapseArtifactDeploymentError("Error while creating the executor from the " + "configuration in file: " + fileName);
    }
    return null;
}
Also used : AxisFault(org.apache.axis2.AxisFault) DeploymentException(org.apache.axis2.deployment.DeploymentException) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) File(java.io.File)

Example 3 with PriorityExecutor

use of org.apache.synapse.commons.executors.PriorityExecutor in project wso2-synapse by wso2.

the class PriorityExecutorDeployer method restoreSynapseArtifact.

@Override
public void restoreSynapseArtifact(String artifactName) {
    if (log.isDebugEnabled()) {
        log.debug("Restoring PriorityExecutor with name : " + artifactName + " : Started");
    }
    try {
        PriorityExecutor e = getSynapseConfiguration().getPriorityExecutors().get(artifactName);
        OMElement entryElem = PriorityExecutorSerializer.serialize(null, e, SynapseConstants.SYNAPSE_NAMESPACE);
        if (e.getFileName() != null) {
            String fileName = getServerConfigurationInformation().getSynapseXMLLocation() + File.separator + MultiXMLConfigurationBuilder.EXECUTORS_DIR + File.separator + e.getFileName();
            writeToFile(entryElem, fileName);
            if (log.isDebugEnabled()) {
                log.debug("Restoring the PriorityExecutor with name : " + artifactName + " : Completed");
            }
            log.info("PriorityExecutor named '" + artifactName + "' has been restored");
        } else {
            handleSynapseArtifactDeploymentError("Couldn't restore the PriorityExecutor named '" + artifactName + "', filename cannot be found");
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("Restoring of the PriorityExecutor named '" + artifactName + "' has failed", e);
    }
}
Also used : OMElement(org.apache.axiom.om.OMElement) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) DeploymentException(org.apache.axis2.deployment.DeploymentException)

Example 4 with PriorityExecutor

use of org.apache.synapse.commons.executors.PriorityExecutor in project wso2-synapse by wso2.

the class PriorityExecutorFactoryTest method testCreateExecutor.

/**
 * Test creating PriorityExecutor from given xml configuration
 * @throws XMLStreamException
 * @throws AxisFault
 */
public void testCreateExecutor() throws XMLStreamException, AxisFault {
    OMElement element = AXIOMUtil.stringToOM("<priority-executor xmlns:ns=\"http://ws.apache.org/ns/synapse\" name=\"executor1\">" + "<ns:queues></ns:queues>" + "</priority-executor>");
    PriorityExecutor executor = PriorityExecutorFactory.createExecutor("http://ws.apache.org/ns/synapse", element, false, new Properties());
    assertNotNull("Error creating PriorityExecutor", executor);
}
Also used : OMElement(org.apache.axiom.om.OMElement) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) Properties(java.util.Properties)

Example 5 with PriorityExecutor

use of org.apache.synapse.commons.executors.PriorityExecutor in project wso2-synapse by wso2.

the class PriorityExecutorFactoryTest method testCreateExecutorWithQueues.

/**
 * Test creating PriorityExecutor by defining multiple queues in the xml configuration
 * @throws XMLStreamException
 * @throws AxisFault
 */
public void testCreateExecutorWithQueues() throws XMLStreamException, AxisFault {
    OMElement element = AXIOMUtil.stringToOM("<priority-executor xmlns=\"http://ws.apache.org/ns/synapse\" name=\"executor1\">\n" + "   <queues isFixedSize=\"false\">\n" + "      <queue priority=\"10\"/>\n" + "      <queue priority=\"5\"/>\n" + "   </queues>\n" + "   <threads max=\"100\" core=\"20\" keep-alive=\"5\"/>\n" + "</priority-executor>");
    PriorityExecutor executor = PriorityExecutorFactory.createExecutor("http://ws.apache.org/ns/synapse", element, true, new Properties());
    assertEquals("Priority executor queue count invalid", 2, executor.getQueue().getQueues().size());
}
Also used : OMElement(org.apache.axiom.om.OMElement) PriorityExecutor(org.apache.synapse.commons.executors.PriorityExecutor) Properties(java.util.Properties)

Aggregations

PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)19 OMElement (org.apache.axiom.om.OMElement)11 Properties (java.util.Properties)5 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 ProxyService (org.apache.synapse.core.axis2.ProxyService)4 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)4 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)4 ArrayList (java.util.ArrayList)3 InternalQueue (org.apache.synapse.commons.executors.InternalQueue)3 MultiPriorityBlockingQueue (org.apache.synapse.commons.executors.MultiPriorityBlockingQueue)3 PRRNextQueueAlgorithm (org.apache.synapse.commons.executors.PRRNextQueueAlgorithm)3 Endpoint (org.apache.synapse.endpoints.Endpoint)3 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)3 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)3 MessageStore (org.apache.synapse.message.store.MessageStore)3 API (org.apache.synapse.rest.API)3 File (java.io.File)2 AxisFault (org.apache.axis2.AxisFault)2 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)2 Startup (org.apache.synapse.Startup)2