Search in sources :

Example 1 with CircuitBreakerFailoverFeature

use of org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature in project cxf by apache.

the class CircuitBreakerFailoverTest method testSequentialStrategyWithElapsingCircuitBreakerTimeout.

@Test
public void testSequentialStrategyWithElapsingCircuitBreakerTimeout() throws Throwable {
    FailoverFeature feature = customizeFeature(new CircuitBreakerFailoverFeature(1, 3000), false, "http://localhost:" + NON_PORT + "/non-existent", "http://localhost:" + NON_PORT + "/non-existent2");
    final BookStore bookStore = getBookStore("http://localhost:" + NON_PORT + "/non-existent", feature);
    // should reset all circuit breakers and the URLs could be tried again.
    for (int i = 0; i < 2; ++i) {
        try {
            bookStore.getBook(1);
            fail("Exception expected");
        } catch (ProcessingException ex) {
            if (!(ex.getCause() instanceof IOException)) {
                throw ex.getCause();
            }
        }
        // Let's wait a bit more than circuit breaker timeout
        Thread.sleep(4000);
    }
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) FailoverFeature(org.apache.cxf.clustering.FailoverFeature) CircuitBreakerFailoverFeature(org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature) CircuitBreakerFailoverFeature(org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 2 with CircuitBreakerFailoverFeature

use of org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature in project testcases by coheigea.

the class FailoverTest method testCircuitBreaker.

@org.junit.Test
public void testCircuitBreaker() throws Exception {
    URL busFile = FailoverTest.class.getResource("cxf-client.xml");
    CircuitBreakerFailoverFeature feature = new CircuitBreakerFailoverFeature();
    feature.setThreshold(2);
    SequentialStrategy strategy = new SequentialStrategy();
    strategy.setDelayBetweenRetries(3000L);
    List<String> addresses = new ArrayList<>();
    addresses.add("http://localhost:" + PORT2 + "/doubleit/services");
    strategy.setAlternateAddresses(addresses);
    feature.setStrategy(strategy);
    String address = "http://localhost:" + PORT1 + "/doubleit/services";
    WebClient client = WebClient.create(address, null, Collections.singletonList(feature), busFile.toString());
    client = client.type("application/xml");
    Number numberToDouble = new Number();
    numberToDouble.setDescription("This is the number to double");
    numberToDouble.setNumber(25);
    // First call is successful to PORT1
    Response response = client.post(numberToDouble);
    assertEquals(response.getStatus(), 200);
    assertEquals(response.readEntity(Number.class).getNumber(), 50);
    // Second call fails over to PORT2
    response = client.post(numberToDouble);
    assertEquals(response.getStatus(), 200);
    assertEquals(response.readEntity(Number.class).getNumber(), 50);
}
Also used : Response(javax.ws.rs.core.Response) SequentialStrategy(org.apache.cxf.clustering.SequentialStrategy) Number(org.apache.coheigea.cxf.failover.common.Number) CircuitBreakerFailoverFeature(org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Aggregations

CircuitBreakerFailoverFeature (org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 ProcessingException (javax.ws.rs.ProcessingException)1 Response (javax.ws.rs.core.Response)1 Number (org.apache.coheigea.cxf.failover.common.Number)1 FailoverFeature (org.apache.cxf.clustering.FailoverFeature)1 SequentialStrategy (org.apache.cxf.clustering.SequentialStrategy)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 BookStore (org.apache.cxf.systest.jaxrs.BookStore)1 Test (org.junit.Test)1