Search in sources :

Example 66 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project rest.li by linkedin.

the class PropertyEventBusImplTest method getBus.

@Override
public PropertyEventBus<String> getBus() {
    // TODO rewrite tests in the parent class so they work with either sync or async, and
    // test both modes of operation.
    ScheduledExecutorService executorService = new SynchronousExecutorService();
    PropertyEventPublisher<String> publisher = new MockStore<String>();
    PropertyEventBus<String> bus = new PropertyEventBusImpl<String>(executorService, publisher);
    return bus;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore)

Example 67 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project rest.li by linkedin.

the class BigDataClientExample method main.

public static void main(String[] args) throws Exception {
    stats.put("normalClient", new AtomicInteger());
    stats.put("overrideClient", new AtomicInteger());
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
    Map<String, Map<String, Object>> clientServicesConfig = (Map<String, Map<String, Object>>) json.get("clientServicesConfig");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    System.out.println("Finished parsing client config");
    D2ClientBuilder builder = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath);
    final D2Client normalD2Client = builder.build();
    final D2Client overrideD2Client = builder.setClientServicesConfig(clientServicesConfig).build();
    System.out.println("Finished creating d2 clients, starting d2 clients...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(2);
    //start both d2 clients
    startClients(normalD2Client, overrideD2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 clients are sending traffic to 'compute' service.");
    System.out.println("NormalClient will fail because the timeout is 5 seconds.");
    System.out.println("The server responds to our request in 10 seconds.");
    System.out.println("OverrideClient will succeed because we override the timeout to 10 seconds");
    ScheduledFuture normalTask = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, normalD2Client, "Normal d2 client");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    ScheduledFuture overrideTask = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, overrideD2Client, "Override d2 client");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to shutdown");
    System.out.println("===========================================================\n\n");
    System.in.read();
    normalTask.cancel(false);
    overrideTask.cancel(false);
    System.out.println("Shutting down... please wait 15 seconds.");
    shutdown(normalD2Client, overrideD2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONObject(org.json.simple.JSONObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) None(com.linkedin.common.util.None)

Example 68 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project rest.li by linkedin.

the class ExampleD2Client method main.

public static void main(String[] args) throws IOException, ParseException, InterruptedException {
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    Long rate = (Long) json.get("rateMillisecond");
    System.out.println("Finished parsing client config");
    //create d2 client
    final D2Client d2Client = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath).build();
    System.out.println("Finished creating d2 client, starting d2 client...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    //start d2 client by connecting to zookeeper
    startClient(d2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 client is sending traffic");
    ScheduledFuture task = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, d2Client);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }, 0, rate, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to stop D2 client...");
    System.in.read();
    task.cancel(false);
    System.out.println("Shutting down...");
    shutdown(d2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) URISyntaxException(java.net.URISyntaxException) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) None(com.linkedin.common.util.None)

Example 69 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue5 method test.

/**
   * Asserts that a failure is handled as expected by a listener registered via whenFailure.
   */
public void test() throws Throwable {
    Waiter waiter = new Waiter();
    RetryPolicy retryPolicy = new RetryPolicy().withDelay(100, TimeUnit.MILLISECONDS).withMaxDuration(2, TimeUnit.SECONDS).withMaxRetries(3).retryWhen(null);
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    Failsafe.with(retryPolicy).with(executor).onFailure((result, failure) -> {
        waiter.assertNull(result);
        waiter.assertNull(failure);
        waiter.resume();
    }).get(() -> null);
    waiter.await(1000);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Waiter(net.jodah.concurrentunit.Waiter) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test) RetryPolicy(net.jodah.failsafe.RetryPolicy) Executors(java.util.concurrent.Executors) Failsafe(net.jodah.failsafe.Failsafe) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Waiter(net.jodah.concurrentunit.Waiter) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 70 with ScheduledExecutorService

use of java.util.concurrent.ScheduledExecutorService in project failsafe by jhalterman.

the class Issue52 method shouldCancelExecutionViaFuture.

public void shouldCancelExecutionViaFuture() throws Throwable {
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
    FailsafeFuture<String> proxyFuture = Failsafe.with(new RetryPolicy().withDelay(10, TimeUnit.MILLISECONDS)).with(scheduler).get(exec -> {
        throw new IllegalStateException();
    });
    Thread.sleep(100);
    proxyFuture.cancel(true);
    assertNull(proxyFuture.get());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Aggregations

ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)332 Test (org.junit.Test)99 ExecutorService (java.util.concurrent.ExecutorService)41 Test (org.testng.annotations.Test)35 CountDownLatch (java.util.concurrent.CountDownLatch)33 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)31 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)31 HashMap (java.util.HashMap)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Map (java.util.Map)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)25 List (java.util.List)19 ThreadFactory (java.util.concurrent.ThreadFactory)16 None (com.linkedin.common.util.None)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 UUID (java.util.UUID)15 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)15 URI (java.net.URI)14 CompletableFuture (java.util.concurrent.CompletableFuture)14