gpt4 book ai didi

java - 重定向 servlet 异步和心跳

转载 作者:行者123 更新时间:2023-12-01 14:10:19 27 4
gpt4 key购买 nike

我们在java web-app中有一些任务需要很长时间才能完成,并且在任务完成之前,请求超时并且页面无法显示。我们正在考虑设置一个异步重定向 servlet,充当前端 Controller ,将请求重定向到适当的操作类,并且在处理请求时,servlet 会每隔一分钟左右发送一次心跳,直到相应的操作完成该请求。 Action 类。有人使用异步 servlet 3.0 实现了类似的东西吗?这也可能吗?据我了解,这类似于服务器推送。感谢您的指导。

最佳答案

是的,您可以通过使用来实现这种功能异步servlet 3.0.它基本上像push一样工作通知并持续给您回复在这里提出请求,我有一个与之共享的代码此代码可以帮助您发出异步请求。

this example check live users



@WebServlet(urlPatterns = { "/checkliveuser" }, asyncSupported = true)
public class CheckLiveUser extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final Queue<AsyncContext> queue = new ConcurrentLinkedQueue();

private static final BlockingQueue<String> messageQueue = new LinkedBlockingQueue();

private static final String BEGIN_SCRIPT_TAG = "<script type='text/javascript'>\n";

private static final String END_SCRIPT_TAG = "</script>\n";

private Thread notifierThread = null;
@Override
public void init(ServletConfig config) throws ServletException {

ServletContext context = config.getServletContext();
Set<String> users = new HashSet<String>();
Map<String, String> page = new HashMap<String, String>();
context.setAttribute("page", page);
context.setAttribute("messageQueue", messageQueue);


Runnable notifierRunnable = new Runnable() {
public void run() {
boolean done = false;
while (!done) {
System.out.println("in thread");
String cMessage = null;
try {
cMessage = BEGIN_SCRIPT_TAG + toJsonp("<b>Live User:", messageQueue.take())
+ END_SCRIPT_TAG;
for (AsyncContext ac : queue) {
try {
PrintWriter acWriter = ac.getResponse()
.getWriter();
acWriter.println(cMessage);
acWriter.flush();
} catch (IOException ex) {
System.out.println(ex);
queue.remove(ac);

}
}
} catch (InterruptedException iex) {
done = true;
System.out.println(iex);
}
}
}
};
notifierThread = new Thread(notifierRunnable);
notifierThread.start();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {


response.setContentType("text/html");

PrintWriter writer = response.getWriter();

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
final AsyncContext ac = request.startAsync();
ac.setTimeout(10 * 60 * 1000 * 1000);
ac.addListener(new AsyncListener() {
public void onComplete(AsyncEvent event) throws IOException {
queue.remove(ac);
System.out.println("on complete");
}

public void onTimeout(AsyncEvent event) throws IOException {
queue.remove(ac);
System.out.println("on timeout");
}

public void onError(AsyncEvent event) throws IOException {
queue.remove(ac);
System.out.println("on error");

}

public void onStartAsync(AsyncEvent event) throws IOException {
System.out.println("on startup");
}
});
queue.add(ac);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
ServletContext context = request.getServletContext();
HttpSession session = request.getSession();
Map<String, String> logins = (Map<String, String>) context
.getAttribute("page");
if (request.getParameter("action") != null
&& !request.getParameter("action").isEmpty()) {

if (request.getParameter("action").equalsIgnoreCase("logout")) {

logins.remove(request.getSession().getId());
request.getSession().invalidate();

}
}
String name = request.getParameter("loginID");
if (name != null) {

session.setAttribute("user", name);
session.setAttribute("jsessionId", session.getId());
logins.put(session.getId(), name);

}
String html = "";

for (Map.Entry<String, String> entry : logins.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());
html += entry.getValue() + "<br>";
}

String cMessage = BEGIN_SCRIPT_TAG + toJsonp("<b>Live User:", html)
+ END_SCRIPT_TAG;
notify(cMessage);

response.getWriter().println("success");
if (request.getParameter("action") != null
&& !request.getParameter("action").isEmpty()) {

if (request.getParameter("action").equalsIgnoreCase("logout"))
response.sendRedirect("login.jsp");

} else {
response.sendRedirect("welcome.jsp");
}

}

@Override
public void destroy() {
queue.clear();
notifierThread.interrupt();
}

private void notify(String cMessage) throws IOException {
try {
messageQueue.put(cMessage);
} catch (Exception ex) {
IOException t = new IOException();
t.initCause(ex);
throw t;
}
}

private String escape(String orig) {
StringBuffer buffer = new StringBuffer(orig.length());

for (int i = 0; i < orig.length(); i++) {
char c = orig.charAt(i);
switch (c) {
case '\b':
buffer.append("\\b");
break;
case '\f':
buffer.append("\\f");
break;
case '\n':
buffer.append("<br />");
break;
case '\r':
// ignore
break;
case '\t':
buffer.append("\\t");
break;
case '\'':
buffer.append("\\'");
break;
case '\"':
buffer.append("\\\"");
break;
case '\\':
buffer.append("\\\\");
break;
case '<':
buffer.append("<");
break;
case '>':
buffer.append(">");
break;
case '&':
buffer.append("&");
break;
default:
buffer.append(c);
}
}

return buffer.toString();
}

private String toJsonp(String name, String message) {
return "window.parent.app.update({ name: \"" + escape(name)
+ "\", message: \"" + escape(message) + "\" });\n";
}

关于java - 重定向 servlet 异步和心跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18565648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com