Skip to content

Commit f261282

Browse files
author
wangbin
committed
1.
1 parent e207741 commit f261282

5 files changed

Lines changed: 259 additions & 186 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ target/*
55
output/logs/*
66
output/work/*
77
output/webapps/ROOT/*
8+
bestsign-sdk-logs

src/main/java/com/mat/net/AsyncContextExample.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@
1111
import javax.servlet.http.HttpServletResponse;
1212

1313
@WebServlet(asyncSupported = true, value = "/AsyncContextExample", loadOnStartup = 1)
14-
public class AsyncContextExample extends HttpServlet {
15-
private static final long serialVersionUID = 1L;
16-
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
17-
doGet(request,response);
18-
}
19-
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
20-
response.setContentType("text/html");
21-
PrintWriter out = response.getWriter();
22-
AsyncContext asyncContext = request.startAsync();
23-
asyncContext.setTimeout(0);
24-
ServletRequest servReq = asyncContext.getRequest();
25-
boolean b = servReq.isAsyncStarted();
26-
out.println("isAsyncStarted : "+b);
27-
asyncContext.dispatch("/asynctest.jsp");
28-
out.println("<br/>asynchronous task finished.");
29-
}
14+
public class AsyncContextExample extends HttpServlet {
15+
private static final long serialVersionUID = 1L;
16+
17+
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
18+
doGet(request, response);
19+
}
20+
21+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
22+
response.setContentType("text/html");
23+
PrintWriter out = response.getWriter();
24+
AsyncContext asyncContext = request.startAsync();//Mat.AsyncStateMachine-->AsyncState.DISPATCHED->AsyncState.STARTING
25+
asyncContext.start(()->{
26+
asyncContext.setTimeout(0);
27+
ServletRequest servReq = asyncContext.getRequest();
28+
boolean b = servReq.isAsyncStarted();
29+
out.println("isAsyncStarted : " + b);
30+
asyncContext.dispatch("/asynctest.jsp");
31+
asyncContext.complete();
32+
out.println("<br/>asynchronous task finished.");
33+
});
34+
}
3035
}

src/main/java/org/apache/coyote/AbstractProtocol.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ public SocketState process(SocketWrapper<S> wrapper,
669669
}
670670

671671
wrapper.setAsync(false);
672-
ContainerThreadMarker.set();
672+
ContainerThreadMarker.set();//设置容器标识
673673

674674
try {
675675
if (processor == null) {
@@ -688,7 +688,7 @@ public SocketState process(SocketWrapper<S> wrapper,
688688
if (status == SocketStatus.CLOSE_NOW) {
689689
processor.errorDispatch();
690690
state = SocketState.CLOSED;
691-
} else if (dispatches != null) {
691+
} else if (dispatches != null) {//处理分发下来读写请求
692692
// Associate the processor with the connection as
693693
// these calls may result in a nested call to process()
694694
connections.put(socket, processor);
@@ -734,7 +734,7 @@ public SocketState process(SocketWrapper<S> wrapper,
734734
}
735735

736736
if (state != SocketState.CLOSED && processor.isAsync()) {
737-
state = processor.asyncPostProcess();
737+
state = processor.asyncPostProcess();//Mat.AsyncStateMachine-->AsyncState.STARTING->AsyncState.STARTED
738738
}
739739

740740
if (state == SocketState.UPGRADING) {
@@ -783,14 +783,14 @@ public SocketState process(SocketWrapper<S> wrapper,
783783
state == SocketState.UPGRADING ||
784784
dispatches != null && state != SocketState.CLOSED);
785785

786-
if (state == SocketState.LONG) {
786+
if (state == SocketState.LONG) {//后续还有数据要读取,将Processor和处理的socket继续做映射。
787787
// In the middle of processing a request/response. Keep the
788788
// socket associated with the processor. Exact requirements
789789
// depend on type of long poll
790790
connections.put(socket, processor);
791791
longPoll(wrapper, processor);
792792
getLog().info("Mat-->SocketState:SocketState.LONG.In the middle of processing a request/response. Keep the socket associated with the processor. Exact requirements depend on type of long poll!");
793-
} else if (state == SocketState.OPEN) {
793+
} else if (state == SocketState.OPEN) {//一个请求数据(header+body)已经处理完成,将processor和处理的socket解绑以供后续连接使用
794794
// In keep-alive but between requests. OK to recycle
795795
// processor. Continue to poll for the next request.
796796
connections.remove(socket);
@@ -855,7 +855,7 @@ public SocketState process(SocketWrapper<S> wrapper,
855855
getLog().error(
856856
sm.getString("abstractConnectionHandler.error"), e);
857857
} finally {
858-
ContainerThreadMarker.clear();
858+
ContainerThreadMarker.clear();//删除容器标识
859859
}
860860

861861
// Make sure socket/processor is removed from the list of current

src/main/java/org/apache/coyote/ActionCode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ public enum ActionCode {
238238

239239
/**
240240
* Indicates that the container needs to trigger a call to onDataAvailable()
241-
* for the registered non-blocking read listener.
241+
* for the registered non-blocking read listener.如果调用的线程不是容器线程的话,直接调用DISPATCH_EXECUTE执行。
242242
*/
243243
DISPATCH_READ,
244244

245245
/**
246246
* Indicates that the container needs to trigger a call to onWritePossible()
247-
* for the registered non-blocking write listener.
247+
* for the registered non-blocking write listener.如果调用的线程不是容器线程的话,直接调用DISPATCH_EXECUTE执行。
248248
*/
249249
DISPATCH_WRITE,
250250

0 commit comments

Comments
 (0)