Search in sources :

Example 6 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,
     * session id and other informations
     */
private SessionInformation createSessionInformation(MessageContext synCtx, String id, List<Endpoint> endpoints) {
    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;
    if (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);
    if (isClustered) {
        List<String> epNameList = getEndpointNames(endpoints);
        information.setPath(epNameList);
        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 7 with Endpoint

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

the class RoundRobin method getNextEndpoint.

/**
 * Choose an active endpoint using the round robin algorithm. If there are no active endpoints
 * available, returns null.
 *
 * @param synCtx           MessageContext instance which holds all per-message properties
 * @param algorithmContext The context in which holds run time states related to the algorithm
 * @return endpoint to send the next message
 */
public Endpoint getNextEndpoint(MessageContext synCtx, AlgorithmContext algorithmContext) {
    Endpoint nextEndpoint;
    int attempts = 0;
    synchronized (algorithmContext) {
        int currentEPR = algorithmContext.getCurrentEndpointIndex();
        do {
            // two successive clients could get the same endpoint if not synchronized.
            nextEndpoint = (Endpoint) endpoints.get(currentEPR);
            if (currentEPR == endpoints.size() - 1) {
                currentEPR = 0;
            } else {
                currentEPR++;
            }
            algorithmContext.setCurrentEndpointIndex(currentEPR);
            attempts++;
            if (attempts > endpoints.size()) {
                return null;
            }
        } while (!nextEndpoint.readyToSend());
    }
    return nextEndpoint;
}
Also used : Endpoint(org.apache.synapse.endpoints.Endpoint) Endpoint(org.apache.synapse.endpoints.Endpoint)

Example 8 with Endpoint

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

the class WeightedRRLCAlgorithm method intialize.

/**
 * Initialize the algorithm reading the configurations from the endpoints.
 */
private void intialize() {
    // get the global properties
    if (loadBalanceEndpoint != null && loadBalanceEndpoint instanceof PropertyInclude) {
        PropertyInclude include = (PropertyInclude) loadBalanceEndpoint;
        MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_ROUNDS_PER_RECAL);
        if (val != null) {
            roundsPerRecalculation = Integer.parseInt(val.getValue());
        }
    }
    // initialize the states list, this runs only once
    list = new WeightedState[endpoints.size()];
    int totalWeight = 0;
    for (Endpoint endpoint : endpoints) {
        if (endpoint instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) endpoint;
            MediatorProperty val = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            if (val == null) {
                String msg = "Parameter " + "loadbalance.weighted.weight should be specified for every " + "endpoint in the load balance group";
                log.error(msg);
                throw new SynapseException(msg);
            }
            totalWeight += Integer.parseInt(val.getValue());
        }
    }
    this.totalWeight = totalWeight;
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint e = endpoints.get(i);
        if (e instanceof PropertyInclude) {
            PropertyInclude include = (PropertyInclude) e;
            MediatorProperty weight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT);
            String key;
            URL url;
            if (e instanceof AddressEndpoint) {
                AddressEndpoint addressEndpoint = (AddressEndpoint) e;
                try {
                    url = new URL(addressEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else if (e instanceof WSDLEndpoint) {
                WSDLEndpoint wsdlEndpoint = (WSDLEndpoint) e;
                try {
                    url = new URL(wsdlEndpoint.getDefinition().getAddress());
                } catch (MalformedURLException e1) {
                    String msg = "Mulformed URL in address endpoint";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
            } else {
                String msg = "Only AddressEndpoint and WSDLEndpoint can be used " + "with WeightedRRLCAlgorithm";
                log.error(msg);
                throw new SynapseException(msg);
            }
            // construct the key
            key = url.getHost() + ":" + url.getPort();
            WeightedState state = new WeightedState(Integer.parseInt(weight.getValue()), i, key);
            MediatorProperty minimumWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MIN);
            if (minimumWeight != null) {
                state.setMinWeight(Integer.parseInt(minimumWeight.getValue()));
            }
            MediatorProperty maxWeight = include.getProperty(LB_WEIGHTED_RRLC_WEIGHT_MAX);
            if (maxWeight != null) {
                state.setMaxWeight(Integer.parseInt(maxWeight.getValue()));
            }
            list[i] = state;
        }
    }
    // sort the states according to the initial fixed weights
    Arrays.sort(list, new Comparator<WeightedState>() {

        public int compare(WeightedState o1, WeightedState o2) {
            return o2.getFixedWeight() - o1.getFixedWeight();
        }
    });
}
Also used : WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) MalformedURLException(java.net.MalformedURLException) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) SynapseException(org.apache.synapse.SynapseException) PropertyInclude(org.apache.synapse.PropertyInclude) WSDLEndpoint(org.apache.synapse.endpoints.WSDLEndpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) URL(java.net.URL)

