Search in sources :

Example 1 with JmsStore

use of org.apache.synapse.message.store.impl.jms.JmsStore 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 JmsStore

use of org.apache.synapse.message.store.impl.jms.JmsStore in project wso2-synapse by wso2.

the class FailoverForwardingServiceTest method test.

/**
 * Testing whether the failover forwarding service is successfully storing
 * the message in the queue
 *
 * @throws Exception
 */
@Test
public void test() throws Exception {
    JmsStore jmsStore = 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");
    jmsStore.setParameters(parameters);
    jmsStore.setName("JMSStore");
    MessageProcessor messageProcessor = new FailoverScheduledMessageForwardingProcessor();
    Map<String, Object> parametersPro = new HashMap<>();
    parametersPro.put("message.target.store.name", jmsStore.getName());
    parametersPro.put("interval", "5000");
    messageProcessor.setName("FailoverProcessor");
    messageProcessor.setParameters(parametersPro);
    messageProcessor.setMessageStoreName("JMSStore");
    SynapseConfiguration synapseConfiguration = new SynapseConfiguration();
    ConfigurationContext cfgCtx = new ConfigurationContext(synapseConfiguration.getAxisConfiguration());
    SynapseEnvironment synapseEnvironment = new Axis2SynapseEnvironment(cfgCtx, synapseConfiguration);
    synapseConfiguration.addMessageStore("JMSStore", jmsStore);
    synapseConfiguration.addMessageProcessor("FailoverProcessor", messageProcessor);
    jmsStore.init(synapseEnvironment);
    FailoverForwardingService failoverForwardingService = new FailoverForwardingService(messageProcessor, synapseEnvironment, 5000, false);
    failoverForwardingService.init(synapseEnvironment);
    Axis2MessageContext axis2MessageContext = new Axis2MessageContext(new org.apache.axis2.context.MessageContext(), synapseConfiguration, synapseEnvironment);
    MessageContext messageContext = axis2MessageContext;
    SOAP11Factory factory = new SOAP11Factory();
    SOAPEnvelope envelope = factory.getDefaultEnvelope();
    OMElement element = AXIOMUtil.stringToOM("<name><value>Test</value></name>");
    envelope.getBody().addChild(element);
    messageContext.setEnvelope(envelope);
    Assert.assertEquals("Queue is not empty!", 0, broker.getAdminView().getTotalMessageCount());
    failoverForwardingService.dispatch(messageContext);
    Assert.assertEquals("Message not forwarded!", 1, broker.getAdminView().getTotalMessageCount());
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) HashMap(java.util.HashMap) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) MessageProcessor(org.apache.synapse.message.processor.MessageProcessor) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) JmsStore(org.apache.synapse.message.store.impl.jms.JmsStore) SOAP11Factory(org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 3 with JmsStore

use of org.apache.synapse.message.store.impl.jms.JmsStore 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

JmsStore (org.apache.synapse.message.store.impl.jms.JmsStore)3 HashMap (java.util.HashMap)2 OMElement (org.apache.axiom.om.OMElement)2 MessageContext (org.apache.synapse.MessageContext)2 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 MessageStore (org.apache.synapse.message.store.MessageStore)2 Test (org.junit.Test)2 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SOAP11Factory (org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory)1 AxisFault (org.apache.axis2.AxisFault)1 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)1 Mediator (org.apache.synapse.Mediator)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)1 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)1 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)1