Search in sources :

Example 1 with RoundRobin

use of org.apache.synapse.endpoints.algorithms.RoundRobin in project wso2-synapse by wso2.

the class LoadbalanceAlgorithmFactory method createLoadbalanceAlgorithm.

public static LoadbalanceAlgorithm createLoadbalanceAlgorithm(OMElement loadbalanceElement, List endpoints) {
    // default algorithm is round robin
    LoadbalanceAlgorithm algorithm = new RoundRobin(endpoints);
    OMAttribute policyAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_POLICY));
    OMAttribute algoAttribute = loadbalanceElement.getAttribute(new QName(null, XMLConfigConstants.LOADBALANCE_ALGORITHM));
    if (policyAttribute != null && algoAttribute != null) {
        String msg = "You cannot specify both the 'policy' & 'algorithm' in the configuration. " + "It is sufficient to provide only the 'algorithm'.";
        // We cannot continue execution. Hence it is logged at fatal level
        log.fatal(msg);
        throw new SynapseException(msg);
    }
    if (algoAttribute != null) {
        String algorithmStr = algoAttribute.getAttributeValue().trim();
        try {
            algorithm = (LoadbalanceAlgorithm) Class.forName(algorithmStr).newInstance();
            algorithm.setEndpoints(endpoints);
        } catch (Exception e) {
            String msg = "Cannot instantiate LoadbalanceAlgorithm implementation class " + algorithmStr;
            // We cannot continue execution. Hence it is logged at fatal level
            log.fatal(msg, e);
            throw new SynapseException(msg, e);
        }
    } else if (policyAttribute != null) {
        // currently only the roundRobin policy is supported
        if (!policyAttribute.getAttributeValue().trim().equals("roundRobin")) {
            String msg = "Unsupported algorithm " + policyAttribute.getAttributeValue().trim() + " specified. Please use the 'algorithm' attribute to specify the " + "correct loadbalance algorithm implementation.";
            // We cannot continue execution. Hence it is logged at fatal level
            log.fatal(msg);
            throw new SynapseException(msg);
        }
    }
    return algorithm;
}
Also used : SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) LoadbalanceAlgorithm(org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm) RoundRobin(org.apache.synapse.endpoints.algorithms.RoundRobin) OMAttribute(org.apache.axiom.om.OMAttribute) SynapseException(org.apache.synapse.SynapseException)

Aggregations

QName (javax.xml.namespace.QName)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 SynapseException (org.apache.synapse.SynapseException)1 LoadbalanceAlgorithm (org.apache.synapse.endpoints.algorithms.LoadbalanceAlgorithm)1 RoundRobin (org.apache.synapse.endpoints.algorithms.RoundRobin)1