关于 Jetty Continuation

栏目: Java · 发布时间: 7年前

内容简介:领导说,我们不能绑定在某个产品,某个框架,某个技术实现上。所以当我们希望使用 jetty 的 continuation 技术的时候,我们必须对它进行一个包装,以保证将来如果需要,我们可以用其它的技术或框架进行替换。一个简单的包装:TaskManager 类:

领导说,我们不能绑定在某个产品,某个框架,某个技术实现上。所以当我们希望使用 jetty 的 continuation 技术的时候,我们必须对它进行一个包装,以保证将来如果需要,我们可以用其它的技术或框架进行替换。

一个简单的包装:

TaskManager 类:

以下是代码片段:

/**

* submit a task async

*

* @param request

* @param response

* @param task

* @return result true for first submit task, false for expire or exception

*/

public static boolean submitTask(final HttpServletRequest request,

final HttpServletResponse response,

final AbstractContinuation task) {

final Continuation continuation = ContinuationSupport

.getContinuation(request);

if (continuation.isExpired()) {

@SuppressWarnings("unchecked")

final FutureTask ftask = (FutureTask) request

.getAttribute(TASK_FUTURE);

if (ftask != null) {

// not Interrupt if it is still running

ftask.cancel(false);

}

task.onExpire(request, response);

return false;

}

// first call of this request

continuation.setTimeout(ContinuationConfig.timeout);

continuation.suspend(response);

try {

final FutureTask ftask = new FutureTask(task) {

@Override

protected void done() {

// cancelled, just return

if (isCancelled()) {

return;

}

// expired, just return

if (continuation.isExpired()) {

return;

}

// complete, so *NO* attribute will take effects

// request.setAttribute(TASK_RESULTS, get());

try {

final Object result = get();

task.onResult(request, response, result);

} catch (final Exception e) {

task.onException(request, response, e);

}

continuation.complete();

}

};

exec.submit(ftask);

request.setAttribute(TASK_FUTURE, ftask);

return true;

} catch (final TooManyWaitingTaskException e) {

task.onException(request, response, e);

return false;

}

}

/**

* submit a task sync, with *NO* expire support

*

* @param request

* @param response

* @param task

* @return result true for first submit task, false for expire or exception

*/

public static boolean submitTask2(final HttpServletRequest request,

final HttpServletResponse response,

final AbstractContinuation task) {

try {

final Object result = task.call();

task.onResult(request, response, result);

return true;

} catch (final TooManyWaitingTaskException e) {

task.onException(request, response, e);

return false;

} catch (final Exception e) {

task.onException(request, response, e);

return false;

}

}

servlet 里面可以这样调用:(注意,当前不支持 jsp )

以下是代码片段:

@Override

protected void doGet(final HttpServletRequest request,

final HttpServletResponse response) throws ServletException,

IOException {

// create an asynchronous task

final AbstractContinuation task = new AbstractContinuation() {

@Override

public Object call() throws Exception {

// do the job

}

@Override

public void onResult(final HttpServletRequest request,

final HttpServletResponse response, final Object result) {

// has result, so ignore all exception

// do output

}

@Override

public void onExpire(final HttpServletRequest request,

final HttpServletResponse response) {

try {

response.sendError(504, "continuation job expired");

log.warn("on expire:" + request);

} catch (final IOException e) {

log.warn("exception on expire: " + request + e.getMessage());

}

return;

}

@Override

public void onException(final HttpServletRequest request,

final HttpServletResponse response, final Exception e) {

try {

response.sendError(503, "some exception occured ");

log.warn("on exception:" + request, e);

} catch (final IOException ioe) {

log.warn("exception on exception: " + request

+ e.getMessage() + ioe.getMessage());

}

return;

}

};

if (TaskManager.submitTask(request, response, task)) {

// first submit

// do nothing here

} else {

// expire, or exception

// just ignore here, because we have dealed with it other place

}

return;

}


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python神经网络编程

Python神经网络编程

[英]塔里克·拉希德(Tariq Rashid) / 林赐 / 人民邮电出版社 / 2018-4 / 69.00元

神经网络是一种模拟人脑的神经网络,以期能够实现类人工智能的机器学习 技术。 本书揭示神经网络背后的概念,并介绍如何通过Python实现神经网络。全书 分为3章和两个附录。第1章介绍了神经网络中所用到的数学思想。第2章介绍使 用Python实现神经网络,识别手写数字,并测试神经网络的性能。第3章带领读 者进一步了解简单的神经网络,观察已受训练的神经网络内部,尝试进一步改......一起来看看 《Python神经网络编程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器