Search in sources :

Example 56 with Endpoint

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

the class SALSessions method getEndpoints.

/*
     * Helper method to get a list of endpoints from a list of endpoint name maps -
     * This is for clustered env.
     */
private List<Endpoint> getEndpoints(List<String> endpointNames, String root) {
    if (endpointNames == null || endpointNames.isEmpty()) {
        handleException("Invalid session - path cannot be null.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving endpoint sequence for path " + endpointNames);
    }
    List<Endpoint> endpoints = new ArrayList<Endpoint>();
    // to each time calculate
    if (namesToEndpointsMap.containsKey(endpointNames)) {
        endpoints.addAll(namesToEndpointsMap.get(endpointNames));
        return endpoints;
    }
    Map<String, Endpoint> map = childEndpoints.get(root);
    assert endpointNames != null;
    for (String endpointName : endpointNames) {
        Endpoint endpoint = null;
        if (map != null) {
            endpoint = map.get(endpointName);
            if (endpoint == null || endpoints.contains(endpoint)) {
                map = childEndpoints.get(endpointName);
                if (map != null) {
                    endpoint = map.get(endpointName);
                }
            }
        }
        if (endpoint == null) {
            handleException("Invalid session. Endpoint with name '" + endpointName + "' cannot found");
        }
        endpoints.add(endpoint);
    }
    // cache path(endpoint names) vs endpoint (instance) sequence
    namesToEndpointsMap.put(endpointNames, endpoints);
    return endpoints;
}
Also used : SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) DynamicLoadbalanceEndpoint(org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint)

Example 57 with Endpoint

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

the class SALSessions method createSessionInformation.

/*
     * Factory method to create a session information using given endpoint list, list of paths
     * session id and other informations
     */
private SessionInformation createSessionInformation(MessageContext synCtx, String id, List<Endpoint> endpoints, List<String> paths) {
    if (endpoints == null || endpoints.isEmpty()) {
        handleException("Invalid request to create sessions . Cannot find a endpoint sequence.");
    }
    if (log.isDebugEnabled()) {
        log.debug("Creating a session information for given session id  " + id + " with endpoint sequence " + endpoints);
    }
    long expireTimeWindow = -1;
    assert endpoints != null;
    for (Endpoint endpoint : endpoints) {
        if (endpoint instanceof SALoadbalanceEndpoint) {
            long sessionsTimeout = ((SALoadbalanceEndpoint) endpoint).getSessionTimeout();
            if (expireTimeWindow == -1) {
                expireTimeWindow = sessionsTimeout;
            } else if (expireTimeWindow > sessionsTimeout) {
                expireTimeWindow = sessionsTimeout;
            }
        }
    }
    if (expireTimeWindow == -1) {
        expireTimeWindow = synCtx.getConfiguration().getProperty(SynapseConstants.PROP_SAL_ENDPOINT_DEFAULT_SESSION_TIMEOUT, SynapseConstants.SAL_ENDPOINTS_DEFAULT_SESSION_TIMEOUT);
    }
    if (log.isDebugEnabled()) {
        log.debug("For session with id " + id + " : expiry time interval : " + expireTimeWindow);
    }
    long expiryTime = System.currentTimeMillis() + expireTimeWindow;
    Endpoint rootEndpoint = endpoints.get(0);
    SessionInformation information = new SessionInformation(id, endpoints, expiryTime);
    information.setPath(paths);
    if (isClustered) {
        information.setRootEndpointName(getEndpointName(rootEndpoint));
    }
    return information;
}
Also used : SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint) IndirectEndpoint(org.apache.synapse.endpoints.IndirectEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) DynamicLoadbalanceEndpoint(org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint) SALoadbalanceEndpoint(org.apache.synapse.endpoints.SALoadbalanceEndpoint)

Example 58 with Endpoint

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

the class WeightedRoundRobin method getNextEndpoint.

public Endpoint getNextEndpoint(MessageContext synapseMessageContext, AlgorithmContext algorithmContext) {
    Lock readLock = lock.readLock();
    readLock.lock();
    try {
        if (!isThreadLocal) {
            synchronized (this) {
                EndpointState state = endpointStates[endpointCursor];
                if (state.getCurrentWeight() == 0) {
                    // reset the current state
                    state.reset();
                    // go to the next endpoint
                    if (endpointCursor == endpointStates.length - 1) {
                        endpointCursor = 0;
                    } else {
                        ++endpointCursor;
                    }
                    state = endpointStates[endpointCursor];
                }
                // we are about to use this endpoint, so decrement its current count
                state.decrementCurrentWeight();
                // return the endpoint corresponding to the current position
                return endpoints.get(state.getEndpointPosition());
            }
        } else {
            if (threadedAlgorithm != null) {
                Algorithm algo = threadedAlgorithm.get();
                int position = algo.getNextEndpoint();
                return endpoints.get(position);
            } else {
                String msg = "Algorithm: WeightedRoundRobin algorithm not initialized properly";
                log.error(msg);
                throw new SynapseException(msg);
            }
        }
    } finally {
        readLock.unlock();
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) Endpoint(org.apache.synapse.endpoints.Endpoint) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 59 with Endpoint

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

the class WeightedRRLCAlgorithmTest method createLoadBalanceEndpoint.

private LoadbalanceEndpoint createLoadBalanceEndpoint() {
    LoadbalanceEndpoint loadbalanceEndpoint = new LoadbalanceEndpoint();
    List<Endpoint> endpoints = createEndpoints();
    WeightedRRLCAlgorithm algorithm = new WeightedRRLCAlgorithm();
    MediatorProperty property = new MediatorProperty();
    property.setName(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_ROUNDS_PER_RECAL);
    property.setValue("2");
    loadbalanceEndpoint.addProperty(property);
    algorithm.setEndpoints(endpoints);
    algorithm.setLoadBalanceEndpoint(loadbalanceEndpoint);
    loadbalanceEndpoint.setChildren(endpoints);
    loadbalanceEndpoint.setAlgorithm(algorithm);
    SynapseEnvironment env = new Axis2SynapseEnvironment(new ConfigurationContext(new AxisConfiguration()), new SynapseConfiguration());
    loadbalanceEndpoint.init(env);
    return loadbalanceEndpoint;
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 60 with Endpoint

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

the class WeightedRRLCAlgorithmTest method createEndpoints.

private List<Endpoint> createEndpoints() {
    List<Endpoint> endpoints = new ArrayList<Endpoint>();
    for (int i = 0; i < hosts.length; i++) {
        AddressEndpoint addressEndpoint = new AddressEndpoint();
        EndpointDefinition definition = new EndpointDefinition();
        definition.setAddress("http://" + hosts[i] + "/");
        addressEndpoint.setDefinition(definition);
        MediatorProperty property = new MediatorProperty();
        property.setName(WeightedRRLCAlgorithm.LB_WEIGHTED_RRLC_WEIGHT);
        property.setValue(weights[i]);
        addressEndpoint.addProperty(property);
        endpoints.add(addressEndpoint);
    }
    return endpoints;
}
Also used : AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint) ArrayList(java.util.ArrayList) EndpointDefinition(org.apache.synapse.endpoints.EndpointDefinition) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) LoadbalanceEndpoint(org.apache.synapse.endpoints.LoadbalanceEndpoint)

Aggregations

Endpoint (org.apache.synapse.endpoints.Endpoint)64 OMElement (org.apache.axiom.om.OMElement)22 SynapseException (org.apache.synapse.SynapseException)13 AddressEndpoint (org.apache.synapse.endpoints.AddressEndpoint)13 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)11 ArrayList (java.util.ArrayList)10 MessageContext (org.apache.synapse.MessageContext)9 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)8 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)8 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)8 IndirectEndpoint (org.apache.synapse.endpoints.IndirectEndpoint)8 SALoadbalanceEndpoint (org.apache.synapse.endpoints.SALoadbalanceEndpoint)8 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)8 OMAttribute (org.apache.axiom.om.OMAttribute)7 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)7 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)7 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)7 QName (javax.xml.namespace.QName)6 AbstractEndpoint (org.apache.synapse.endpoints.AbstractEndpoint)6 DynamicLoadbalanceEndpoint (org.apache.synapse.endpoints.DynamicLoadbalanceEndpoint)6