Skip to content

Commit a2cd1d7

Browse files
committed
Merge branch 'master' of github.com:AsyncHttpClient/async-http-client
2 parents f4768d0 + 92010b5 commit a2cd1d7

15 files changed

+422
-85
lines changed

src/main/java/com/ning/http/client/AsyncHttpClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ private BoundRequestBuilder(Request prototype) {
223223
super(BoundRequestBuilder.class, prototype);
224224
}
225225

226-
public <T> Future<T> execute(AsyncHandler<T> handler) throws IOException {
226+
public <T> ListenableFuture<T> execute(AsyncHandler<T> handler) throws IOException {
227227
return AsyncHttpClient.this.executeRequest(build(), handler);
228228
}
229229

230-
public Future<Response> execute() throws IOException {
230+
public ListenableFuture<Response> execute() throws IOException {
231231
return AsyncHttpClient.this.executeRequest(build(), new AsyncCompletionHandlerBase());
232232
}
233233

@@ -468,7 +468,7 @@ public BoundRequestBuilder prepareRequest(Request request) {
468468
* @return a {@link Future} of type T
469469
* @throws IOException
470470
*/
471-
public <T> Future<T> executeRequest(Request request, AsyncHandler<T> handler) throws IOException {
471+
public <T> ListenableFuture<T> executeRequest(Request request, AsyncHandler<T> handler) throws IOException {
472472

473473
FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(handler).request(request).build();
474474
fc = preProcessRequest(fc);
@@ -482,7 +482,7 @@ public <T> Future<T> executeRequest(Request request, AsyncHandler<T> handler) th
482482
* @return a {@link Future} of type Response
483483
* @throws IOException
484484
*/
485-
public Future<Response> executeRequest(Request request) throws IOException {
485+
public ListenableFuture<Response> executeRequest(Request request) throws IOException {
486486
FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(new AsyncCompletionHandlerBase()).request(request).build();
487487
fc = preProcessRequest(fc);
488488
return httpProvider.execute(fc.getRequest(), fc.getAsyncHandler());

src/main/java/com/ning/http/client/AsyncHttpProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public interface AsyncHttpProvider<A> {
2929
* Execute the request and invoke the {@link AsyncHandler} when the response arrive.
3030
*
3131
* @param handler an instance of {@link AsyncHandler}
32-
* @return a {@link java.util.concurrent.Future} of Type T.
32+
* @return a {@link ListenableFuture} of Type T.
3333
* @throws IOException
3434
*/
35-
public <T> Future<T> execute(Request request, AsyncHandler<T> handler) throws IOException;
35+
public <T> ListenableFuture<T> execute(Request request, AsyncHandler<T> handler) throws IOException;
3636

3737
/**
3838
* Close the current underlying TCP/HTTP connection.

src/main/java/com/ning/http/client/FutureImpl.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
/*
17+
* Copyright (C) 2007 Google Inc.
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
package com.ning.http.client;
32+
33+
import java.util.concurrent.Callable;
34+
import java.util.concurrent.Executor;
35+
import java.util.concurrent.Future;
36+
37+
/**
38+
* Extended {@link Future}
39+
*
40+
* @param <V> Type of the value that will be returned.
41+
*/
42+
public interface ListenableFuture<V> extends Future<V> {
43+
44+
/**
45+
* Execute a {@link Callable} and if there is no exception, mark this Future as done and release the internal lock.
46+
*
47+
* @param callable
48+
*/
49+
void done(Callable callable);
50+
51+
/**
52+
* Abort the current processing, and propagate the {@link Throwable} to the {@link AsyncHandler} or {@link Future}
53+
*
54+
* @param t
55+
*/
56+
void abort(Throwable t);
57+
58+
/**
59+
* Set the content that will be returned by this instance
60+
*
61+
* @param v the content that will be returned by this instance
62+
*/
63+
void content(V v);
64+
65+
/**
66+
* Touch the current instance to prevent external service to times out.
67+
*/
68+
void touch();
69+
70+
/**
71+
* Write the {@link Request} headers
72+
*/
73+
boolean getAndSetWriteHeaders(boolean writeHeader);
74+
75+
/**
76+
* Write the {@link Request} body
77+
*/
78+
boolean getAndSetWriteBody(boolean writeBody);
79+
80+
/**
81+
* <p>Adds a listener and executor to the ListenableFuture.
82+
* The listener will be {@linkplain java.util.concurrent.Executor#execute(Runnable) passed
83+
* to the executor} for execution when the {@code Future}'s computation is
84+
* {@linkplain Future#isDone() complete}.
85+
* <p/>
86+
* <p>There is no guaranteed ordering of execution of listeners, they may get
87+
* called in the order they were added and they may get called out of order,
88+
* but any listener added through this method is guaranteed to be called once
89+
* the computation is complete.
90+
*
91+
* @param listener the listener to run when the computation is complete.
92+
* @param exec the executor to run the listener in.
93+
* @return this Future
94+
* @throws NullPointerException if the executor or listener was null.
95+
* @throws java.util.concurrent.RejectedExecutionException
96+
* if we tried to execute the listener
97+
* immediately but the executor rejected it.
98+
*/
99+
ListenableFuture<V> addListener(Runnable listener, Executor exec);
100+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
/*
14+
* Copyright (C) 2007 Google Inc.
15+
*
16+
* Licensed under the Apache License, Version 2.0 (the "License");
17+
* you may not use this file except in compliance with the License.
18+
* You may obtain a copy of the License at
19+
*
20+
* http://www.apache.org/licenses/LICENSE-2.0
21+
*
22+
* Unless required by applicable law or agreed to in writing, software
23+
* distributed under the License is distributed on an "AS IS" BASIS,
24+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
* See the License for the specific language governing permissions and
26+
* limitations under the License.
27+
*/
28+
29+
package com.ning.http.client.listenable;
30+
31+
import com.ning.http.client.ListenableFuture;
32+
33+
import java.util.concurrent.Executor;
34+
35+
/**
36+
* <p>An abstract base implementation of the listener support provided by
37+
* {@link ListenableFuture}. This class uses an {@link ExecutionList} to
38+
* guarantee that all registered listeners will be executed. Listener/Executor
39+
* pairs are stored in the execution list and executed in the order in which
40+
* they were added, but because of thread scheduling issues there is no
41+
* guarantee that the JVM will execute them in order. In addition, listeners
42+
* added after the task is complete will be executed immediately, even if some
43+
* previously added listeners have not yet been executed.
44+
*
45+
* @author Sven Mawson
46+
* @since 1
47+
*/
48+
public abstract class AbstractListenableFuture<V> implements ListenableFuture<V> {
49+
50+
// The execution list to hold our executors.
51+
private final ExecutionList executionList = new ExecutionList();
52+
53+
/*
54+
* Adds a listener/executor pair to execution list to execute when this task
55+
* is completed.
56+
*/
57+
58+
public ListenableFuture<V> addListener(Runnable listener, Executor exec) {
59+
executionList.add(listener, exec);
60+
return this;
61+
}
62+
63+
/*
64+
* Override the done method to execute the execution list.
65+
*/
66+
67+
protected void done() {
68+
executionList.run();
69+
}
70+
}

0 commit comments

Comments
 (0)