1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.response;
import com.google.common.util.concurrent.ForwardingFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.yahoo.processing.Request;
import com.yahoo.processing.Response;
import com.yahoo.processing.execution.Execution;
import com.yahoo.processing.request.ErrorMessage;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A processing response which will arrive in the future.
*
* @author bratseth
*/
public class FutureResponse extends ForwardingFuture<Response> {
private final Request request;
/**
* Only used for generating messages
*/
private final Execution execution;
private final static Logger log = Logger.getLogger(FutureResponse.class.getName());
private final ListenableFutureTask<Response> futureTask;
public FutureResponse(final Callable<Response> callable, Execution execution, final Request request) {
this.futureTask = ListenableFutureTask.create(callable);
this.request = request;
this.execution = execution;
}
@Override
public ListenableFutureTask<Response> delegate() {
return futureTask;
}
public
@Override
Response get() {
try {
return super.get();
} catch (InterruptedException e) {
return new Response(request, new ErrorMessage("'" + execution + "' was interrupted", e));
} catch (ExecutionException e) {
log.log(Level.WARNING, "Exception on executing " + execution + " for " + request, e);
return new Response(request, new ErrorMessage("Error in '" + execution + "'", e));
}
}
public
@Override
Response get(long timeout, TimeUnit timeunit) {
try {
return super.get(timeout, timeunit);
} catch (InterruptedException e) {
return new Response(request, new ErrorMessage("'" + execution + "' was interrupted", e));
} catch (ExecutionException e) {
log.log(Level.WARNING, "Exception on executing " + execution + " for " + request, e);
return new Response(request, new ErrorMessage("Error in '" + execution + "'", e));
} catch (TimeoutException e) {
return new Response(request, new ErrorMessage("Error executing '" + execution + "': " + " Chain timed out."));
}
}
/**
* Returns the query used in this execution, never null
*/
public Request getRequest() {
return request;
}
}
|