Search in sources :

Example 1 with DefaultRetryCommandFactory

use of com.ctrip.xpipe.command.DefaultRetryCommandFactory in project x-pipe by ctripcorp.

the class AdvancedDcMetaService method initService.

@PostConstruct
public void initService() {
    executors = DefaultExecutorFactory.createAllowCoreTimeout("OptimizedDcMetaService", OsUtils.defaultMaxCoreThreadCount()).createExecutorService();
    int retryTimes = 3, retryDelayMilli = 5;
    factory = new DefaultRetryCommandFactory(retryTimes, new RetryDelay(retryDelayMilli), scheduled);
}
Also used : DefaultRetryCommandFactory(com.ctrip.xpipe.command.DefaultRetryCommandFactory) RetryDelay(com.ctrip.xpipe.retry.RetryDelay) PostConstruct(javax.annotation.PostConstruct)

Example 2 with DefaultRetryCommandFactory

use of com.ctrip.xpipe.command.DefaultRetryCommandFactory in project x-pipe by ctripcorp.

the class AdvancedDcMetaServiceTest method testRetry3TimesUntilSuccess.

@Test
public void testRetry3TimesUntilSuccess() throws Exception {
    ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
    service.setScheduled(scheduled).setFactory(new DefaultRetryCommandFactory(3, new RetryDelay(10), scheduled));
    Command<String> command = service.retry3TimesUntilSuccess(new AbstractCommand<String>() {

        private AtomicInteger counter = new AtomicInteger(0);

        @Override
        protected void doExecute() throws Exception {
            int currentCount = counter.getAndIncrement();
            logger.info(String.format("Run %d time", currentCount));
            if (currentCount > 1) {
                future().setSuccess("success");
            } else {
                throw new XpipeRuntimeException("test exception");
            }
        }

        @Override
        protected void doReset() {
        }

        @Override
        public String getName() {
            return "test-retry";
        }
    });
    AtomicBoolean complete = new AtomicBoolean(false);
    command.future().addListener(commandFuture -> {
        Assert.assertEquals("success", commandFuture.getNow());
        complete.getAndSet(true);
    });
    command.execute();
    waitConditionUntilTimeOut(() -> complete.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DefaultRetryCommandFactory(com.ctrip.xpipe.command.DefaultRetryCommandFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryDelay(com.ctrip.xpipe.retry.RetryDelay) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) IOException(java.io.IOException) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) AbstractConsoleIntegrationTest(com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest) Test(org.junit.Test)

Aggregations

DefaultRetryCommandFactory (com.ctrip.xpipe.command.DefaultRetryCommandFactory)2 RetryDelay (com.ctrip.xpipe.retry.RetryDelay)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 AbstractConsoleIntegrationTest (com.ctrip.xpipe.redis.console.AbstractConsoleIntegrationTest)1 IOException (java.io.IOException)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PostConstruct (javax.annotation.PostConstruct)1 Test (org.junit.Test)1