Search in sources :

Example 1 with SamplingThrottleMediator

use of org.apache.synapse.mediators.eip.sample.SamplingThrottleMediator in project wso2-synapse by wso2.

the class SamplingThrottleMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement omElement, Properties properties) {
    SamplingThrottleMediator samplingThrottleMediator = new SamplingThrottleMediator();
    processAuditStatus(samplingThrottleMediator, omElement);
    OMAttribute idAttribute = omElement.getAttribute(ID_ATTR);
    if (idAttribute != null) {
        samplingThrottleMediator.setId(idAttribute.getAttributeValue());
    }
    OMAttribute rateAttribute = omElement.getAttribute(RATE_ATTR);
    if (rateAttribute != null) {
        try {
            samplingThrottleMediator.setSamplingRate(Integer.parseInt(rateAttribute.getAttributeValue()));
        } catch (NumberFormatException nfe) {
            handleException("Sampling rate has to be an integer value, but found : " + rateAttribute.getAttributeValue());
        }
    }
    OMAttribute unitTimeAttribute = omElement.getAttribute(UNIT_TIME_ATTR);
    if (unitTimeAttribute != null) {
        try {
            samplingThrottleMediator.setUnitTime(Long.parseLong(unitTimeAttribute.getAttributeValue()));
        } catch (NumberFormatException nfe) {
            handleException("Sampling unitTime has to be a long value in milliseconds, " + "but found : " + rateAttribute.getAttributeValue());
        }
    }
    OMElement targetElem = omElement.getFirstChildWithName(TARGET_Q);
    if (targetElem != null) {
        Target target = TargetFactory.createTarget(targetElem, properties);
        samplingThrottleMediator.setTarget(target);
    } else {
        handleException("Sampler requires a target for the sampling mediation");
    }
    OMElement messageQueueElem = omElement.getFirstChildWithName(MESSAGE_QUEUE_Q);
    if (messageQueueElem != null && messageQueueElem.getAttribute(CLASS_ATTR) != null) {
        String className = messageQueueElem.getAttributeValue(CLASS_ATTR);
        try {
            Class messageQueueImplClass = Class.forName(className);
            Object obj = messageQueueImplClass.newInstance();
            if (obj instanceof MessageQueue) {
                samplingThrottleMediator.setMessageQueue((MessageQueue) obj);
            } else {
                handleException("Provided message queue class : " + className + " doesn't implement the org.apache.synapse.mediators." + "eip.sample.MessageQueue interface");
            }
        } catch (ClassNotFoundException e) {
            handleException("Couldn't find the class specified for the message queue " + "implementation : " + className);
        } catch (InstantiationException e) {
            handleException("Couldn't instantiate the message queue : " + className);
        } catch (IllegalAccessException e) {
            handleException("Couldn't instantiate the message queue : " + className);
        }
    }
    return samplingThrottleMediator;
}
Also used : Target(org.apache.synapse.mediators.eip.Target) MessageQueue(org.apache.synapse.mediators.eip.sample.MessageQueue) SamplingThrottleMediator(org.apache.synapse.mediators.eip.sample.SamplingThrottleMediator) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with SamplingThrottleMediator

use of org.apache.synapse.mediators.eip.sample.SamplingThrottleMediator in project wso2-synapse by wso2.

the class SamplingThrottleMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator mediator) {
    OMElement samplerElem = fac.createOMElement("sampler", synNS);
    saveTracingState(samplerElem, mediator);
    SamplingThrottleMediator samplingThrottleMediator = (SamplingThrottleMediator) mediator;
    if (samplingThrottleMediator.getId() != null) {
        samplerElem.addAttribute("id", samplingThrottleMediator.getId(), nullNS);
    }
    samplerElem.addAttribute("rate", Integer.toString(samplingThrottleMediator.getSamplingRate()), nullNS);
    samplerElem.addAttribute("unitTime", Long.toString(samplingThrottleMediator.getUnitTime()), nullNS);
    if (samplingThrottleMediator.isMessageQueueExplicitlySet()) {
        OMElement messageQueueElem = fac.createOMElement("messageQueue", synNS);
        messageQueueElem.addAttribute("class", samplingThrottleMediator.getMessageQueue().getClass().getName(), nullNS);
        samplerElem.addChild(messageQueueElem);
    }
    if (samplingThrottleMediator.getTarget() != null) {
        samplerElem.addChild(TargetSerializer.serializeTarget(samplingThrottleMediator.getTarget()));
    } else {
        handleException("Couldn't find the target for the sampler. " + "Target is mandatory for a sampler");
    }
    return samplerElem;
}
Also used : SamplingThrottleMediator(org.apache.synapse.mediators.eip.sample.SamplingThrottleMediator) OMElement(org.apache.axiom.om.OMElement)

Aggregations

OMElement (org.apache.axiom.om.OMElement)2 SamplingThrottleMediator (org.apache.synapse.mediators.eip.sample.SamplingThrottleMediator)2 OMAttribute (org.apache.axiom.om.OMAttribute)1 Target (org.apache.synapse.mediators.eip.Target)1 MessageQueue (org.apache.synapse.mediators.eip.sample.MessageQueue)1