Search in sources :

Example 1 with MessageStore

use of org.apache.synapse.message.store.MessageStore in project wso2-synapse by wso2.

the class MessageStoreMediator method mediate.

public boolean mediate(MessageContext synCtx) {
    boolean produceStatus;
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    if (synCtx != null) {
        MessageStore messageStore;
        if (messageStoreExp != null) {
            messageStore = synCtx.getConfiguration().getMessageStore(messageStoreExp.stringValueOf(synCtx));
        } else {
            messageStore = synCtx.getConfiguration().getMessageStore(messageStoreName);
        }
        if (messageStore != null) {
            if (messageStore.getParameters().get(PRODUCER_GUARANTEED_DELIVERY) != null) {
                isGuaranteedDeliveryEnabled = Boolean.parseBoolean(messageStore.getParameters().get(PRODUCER_GUARANTEED_DELIVERY).toString());
            }
            if (messageStore.getParameters().get(FAILOVER_MESSAGE_STORE_NAME) != null) {
                failoverMessageStoreName = (String) messageStore.getParameters().get(FAILOVER_MESSAGE_STORE_NAME);
            }
            if (onStoreSequence != null) {
                Mediator sequence = synCtx.getSequence(onStoreSequence);
                if (sequence != null) {
                    sequence.mediate(synCtx);
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Message Store mediator storing the message : \n " + synCtx.getEnvelope());
            }
            // Ensure that the message is fully read
            synCtx.getEnvelope().buildWithAttachments();
            // Clone the message before sending to the producer
            // Fix ESBJAVA-3650
            MessageContext newCtx = null;
            try {
                newCtx = MessageHelper.cloneMessageContext(synCtx);
                ContinuationStackManager.clearStack(newCtx);
            } catch (AxisFault af) {
                handleException("Error when cloning the message context", af, synCtx);
            }
            synchronized (storeMessageLock) {
                produceStatus = messageStore.getProducer().storeMessage(newCtx);
            }
            if (!produceStatus) {
                // Fix ESBJAVA-5011, since connection is already null need to nullify producer also
                if (messageStore instanceof JmsStore) {
                    ((JmsStore) messageStore).setProducer(null);
                }
                if (isGuaranteedDeliveryEnabled && failoverMessageStoreName != null && !failoverMessageStoreName.isEmpty()) {
                    MessageStore failoverMessageStore = synCtx.getConfiguration().getMessageStore(failoverMessageStoreName);
                    boolean failoverProduceStatus = failoverMessageStore.getProducer().storeMessage(newCtx);
                    if (!failoverProduceStatus) {
                        synCtx.setProperty(NhttpConstants.HTTP_SC, 500);
                        synCtx.setProperty(NhttpConstants.ERROR_DETAIL, "Failed to store message.");
                        synCtx.setProperty(NhttpConstants.ERROR_MESSAGE, "Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].");
                        handleException("Failed to store message [" + synCtx.getMessageID() + "] in failover store [" + failoverMessageStoreName + "].", synCtx);
                    }
                    if (shouldTrace(synCtx)) {
                        trace.error("Message [" + synCtx.getMessageID() + "] store in the failover message store [" + failoverMessageStoreName + "]");
                    }
                } else {
                    synCtx.setProperty(NhttpConstants.HTTP_SC, 500);
                    synCtx.setProperty(NhttpConstants.ERROR_DETAIL, "Failed to store message.");
                    synCtx.setProperty(NhttpConstants.ERROR_MESSAGE, "Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].");
                    handleException("Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].", 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 msgCtx = (Axis2MessageContext) synCtx;
            if (null != msgCtx.getAxis2MessageContext() && null != msgCtx.getAxis2MessageContext().getOperationContext()) {
                msgCtx.getAxis2MessageContext().getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
            }
            return true;
        } else {
            handleException("Message Store does not exist.", synCtx);
        }
    }
    return false;
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) AxisFault(org.apache.axis2.AxisFault) JmsStore(org.apache.synapse.message.store.impl.jms.JmsStore) AbstractMediator(org.apache.synapse.mediators.AbstractMediator) Mediator(org.apache.synapse.Mediator) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 2 with MessageStore

use of org.apache.synapse.message.store.MessageStore in project wso2-synapse by wso2.

the class MessageStoreDeployer method deploySynapseArtifact.

@Override
public String deploySynapseArtifact(OMElement artifactConfig, String fileName, Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("Message Store Deployment from file : " + fileName + " : Started");
    }
    try {
        MessageStore ms = MessageStoreFactory.createMessageStore(artifactConfig, properties);
        if (ms != null) {
            ms.setFileName((new File(fileName)).getName());
            /**
             * Set the name of the artifact container from which the message store deployed
             */
            ms.setArtifactContainerName(customLogContent);
            if (log.isDebugEnabled()) {
                log.debug("Message Store named '" + ms.getName() + "' has been built from the file " + fileName);
            }
            ms.init(getSynapseEnvironment());
            if (log.isDebugEnabled()) {
                log.debug("Initialized the Message Store : " + ms.getName());
            }
            getSynapseConfiguration().addMessageStore(ms.getName(), ms);
            if (log.isDebugEnabled()) {
                log.debug("Message Store Deployment from file : " + fileName + " : Completed");
            }
            log.info("Message Store named '" + ms.getName() + "' has been deployed from file : " + fileName);
            return ms.getName();
        } else {
            handleSynapseArtifactDeploymentError("Message Store Deployment from the file : " + fileName + " : Failed. The artifact " + "described in the file  is not a Message Store");
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("Message Store Deployment from the file : " + fileName + " : Failed.", e);
    }
    return null;
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) File(java.io.File) DeploymentException(org.apache.axis2.deployment.DeploymentException)

Example 3 with MessageStore

use of org.apache.synapse.message.store.MessageStore in project wso2-synapse by wso2.

the class MessageStoreDeployer method updateSynapseArtifact.

@Override
public String updateSynapseArtifact(OMElement artifactConfig, String fileName, String existingArtifactName, Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("Message Store update from file : " + fileName + " has started");
    }
    try {
        MessageStore ms = MessageStoreFactory.createMessageStore(artifactConfig, properties);
        if (ms == null) {
            handleSynapseArtifactDeploymentError("Message Store update failed. The artifact " + "defined in the file: " + fileName + " is not valid");
            return null;
        }
        ms.setFileName(new File(fileName).getName());
        if (log.isDebugEnabled()) {
            log.debug("MessageStore: " + ms.getName() + " has been built from the file: " + fileName);
        }
        ms.init(getSynapseEnvironment());
        MessageStore existingMs = getSynapseConfiguration().getMessageStore(existingArtifactName);
        // We should add the updated MessageStore as a new MessageStore and remove the old one
        getSynapseConfiguration().removeMessageStore(existingArtifactName);
        getSynapseConfiguration().addMessageStore(ms.getName(), ms);
        log.info("MessageStore: " + existingArtifactName + " has been undeployed");
        log.info("MessageStore: " + ms.getName() + " has been updated from the file: " + fileName);
        waitForCompletion();
        existingMs.destroy();
        return ms.getName();
    } catch (DeploymentException e) {
        handleSynapseArtifactDeploymentError("Error while updating the MessageStore from the " + "file: " + fileName);
    }
    return null;
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) DeploymentException(org.apache.axis2.deployment.DeploymentException) File(java.io.File)

Example 4 with MessageStore

use of org.apache.synapse.message.store.MessageStore in project wso2-synapse by wso2.

the class MessageStoreDeployer method undeploySynapseArtifact.

@Override
public void undeploySynapseArtifact(String artifactName) {
    if (log.isDebugEnabled()) {
        log.debug("MessageStore Undeployment of the MessageStore named : " + artifactName + " : Started");
    }
    try {
        MessageStore ms = getSynapseConfiguration().getMessageStore(artifactName);
        if (ms != null) {
            getSynapseConfiguration().removeMessageStore(artifactName);
            if (log.isDebugEnabled()) {
                log.debug("Destroying the MessageStore named : " + artifactName);
            }
            ms.destroy();
            if (log.isDebugEnabled()) {
                log.debug("MessageStore Undeployment of the endpoint named : " + artifactName + " : Completed");
            }
            log.info("MessageStore named '" + ms.getName() + "' has been undeployed");
        } else if (log.isDebugEnabled()) {
            log.debug("MessageStore " + artifactName + " has already been undeployed");
        }
    } catch (Exception e) {
        handleSynapseArtifactDeploymentError("MessageStore Undeployement of MessageStore named : " + artifactName + " : Failed", e);
    }
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) DeploymentException(org.apache.axis2.deployment.DeploymentException)

Example 5 with MessageStore

use of org.apache.synapse.message.store.MessageStore in project wso2-synapse by wso2.

the class SynapseXMLConfigurationSerializerTest method testSerializeConfiguration9.

/**
 * Test serializeConfigurationMethod with messageStore added for SynapseConfiguration and assert OMElement returned
 */
@Test
public void testSerializeConfiguration9() {
    SynapseXMLConfigurationSerializer serializer = new SynapseXMLConfigurationSerializer();
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    MessageStore messageStore = new JmsStore();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    parameters.put("java.naming.provider.url", "tcp://127.0.0.1:61616");
    messageStore.setName("testMessageStore");
    messageStore.setParameters(parameters);
    synapseConfiguration.addMessageStore(messageStore.getName(), messageStore);
    OMElement element = serializer.serializeConfiguration(synapseConfiguration);
    Assert.assertNotNull("OMElement is not returned", element);
    Assert.assertEquals("definitions", element.getLocalName());
    Assert.assertTrue("MessageStore added is not serialized.", element.getChildren().next().toString().contains("name=\"testMessageStore\""));
}
Also used : MessageStore(org.apache.synapse.message.store.MessageStore) JmsStore(org.apache.synapse.message.store.impl.jms.JmsStore) HashMap(java.util.HashMap) OMElement(org.apache.axiom.om.OMElement) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Test(org.junit.Test)

Aggregations

MessageStore (org.apache.synapse.message.store.MessageStore)14 OMElement (org.apache.axiom.om.OMElement)6 DeploymentException (org.apache.axis2.deployment.DeploymentException)4 SynapseException (org.apache.synapse.SynapseException)4 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)3 ProxyService (org.apache.synapse.core.axis2.ProxyService)3 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)3 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)3 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)3 API (org.apache.synapse.rest.API)3 File (java.io.File)2 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 AbstractMessageProcessor (org.apache.synapse.message.processor.impl.AbstractMessageProcessor)2 JmsStore (org.apache.synapse.message.store.impl.jms.JmsStore)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1