Example 9 with Endpoint

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

the class WeightedRoundRobin method init.

public void init(SynapseEnvironment se) {
    if (endpoints == null) {
        String msg = "Endpoints are not set, cannot initialize the algorithm";
        log.error(msg);
        throw new SynapseException(msg);
    }
    endpointStates = new EndpointState[endpoints.size()];
    for (int i = 0; i < endpoints.size(); i++) {
        Endpoint endpoint = endpoints.get(i);
        if (!(endpoint instanceof PropertyInclude)) {
            EndpointState state = new EndpointState(i, DEFAULT_WEIGHT);
            endpointStates[i] = state;
        } else {
            MediatorProperty property = ((PropertyInclude) endpoint).getProperty(LOADBALANCE_WEIGHT);
            EndpointState state;
            if (property != null) {
                int weight = Integer.parseInt(property.getValue());
                if (weight <= 0) {
                    String msg = "Weight must be greater than zero";
                    log.error(msg);
                    throw new SynapseException(msg);
                }
                state = new EndpointState(i, weight);
            } else {
                state = new EndpointState(i, DEFAULT_WEIGHT);
            }
            endpointStates[i] = state;
        }
    }
    if (loadBalanceEndpoint instanceof PropertyInclude) {
        MediatorProperty threadLocalProperty = ((PropertyInclude) loadBalanceEndpoint).getProperty(LOADBALANCE_ThEADLOCAL);
        if (threadLocalProperty != null && threadLocalProperty.getValue().equals("true")) {
            isThreadLocal = true;
        }
    }
    view = new WeightedRoundRobinView(this);
    MBeanRegistrar.getInstance().registerMBean(view, "LBAlgorithms", loadBalanceEndpoint.getName() != null ? loadBalanceEndpoint.getName() : "LBEpr");
}
Also used : MediatorProperty(org.apache.synapse.mediators.MediatorProperty) SynapseException(org.apache.synapse.SynapseException) Endpoint(org.apache.synapse.endpoints.Endpoint) PropertyInclude(org.apache.synapse.PropertyInclude) Endpoint(org.apache.synapse.endpoints.Endpoint)

Example 10 with Endpoint

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

the class DynamicResourceTest method testDynamicEndpointLookup.

public void testDynamicEndpointLookup() throws Exception {
    System.out.println("Testing dynamic endpoint lookup...");
    // Phase 1
    System.out.println("Testing basic registry lookup functionality...");
    MessageContext synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep1 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep1);
    assertTrue(ep1.isInitialized());
    assertEquals(1, registry.getHitCount());
    assertEquals("http://test.url", ((AddressEndpoint) ep1).getDefinition().getAddress());
    // Phase 2
    System.out.println("Testing basic endpoint caching...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep2 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep2);
    assertEquals(1, registry.getHitCount());
    assertTrue(ep1 == ep2);
    // Phase 3
    System.out.println("Testing advanced endpoint caching...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    Endpoint ep3 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep3);
    assertEquals(1, registry.getHitCount());
    assertTrue(ep1 == ep3);
    // Phase 4
    System.out.println("Testing endpoint reloading...");
    registry.updateResource(KEY_DYNAMIC_ENDPOINT_1, TestUtils.createOMElement(DYNAMIC_ENDPOINT_2));
    System.out.println("Waiting for the cache to expire...");
    Thread.sleep(8500L);
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep4 = synCtx.getEndpoint(KEY_DYNAMIC_ENDPOINT_1);
    assertNotNull(ep4);
    assertTrue(ep4.isInitialized());
    assertEquals(2, registry.getHitCount());
    assertEquals("http://test2.url", ((AddressEndpoint) ep4).getDefinition().getAddress());
    assertTrue(ep1 != ep4);
    assertTrue(!ep1.isInitialized());
    // Phase 5
    System.out.println("Testing for non-existing endpoints...");
    synCtx = TestUtils.createSynapseMessageContext("<empty/>", config);
    Endpoint ep5 = synCtx.getEndpoint("non-existing-endpoint");
    assertNull(ep5);
    System.out.println("Dynamic endpoint lookup tests were successful...");
}
Also used : AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) Endpoint(org.apache.synapse.endpoints.Endpoint) AddressEndpoint(org.apache.synapse.endpoints.AddressEndpoint) MessageContext(org.apache.synapse.MessageContext)

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