Search in sources :

Example 6 with CommandFuture

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

the class AsyncSendEmailCommandTest method testAsyncSendEmailCommand.

@Test
public void testAsyncSendEmailCommand() throws Exception {
    when(client.sendEmail(any())).thenThrow(new XpipeException("test exception"));
    CtripPlatformEmailService.AsyncSendEmailCommand command = new CtripPlatformEmailService.AsyncSendEmailCommand(generateEmail());
    command.setClient(client);
    CommandFuture future = command.execute();
    Assert.assertFalse(future.isSuccess());
    Assert.assertEquals("test exception", future.cause().getMessage());
}
Also used : CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) XpipeException(com.ctrip.xpipe.exception.XpipeException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 7 with CommandFuture

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

the class AsyncSendEmailCommandTest method testAsyncSendEmailCommand2.

@Test
public void testAsyncSendEmailCommand2() throws Exception {
    SendEmailResponse response = new SendEmailResponse();
    response.setResultCode(1);
    when(client.sendEmail(any())).thenReturn(response);
    GetEmailStatusResponse getResponse = new GetEmailStatusResponse();
    getResponse.setResultCode(1);
    when(client.getEmailStatus(any())).thenReturn(getResponse);
    CtripPlatformEmailService.AsyncSendEmailCommand command = new CtripPlatformEmailService.AsyncSendEmailCommand(generateEmail());
    command.setClient(client);
    CommandFuture future = command.execute();
    Assert.assertTrue(future.isSuccess());
}
Also used : SendEmailResponse(com.ctrip.soa.platform.basesystem.emailservice.v1.SendEmailResponse) GetEmailStatusResponse(com.ctrip.soa.platform.basesystem.emailservice.v1.GetEmailStatusResponse) CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 8 with CommandFuture

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

the class ScheduleCommandWrapper method doExecute.

@Override
protected void doExecute() throws Exception {
    final ScheduledFuture<?> scheduleFuture = scheduled.schedule(new AbstractExceptionLogTask() {

        @Override
        protected void doRun() throws Exception {
            try {
                command.execute().addListener(new CommandFutureListener<V>() {

                    @Override
                    public void operationComplete(CommandFuture<V> commandFuture) throws Exception {
                        if (commandFuture.isSuccess()) {
                            future().setSuccess(commandFuture.get());
                        } else {
                            future().setFailure(ExceptionUtils.getRootCause(commandFuture.cause()));
                        }
                    }
                });
            } catch (Exception e) {
                future().setFailure(ExceptionUtils.getRootCause(e));
            }
        }
    }, time, timeUnit);
    future().addListener(new CommandFutureListener<V>() {

        @Override
        public void operationComplete(CommandFuture<V> commandFuture) throws Exception {
            if (commandFuture.isCancelled()) {
                logger.info("[command canceled][cancel execution]{}", time);
                command.future().cancel(true);
                scheduleFuture.cancel(false);
            }
        }
    });
}
Also used : CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) CommandFutureListener(com.ctrip.xpipe.api.command.CommandFutureListener) AbstractExceptionLogTask(com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)

Example 9 with CommandFuture

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

the class TransactionalCommand method doWork.

@SuppressWarnings("unchecked")
protected void doWork(final SimpleObjectPool<NettyClient> clientPool) {
    SequenceCommandChain chain = new SequenceCommandChain(false);
    for (RedisCommand currentCommand : commands) {
        OneTranscationCommand oneTranscationCommand = new OneTranscationCommand(clientPool, currentCommand, scheduled);
        chain.add(oneTranscationCommand);
    }
    chain.execute().addListener(new CommandFutureListener() {

        @Override
        public void operationComplete(CommandFuture commandFuture) throws Exception {
            if (!commandFuture.isSuccess()) {
                logger.error("[doWork][fail]", commandFuture.cause());
            }
            endTranscation(clientPool);
        }
    });
}
Also used : CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) CommandFutureListener(com.ctrip.xpipe.api.command.CommandFutureListener) AbstractRedisCommand(com.ctrip.xpipe.redis.core.protocal.cmd.AbstractRedisCommand) RedisCommand(com.ctrip.xpipe.redis.core.protocal.RedisCommand) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) ReturnObjectException(com.ctrip.xpipe.pool.ReturnObjectException) BorrowObjectException(com.ctrip.xpipe.pool.BorrowObjectException) SequenceCommandChain(com.ctrip.xpipe.command.SequenceCommandChain)

Example 10 with CommandFuture

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

the class RequestResponseCommandTest method testTimeout.

@Test
public void testTimeout() throws CommandExecutionException, InterruptedException {
    TestCommand testCommand = new TestCommand("sleep 5000\r\n", 1000, clientPool, scheduled, null);
    CommandFuture<String> future = testCommand.execute();
    final AtomicReference<CommandFuture<String>> listenerFuture = new AtomicReference<CommandFuture<String>>(null);
    final CountDownLatch latch = new CountDownLatch(1);
    future.addListener(new CommandFutureListener<String>() {

        @Override
        public void operationComplete(CommandFuture<String> commandFuture) throws Exception {
            try {
                listenerFuture.set(commandFuture);
            } finally {
                latch.countDown();
            }
        }
    });
    try {
        future.get();
        Assert.fail();
    } catch (InterruptedException e) {
        Assert.fail();
    } catch (ExecutionException e) {
        if (!(e.getCause() instanceof CommandTimeoutException)) {
            Assert.fail();
        }
    }
    latch.await();
    Assert.assertTrue(listenerFuture.get() != null);
}
Also used : CommandFuture(com.ctrip.xpipe.api.command.CommandFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) ExecutionException(java.util.concurrent.ExecutionException) BorrowObjectException(com.ctrip.xpipe.pool.BorrowObjectException) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) CommandTimeoutException(com.ctrip.xpipe.command.CommandTimeoutException) CommandExecutionException(com.ctrip.xpipe.command.CommandExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

CommandFuture (com.ctrip.xpipe.api.command.CommandFuture)10 AbstractTest (com.ctrip.xpipe.AbstractTest)5 CommandFutureListener (com.ctrip.xpipe.api.command.CommandFutureListener)5 Test (org.junit.Test)5 SendEmailResponse (com.ctrip.soa.platform.basesystem.emailservice.v1.SendEmailResponse)3 CommandExecutionException (com.ctrip.xpipe.command.CommandExecutionException)3 GetEmailStatusResponse (com.ctrip.soa.platform.basesystem.emailservice.v1.GetEmailStatusResponse)2 SequenceCommandChain (com.ctrip.xpipe.command.SequenceCommandChain)2 XpipeException (com.ctrip.xpipe.exception.XpipeException)2 BorrowObjectException (com.ctrip.xpipe.pool.BorrowObjectException)2 IOException (java.io.IOException)2 CommandTimeoutException (com.ctrip.xpipe.command.CommandTimeoutException)1 AbstractExceptionLogTask (com.ctrip.xpipe.concurrent.AbstractExceptionLogTask)1 DefaultNettyClient (com.ctrip.xpipe.netty.commands.DefaultNettyClient)1 NettyClient (com.ctrip.xpipe.netty.commands.NettyClient)1 ReturnObjectException (com.ctrip.xpipe.pool.ReturnObjectException)1 RedisCommand (com.ctrip.xpipe.redis.core.protocal.RedisCommand)1 AbstractRedisCommand (com.ctrip.xpipe.redis.core.protocal.cmd.AbstractRedisCommand)1 Replconf (com.ctrip.xpipe.redis.core.protocal.cmd.Replconf)1 CountDownLatch (java.util.concurrent.CountDownLatch)1