Java Code Examples for scala.concurrent.Future#onComplete()

The following examples show how to use scala.concurrent.Future#onComplete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FutureUtils.java    From Flink-CEPplus with Apache License 2.0 7 votes vote down vote up
/**
 * Converts a Scala {@link Future} to a {@link CompletableFuture}.
 *
 * @param scalaFuture to convert to a Java 8 CompletableFuture
 * @param <T> type of the future value
 * @param <U> type of the original future
 * @return Java 8 CompletableFuture
 */
public static <T, U extends T> CompletableFuture<T> toJava(Future<U> scalaFuture) {
	final CompletableFuture<T> result = new CompletableFuture<>();

	scalaFuture.onComplete(new OnComplete<U>() {
		@Override
		public void onComplete(Throwable failure, U success) {
			if (failure != null) {
				result.completeExceptionally(failure);
			} else {
				result.complete(success);
			}
		}
	}, Executors.directExecutionContext());

	return result;
}
 
Example 2
Source File: FutureUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a Scala {@link Future} to a {@link CompletableFuture}.
 *
 * @param scalaFuture to convert to a Java 8 CompletableFuture
 * @param <T> type of the future value
 * @param <U> type of the original future
 * @return Java 8 CompletableFuture
 */
public static <T, U extends T> CompletableFuture<T> toJava(Future<U> scalaFuture) {
	final CompletableFuture<T> result = new CompletableFuture<>();

	scalaFuture.onComplete(new OnComplete<U>() {
		@Override
		public void onComplete(Throwable failure, U success) {
			if (failure != null) {
				result.completeExceptionally(failure);
			} else {
				result.complete(success);
			}
		}
	}, Executors.directExecutionContext());

	return result;
}
 
Example 3
Source File: ExecutorTest.java    From Nickle-Scheduler with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecutor() {
    ActorRef server = system.actorOf(ServerActor.props());
    Timeout t = new Timeout(Duration.create(3, TimeUnit.SECONDS));
    //使用ask发送消息,actor处理完,必须有返回(超时时间5秒)
    try {
        Future<Object> ask = Patterns.ask(server, "123", t);
        ask.onComplete(new OnComplete<Object>() {
            @Override
            public void onComplete(Throwable throwable, Object o) throws Throwable {
                if (throwable != null) {
                    System.out.println("some thing wrong.{}" + throwable);
                } else {
                    System.out.println("success:" + o);
                }
            }
        }, system.dispatcher());
        System.out.println("执行完毕");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: FutureUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a Scala {@link Future} to a {@link CompletableFuture}.
 *
 * @param scalaFuture to convert to a Java 8 CompletableFuture
 * @param <T> type of the future value
 * @param <U> type of the original future
 * @return Java 8 CompletableFuture
 */
public static <T, U extends T> CompletableFuture<T> toJava(Future<U> scalaFuture) {
	final CompletableFuture<T> result = new CompletableFuture<>();

	scalaFuture.onComplete(new OnComplete<U>() {
		@Override
		public void onComplete(Throwable failure, U success) {
			if (failure != null) {
				result.completeExceptionally(failure);
			} else {
				result.complete(success);
			}
		}
	}, Executors.directExecutionContext());

	return result;
}
 
Example 5
Source File: Delegate.java    From Nickle-Scheduler with Apache License 2.0 4 votes vote down vote up
/**
 * 调取简单任务
 *
 * @param job
 * @param nickleSchedulerExecutorJobs
 */
public static void scheduleSimpleJob(NickleSchedulerJob job,
                                     List<NickleSchedulerExecutorJob> nickleSchedulerExecutorJobs,
                                     SqlSessionFactory sqlSessionFactory, ActorContext actorContext) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    NickleSchedulerExecutorMapper executorMapper = sqlSession.getMapper(NickleSchedulerExecutorMapper.class);
    NickleSchedulerRunJobMapper runJobMapper = sqlSession.getMapper(NickleSchedulerRunJobMapper.class);
    try {
        //简单任务目前采用轮询算法来保证任务高可用
        /**
         * @// TODO: 2019/5/12  应修改为随机散列提高性能
         */
        for (NickleSchedulerExecutorJob executorJob : nickleSchedulerExecutorJobs) {
            //获取到执行器ip和端口
            QueryWrapper<NickleSchedulerExecutor> schedulerExecutorQueryWrapper = new QueryWrapper<>();
            schedulerExecutorQueryWrapper.lambda().eq(NickleSchedulerExecutor::getExecutorName, executorJob.getExecutorName());
            NickleSchedulerExecutor nickleSchedulerExecutor = executorMapper.selectOne(schedulerExecutorQueryWrapper);
            if (nickleSchedulerExecutor == null) {
                log.error("关联表有执行器,但是执行器表没有对应执行器数据");
                continue;
            }
            //插入正在巡行列表
            NickleSchedulerRunJob nickleSchedulerRunJob = insertRunJob(sqlSession, nickleSchedulerExecutor.getExecutorId(), job);
            //拼接获取远程actor发送信息
            String executorDispatcherPath = String.format(AKKA_REMOTE_MODEL
                    , EXECUTOR_SYSTEM_NAME
                    , nickleSchedulerExecutor.getExecutorIp()
                    , nickleSchedulerExecutor.getExecutorPort()
                    , EXECUTOR_DISPATCHER_NAME);
            ActorSelection actorSelection = actorContext.actorSelection(executorDispatcherPath);
            ExecuteJobEvent executorStartEvent = new ExecuteJobEvent(job.getJobClassName(), nickleSchedulerRunJob.getId());
            log.info("开始调度简单job:{},目标主机路径:{}", job, executorDispatcherPath);
            log.info("executorStartEvent:{}", executorStartEvent);
            //需确保DispacherActor真实收到任务所以采用ask模式,超时时间设置500ms
            Timeout t = new Timeout(Duration.create(500, TimeUnit.MILLISECONDS));
            CountDownLatch countDownLatch = new CountDownLatch(1);
            Future<Object> ask = Patterns.ask(actorSelection, executorStartEvent, t);
            boolean[] registerSuccess = new boolean[1];
            registerSuccess[0] = false;
            ask.onComplete(new OnComplete<Object>() {
                @Override
                public void onComplete(Throwable throwable, Object o) throws Throwable {
                    if (throwable != null) {
                        log.error("执行器:{}连接超时,即将删除", nickleSchedulerExecutor);
                        //删除执行器
                        deleteExecutor(sqlSession, nickleSchedulerExecutor);
                        log.error("执行器:{}删除成功", nickleSchedulerExecutor);
                    } else {
                        registerSuccess[0] = true;
                    }
                    countDownLatch.countDown();
                }
            }, actorContext.dispatcher());
            //等待任务注册完毕
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (registerSuccess[0]) {
                log.info("job:{}调度成功", job);
                sqlSession.commit();
                return;
            } else {
                runJobMapper.deleteById(nickleSchedulerRunJob.getId());
                log.info("job:{}调度失败,执行器为:{}", job, nickleSchedulerExecutor);
            }
        }
        //执行到这里证明所有执行器都无法连接,将会把任务失败
        insertFailJob(job, NO_EXECUTOR, EXECUTOR_NOT_CONNECTED, sqlSession);
        sqlSession.commit();
    } finally {
        sqlSession.close();
    }
}