Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit178ec0d

Browse files
author
oleksiys
committed
+ rework the cleanup process.
related to theAsyncHttpClient#1133
1 parentf720f33 commit178ec0d

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

‎src/main/java/com/ning/http/client/providers/grizzly/HttpTransactionContext.java‎

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
importcom.ning.http.util.AsyncHttpProviderUtils;
2222
importcom.ning.http.util.ProxyUtils;
2323
importjava.io.IOException;
24-
importjava.util.HashSet;
25-
importjava.util.Set;
2624
importorg.glassfish.grizzly.CloseListener;
2725
importorg.glassfish.grizzly.CloseType;
2826
importorg.glassfish.grizzly.Closeable;
@@ -86,8 +84,8 @@ public final class HttpTransactionContext {
8684
/**
8785
* <tt>true</tt> if the request is fully sent, or <tt>false</tt>otherwise.
8886
*/
89-
volatilebooleanisRequestFullySent;
90-
Set<CompletionHandler<HttpTransactionContext>>reqFullySentHandlers;
87+
privatebooleanisRequestFullySent;
88+
privateCleanupTaskcleanupTask;
9189

9290
privatefinalCloseListenerlistener =newCloseListener<Closeable,CloseType>() {
9391
@Override
@@ -125,24 +123,9 @@ static void cleanupTransaction(final HttpContext httpCtx,
125123

126124
asserthttpTxContext !=null;
127125

128-
if (httpTxContext.isRequestFullySent) {
129-
cleanupTransaction(httpCtx,httpTxContext);
130-
completionHandler.completed(httpTxContext);
131-
}else {
132-
httpTxContext.addRequestSentCompletionHandler(completionHandler);
133-
if (httpTxContext.isRequestFullySent &&
134-
httpTxContext.removeRequestSentCompletionHandler(completionHandler)) {
135-
completionHandler.completed(httpTxContext);
136-
}
137-
}
126+
httpTxContext.scheduleCleanup(httpCtx,completionHandler);
138127
}
139128

140-
staticvoidcleanupTransaction(finalHttpContexthttpCtx,
141-
finalHttpTransactionContexthttpTxContext) {
142-
httpCtx.getCloseable().removeCloseListener(httpTxContext.listener);
143-
REQUEST_STATE_ATTR.remove(httpCtx);
144-
}
145-
146129
staticHttpTransactionContextcurrentTransaction(
147130
finalHttpHeaderhttpHeader) {
148131
returncurrentTransaction(httpHeader.getProcessingState().getHttpContext());
@@ -269,44 +252,56 @@ void closeConnection() {
269252
connection.closeSilently();
270253
}
271254

272-
privatesynchronizedvoidaddRequestSentCompletionHandler(
255+
privatevoidscheduleCleanup(finalHttpContexthttpCtx,
273256
finalCompletionHandler<HttpTransactionContext>completionHandler) {
274-
if (reqFullySentHandlers ==null) {
275-
reqFullySentHandlers =newHashSet<>();
257+
synchronized (this) {
258+
if (!isRequestFullySent) {
259+
assertcleanupTask ==null;// scheduleCleanup should be called only once
260+
cleanupTask =newCleanupTask(httpCtx,completionHandler);
261+
return;
262+
}
276263
}
277-
reqFullySentHandlers.add(completionHandler);
278-
}
279264

280-
privatesynchronizedbooleanremoveRequestSentCompletionHandler(
281-
finalCompletionHandler<HttpTransactionContext>completionHandler) {
282-
returnreqFullySentHandlers !=null
283-
?reqFullySentHandlers.remove(completionHandler)
284-
:false;
265+
assertisRequestFullySent;
266+
cleanup(httpCtx);
267+
completionHandler.completed(this);
285268
}
286269

287-
booleanisRequestFullySent() {
288-
returnisRequestFullySent;
270+
privatevoidcleanup(finalHttpContexthttpCtx) {
271+
httpCtx.getCloseable().removeCloseListener(listener);
272+
REQUEST_STATE_ATTR.remove(httpCtx);
289273
}
290274

291275
@SuppressWarnings("unchecked")
292276
voidonRequestFullySent() {
293-
this.isRequestFullySent =true;
294-
295-
Object[]handlers =null;
296277
synchronized (this) {
297-
if (reqFullySentHandlers !=null) {
298-
handlers =reqFullySentHandlers.toArray();
299-
reqFullySentHandlers =null;
278+
if (isRequestFullySent) {
279+
return;
300280
}
281+
282+
isRequestFullySent =true;
301283
}
302284

303-
if (handlers !=null) {
304-
for (Objecto :handlers) {
305-
try {
306-
((CompletionHandler<HttpTransactionContext>)o).completed(this);
307-
}catch (Exceptione) {
308-
}
309-
}
285+
if (cleanupTask !=null) {
286+
cleanupTask.run();
287+
}
288+
}
289+
290+
privateclassCleanupTaskimplementsRunnable {
291+
privatefinalHttpContexthttpCtx;
292+
privatefinalCompletionHandler<HttpTransactionContext>completionHandler;
293+
294+
privateCleanupTask(finalHttpContexthttpCtx,
295+
finalCompletionHandler<HttpTransactionContext>completionHandler) {
296+
this.httpCtx =httpCtx;
297+
this.completionHandler =completionHandler;
298+
}
299+
300+
@Override
301+
publicvoidrun() {
302+
cleanup(httpCtx);
303+
completionHandler.completed(HttpTransactionContext.this);
310304
}
305+
311306
}
312307
}// END HttpTransactionContext

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp