简体   繁体   中英

Is there a way to understand if a Job<Void> completed successfully?

In Play, it seems that if an exception is thrown inside a Job<T> , the exception does not propagate outward.

If I understand correctly, in order to know if the inner code throws an exception, I must revert to using boolean return values ( a known anti pattern )? Or am I missing something?

Here is a code sample that does not throw anything, but rather renders the todo page - and I would like to know how to know the inner job threw an exception from the outer controller method:

public static void testException() throws ExecutionException, InterruptedException {
    F.Promise<Void> result = new Job<Void>() {
        @Override
        public void doJob() throws Exception {
            Thread.sleep(1000);
            throw new RuntimeException("Foo");
        }
    }.now();
    await(result);
    result.get();
    todo();
}

我只是使用Job<Boolean>而在成功时返回true

I think await expects the method called inside to return Promise which is a Future (result will be captured when the execution is completed). Its async, so throwing an exception outward won't make much sense.

If you just do a get() on the result, it would give you any exceptions there were (it will throw). See code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM