本文整理汇总了Java中javax.servlet.AsyncEvent类的典型用法代码示例。如果您正苦于以下问题:Java AsyncEvent类的具体用法?Java AsyncEvent怎么用?Java AsyncEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsyncEvent类属于javax.servlet包,在下文中一共展示了AsyncEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onComplete
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Override
public void onComplete(AsyncEvent arg0) throws IOException {
// 返回信息
OutputStream os = ac.getResponse().getOutputStream();
InputStream is = response.getEntity().getContent();
// 调用微信平台的Callback
HttpClientCallbackResult result = new HttpClientCallbackResult(is, os);
result.setRetCode(getStatusCode());
try {
callback.completed(result);
}
catch (RuntimeException e) {
// ignore
}
finally {
os.flush();
os.close();
is.close();
}
}
示例2: onTimeout
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Override
public void onTimeout(AsyncEvent event) throws IOException {
// in this time, maybe:
// 1.invocation in executor's queue
// 2.already executing in executor
// 3.already send response
// to avoid concurrent, must lock request
ServletRequest request = event.getSuppliedRequest();
HttpServletRequestEx requestEx = (HttpServletRequestEx) request.getAttribute(RestConst.REST_REQUEST);
synchronized (requestEx) {
ServletResponse response = event.getAsyncContext().getResponse();
if (!response.isCommitted()) {
LOGGER.error("Rest request timeout, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());
// invocation in executor's queue
response.setContentType(MediaType.APPLICATION_JSON);
// we don't know if developers declared one statusCode in contract
// so we use cse inner statusCode here
((HttpServletResponse) response).setStatus(ExceptionFactory.PRODUCER_INNER_STATUS_CODE);
PrintWriter out = response.getWriter();
out.write(TIMEOUT_MESSAGE);
response.flushBuffer();
}
request.removeAttribute(RestConst.REST_REQUEST);
}
}
示例3: processEvent
import javax.servlet.AsyncEvent; //导入依赖的package包/类
private void processEvent(AsyncEvent event) throws IOException {
Entity prior = recorder.getTraceEntity();
try {
Entity entity = (Entity) event.getSuppliedRequest().getAttribute(ENTITY_ATTRIBUTE_KEY);
recorder.setTraceEntity(entity);
if (null != event.getThrowable()) {
entity.addException(event.getThrowable());
}
filter.postFilter(event.getSuppliedRequest(), event.getSuppliedResponse());
} finally {
recorder.setTraceEntity(prior);
}
}
示例4: onAsyncStart
import javax.servlet.AsyncEvent; //导入依赖的package包/类
private void onAsyncStart(AsyncContext newAsyncContext) {
final boolean setupRequired = SecurityActions.currentServletRequestContext() == null;
ThreadSetupAction.Handle handle = null;
if (setupRequired) {
handle = servletRequestContext.getDeployment().getThreadSetupAction().setup(exchange);
}
try {
//now run request listeners
setupRequestContext(setupRequired);
try {
for (final BoundAsyncListener listener : asyncListeners) {
//make sure we use the new async context
AsyncEvent event = new AsyncEvent(newAsyncContext, listener.servletRequest, listener.servletResponse);
try {
listener.asyncListener.onStartAsync(event);
} catch (IOException e) {
UndertowServletLogger.REQUEST_LOGGER.ioExceptionDispatchingAsyncEvent(e);
}
}
} finally {
tearDownRequestContext(setupRequired);
}
} finally {
if (setupRequired) {
handle.tearDown();
}
}
}
示例5: onError
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Override
public void onError(AsyncEvent arg0) throws IOException {
// 也要返回合理的错误信息
try {
OutputStream os = ac.getResponse().getOutputStream();
HttpClientCallbackResult result = new HttpClientCallbackResult(null, os);
HttpAsyncException exp = new HttpAsyncException(HttpAsyncException.ExceptionEvent.REQASYNC_ERROR,
arg0.getThrowable());
result.setRetCode(getStatusCode());
result.setException(exp);
callback.failed(result);
}
catch (RuntimeException e) {
// ignore
}
}
示例6: onTimeout
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Override
public void onTimeout(AsyncEvent arg0) throws IOException {
// 超时,也要返回合理的错误信息
try {
OutputStream os = ac.getResponse().getOutputStream();
HttpClientCallbackResult result = new HttpClientCallbackResult(null, os);
HttpAsyncException exp = new HttpAsyncException(HttpAsyncException.ExceptionEvent.REQASYNC_TIMEOUT,
arg0.getThrowable());
result.setRetCode(getStatusCode());
result.setException(exp);
callback.failed(result);
}
catch (RuntimeException e) {
// ignore
}
}
示例7: handleTimeoutOrError
import javax.servlet.AsyncEvent; //导入依赖的package包/类
private void handleTimeoutOrError(AsyncEvent event, String message) {
PrintWriter writer = null;
try {
future.cancel(true);
HttpServletResponse response = (HttpServletResponse) event.getAsyncContext().getResponse();
//HttpServletRequest request = (HttpServletRequest) event.getAsyncContext().getRequest();
//request.getRequestDispatcher("/app/error.htm").forward(request, response);
writer = response.getWriter();
writer.print(message);
writer.flush();
} catch (Exception ex) {
// FIXME: logger.error(ex);
} finally {
event.getAsyncContext().complete();
if (writer != null) {
writer.close();
}
}
}
示例8: setStarted
import javax.servlet.AsyncEvent; //导入依赖的package包/类
public void setStarted(Context context, ServletRequest request, ServletResponse response,
boolean originalRequestResponse) {
synchronized (asyncContextLock) {
this.request.getCoyoteRequest().action(ActionCode.ASYNC_START, this);
this.context = context;
this.servletRequest = request;
this.servletResponse = response;
this.hasOriginalRequestAndResponse = originalRequestResponse;
this.event = new AsyncEvent(this, request, response);
List<AsyncListenerWrapper> listenersCopy = new ArrayList<AsyncListenerWrapper>();
listenersCopy.addAll(listeners);
listeners.clear();
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnStartAsync(event);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.warn("onStartAsync() failed for listener of type [" + listener.getClass().getName() + "]", t);
}
}
}
}
示例9: startDeferredResultProcessingTimeoutAndResumeThroughCallback
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughCallback() throws Exception {
final DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
deferredResult.onTimeout(new Runnable() {
@Override
public void run() {
deferredResult.setResult(23);
}
});
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertTrue(this.asyncManager.hasConcurrentResult());
assertEquals(23, this.asyncManager.getConcurrentResult());
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
示例10: startDeferredResultProcessingTimeoutAndResumeThroughInterceptor
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Test
public void startDeferredResultProcessingTimeoutAndResumeThroughInterceptor() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {
@Override
public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
result.setErrorResult(23);
return true;
}
};
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertTrue(this.asyncManager.hasConcurrentResult());
assertEquals(23, this.asyncManager.getConcurrentResult());
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
示例11: startDeferredResultProcessingAfterTimeoutException
import javax.servlet.AsyncEvent; //导入依赖的package包/类
@Test
public void startDeferredResultProcessingAfterTimeoutException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
final Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = new DeferredResultProcessingInterceptorAdapter() {
@Override
public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception {
throw exception;
}
};
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
this.asyncManager.startDeferredResultProcessing(deferredResult);
AsyncEvent event = null;
this.asyncWebRequest.onTimeout(event);
assertTrue(this.asyncManager.hasConcurrentResult());
assertEquals(exception, this.asyncManager.getConcurrentResult());
assertEquals("/test", ((MockAsyncContext) this.servletRequest.getAsyncContext()).getDispatchedPath());
}
示例12: onTimeout
import javax.servlet.AsyncEvent; //导入依赖的package包/类
/**
* Async operation timeout (AsyncListener interface)
*
* @param event Async event
*/
@Override
public void onTimeout(AsyncEvent event) {
AsyncContext context = event.getAsyncContext();
lock.lock();
try {
pendingWaits.remove(context);
JSONObject response = new JSONObject();
response.put("events", new JSONArray());
response.put("requestProcessingTime", System.currentTimeMillis()-timestamp);
try (Writer writer = context.getResponse().getWriter()) {
response.writeJSONString(writer);
} catch (IOException exc) {
Logger.logDebugMessage(String.format("Unable to return API response to %s: %s",
address, exc.toString()));
}
context.complete();
timestamp = System.currentTimeMillis();
} finally {
lock.unlock();
}
}
示例13: setStarted
import javax.servlet.AsyncEvent; //导入依赖的package包/类
public void setStarted(Context context, ServletRequest request,
ServletResponse response, boolean originalRequestResponse) {
this.request.getCoyoteRequest().action(
ActionCode.ASYNC_START, this);
this.context = context;
this.servletRequest = request;
this.servletResponse = response;
this.hasOriginalRequestAndResponse = originalRequestResponse;
this.event = new AsyncEvent(this, request, response);
List<AsyncListenerWrapper> listenersCopy =
new ArrayList<AsyncListenerWrapper>();
listenersCopy.addAll(listeners);
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnStartAsync(event);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
log.warn("onStartAsync() failed for listener of type [" +
listener.getClass().getName() + "]", t);
}
}
listeners.clear();
}
示例14: setStarted
import javax.servlet.AsyncEvent; //导入依赖的package包/类
public void setStarted(Context context, ServletRequest request,
ServletResponse response, boolean originalRequestResponse) {
this.request.getCoyoteRequest().action(
ActionCode.ASYNC_START, this);
this.context = context;
this.servletRequest = request;
this.servletResponse = response;
this.hasOriginalRequestAndResponse = originalRequestResponse;
this.event = new AsyncEvent(this, request, response);
List<AsyncListenerWrapper> listenersCopy =
new ArrayList<AsyncListenerWrapper>();
listenersCopy.addAll(listeners);
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnStartAsync(event);
} catch (IOException ioe) {
log.warn("onStartAsync() failed for listener of type [" +
listener.getClass().getName() + "]", ioe);
}
}
listeners.clear();
}
示例15: setErrorState
import javax.servlet.AsyncEvent; //导入依赖的package包/类
public void setErrorState(Throwable t) {
if (t!=null) request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
request.getCoyoteRequest().action(ActionCode.ASYNC_ERROR, null);
AsyncEvent errorEvent = new AsyncEvent(event.getAsyncContext(),
event.getSuppliedRequest(), event.getSuppliedResponse(), t);
List<AsyncListenerWrapper> listenersCopy =
new ArrayList<AsyncListenerWrapper>();
listenersCopy.addAll(listeners);
for (AsyncListenerWrapper listener : listenersCopy) {
try {
listener.fireOnError(errorEvent);
} catch (IOException ioe) {
log.warn("onStartAsync() failed for listener of type [" +
listener.getClass().getName() + "]", ioe);
}
}
}