29
29
import java .io .IOException ;
30
30
import java .net .MalformedURLException ;
31
31
import java .net .UnknownHostException ;
32
+ import java .util .concurrent .atomic .AtomicBoolean ;
32
33
33
34
/**
34
35
* Internal class, representing the HttpRequest, done in asynchronous manner
@@ -39,16 +40,16 @@ public class AsyncHttpRequest implements Runnable {
39
40
private final HttpUriRequest request ;
40
41
private final ResponseHandlerInterface responseHandler ;
41
42
private int executionCount ;
42
- private boolean isCancelled ;
43
+ private final AtomicBoolean isCancelled = new AtomicBoolean () ;
43
44
private boolean cancelIsNotified ;
44
- private boolean isFinished ;
45
+ private volatile boolean isFinished ;
45
46
private boolean isRequestPreProcessed ;
46
47
47
48
public AsyncHttpRequest (AbstractHttpClient client , HttpContext context , HttpUriRequest request , ResponseHandlerInterface responseHandler ) {
48
- this .client = client ;
49
- this .context = context ;
50
- this .request = request ;
51
- this .responseHandler = responseHandler ;
49
+ this .client = Utils . notNull ( client , "client" ) ;
50
+ this .context = Utils . notNull ( context , "context" ) ;
51
+ this .request = Utils . notNull ( request , "request" ) ;
52
+ this .responseHandler = Utils . notNull ( responseHandler , "responseHandler" ) ;
52
53
}
53
54
54
55
/**
@@ -97,9 +98,7 @@ public void run() {
97
98
return ;
98
99
}
99
100
100
- if (responseHandler != null ) {
101
- responseHandler .sendStartMessage ();
102
- }
101
+ responseHandler .sendStartMessage ();
103
102
104
103
if (isCancelled ()) {
105
104
return ;
@@ -108,20 +107,18 @@ public void run() {
108
107
try {
109
108
makeRequestWithRetries ();
110
109
} catch (IOException e ) {
111
- if (!isCancelled () && responseHandler != null ) {
110
+ if (!isCancelled ()) {
112
111
responseHandler .sendFailureMessage (0 , null , null , e );
113
112
} else {
114
- Log .e ("AsyncHttpRequest" , "makeRequestWithRetries returned error, but handler is null " , e );
113
+ Log .e ("AsyncHttpRequest" , "makeRequestWithRetries returned error" , e );
115
114
}
116
115
}
117
116
118
117
if (isCancelled ()) {
119
118
return ;
120
119
}
121
120
122
- if (responseHandler != null ) {
123
- responseHandler .sendFinishMessage ();
124
- }
121
+ responseHandler .sendFinishMessage ();
125
122
126
123
if (isCancelled ()) {
127
124
return ;
@@ -150,7 +147,7 @@ private void makeRequest() throws IOException {
150
147
151
148
HttpResponse response = client .execute (request , context );
152
149
153
- if (isCancelled () || responseHandler == null ) {
150
+ if (isCancelled ()) {
154
151
return ;
155
152
}
156
153
@@ -201,7 +198,7 @@ private void makeRequestWithRetries() throws IOException {
201
198
cause = e ;
202
199
retry = retryHandler .retryRequest (cause , ++executionCount , context );
203
200
}
204
- if (retry && ( responseHandler != null ) ) {
201
+ if (retry ) {
205
202
responseHandler .sendRetryMessage (executionCount );
206
203
}
207
204
}
@@ -216,17 +213,17 @@ private void makeRequestWithRetries() throws IOException {
216
213
}
217
214
218
215
public boolean isCancelled () {
219
- if (isCancelled ) {
216
+ boolean cancelled = isCancelled .get ();
217
+ if (cancelled ) {
220
218
sendCancelNotification ();
221
219
}
222
- return isCancelled ;
220
+ return cancelled ;
223
221
}
224
222
225
223
private synchronized void sendCancelNotification () {
226
- if (!isFinished && isCancelled && !cancelIsNotified ) {
224
+ if (!isFinished && isCancelled . get () && !cancelIsNotified ) {
227
225
cancelIsNotified = true ;
228
- if (responseHandler != null )
229
- responseHandler .sendCancelMessage ();
226
+ responseHandler .sendCancelMessage ();
230
227
}
231
228
}
232
229
@@ -235,7 +232,7 @@ public boolean isDone() {
235
232
}
236
233
237
234
public boolean cancel (boolean mayInterruptIfRunning ) {
238
- isCancelled = true ;
235
+ isCancelled . set ( true ) ;
239
236
request .abort ();
240
237
return isCancelled ();
241
238
}
0 commit comments