任务队列有问题吗?
我正在使用谷歌应用程序引擎任务队列。这是我第一次。我找到了一个教程,但遇到了一些问题。以下是我用来检查任务队列的两个 servlet:
public class GAEJSignupSubscriberServlet extends HttpServlet {
private static final Logger _logger = Logger.getLogger(GAEJSignupSubscriberServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String strCallResult = "";
resp.setContentType("text/plain");
try {
String strEmailId = req.getParameter("emailid");
_logger.info("Got a Signup Subscriber Request for Email ID : " + strEmailId);
//
// PUT YOUR TASK CODE HERE
//
if(strEmailId.equals("mh")){
System.out.println("email-id" + strEmailId);
}
strCallResult = "SUCCESS: Subscriber Signup";
_logger.info(strCallResult);
resp.getWriter().println(strCallResult);
}
catch (Exception ex) {
strCallResult = "FAIL: Subscriber Signup : " + ex.getMessage();
_logger.info(strCallResult);
resp.getWriter().println(strCallResult);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
第二个 servlet 是:
public class GAEJCreateTaskServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
this.doPost(req, resp);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String strCallResult = "";
resp.setContentType("text/plain");
try {
//Extract out the To, Subject and Body of the Email to be sent
String strEmailId = req.getParameter("emailid");
//Do validations here. Only basic ones i.e. cannot be null/empty
if (strEmailId == null) throw new Exception("Email Id field cannot be empty.");
//Trim the stuff
strEmailId = strEmailId.trim();
if (strEmailId.length() == 0) throw new Exception("Email Id field cannot be empty.");
//Queue queue = QueueFactory.getDefaultQueue();
Queue queue = QueueFactory.getQueue("subscription-queue");
queue.add(TaskOptions.Builder.withUrl("/gaejsignupsubscriber").param("emailid",strEmailId));
strCallResult = "Successfully created a Task in the Queue";
resp.getWriter().println(strCallResult);
}
catch (Exception ex) {
strCallResult = "Fail: " + ex.getMessage();
resp.getWriter().println(strCallResult);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
我使用此 URL 调用 GAEJCreateTaskServlet:
http://localhost:8080/[email protected]
现在的问题是,当我调用此 url 时,我看到创建任务的输出,但在开发控制台中我看不到任何任务在队列中。如果我从两个 servlet 中删除 doPost 函数,那么我会在队列中看到该任务,但是当我运行它时,什么也没有发生,任务仍然存在。为什么会这样,我该如何解决这个问题。提前致谢。
I am working with google app engine task queue. Its my first time. I found a tutorial but having some problems. Following are two servlets i am using to check task queue:
public class GAEJSignupSubscriberServlet extends HttpServlet {
private static final Logger _logger = Logger.getLogger(GAEJSignupSubscriberServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String strCallResult = "";
resp.setContentType("text/plain");
try {
String strEmailId = req.getParameter("emailid");
_logger.info("Got a Signup Subscriber Request for Email ID : " + strEmailId);
//
// PUT YOUR TASK CODE HERE
//
if(strEmailId.equals("mh")){
System.out.println("email-id" + strEmailId);
}
strCallResult = "SUCCESS: Subscriber Signup";
_logger.info(strCallResult);
resp.getWriter().println(strCallResult);
}
catch (Exception ex) {
strCallResult = "FAIL: Subscriber Signup : " + ex.getMessage();
_logger.info(strCallResult);
resp.getWriter().println(strCallResult);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
second servlet is:
public class GAEJCreateTaskServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
this.doPost(req, resp);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
String strCallResult = "";
resp.setContentType("text/plain");
try {
//Extract out the To, Subject and Body of the Email to be sent
String strEmailId = req.getParameter("emailid");
//Do validations here. Only basic ones i.e. cannot be null/empty
if (strEmailId == null) throw new Exception("Email Id field cannot be empty.");
//Trim the stuff
strEmailId = strEmailId.trim();
if (strEmailId.length() == 0) throw new Exception("Email Id field cannot be empty.");
//Queue queue = QueueFactory.getDefaultQueue();
Queue queue = QueueFactory.getQueue("subscription-queue");
queue.add(TaskOptions.Builder.withUrl("/gaejsignupsubscriber").param("emailid",strEmailId));
strCallResult = "Successfully created a Task in the Queue";
resp.getWriter().println(strCallResult);
}
catch (Exception ex) {
strCallResult = "Fail: " + ex.getMessage();
resp.getWriter().println(strCallResult);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
I am invoking GAEJCreateTaskServlet with this URL:
http://localhost:8080/[email protected]
now problem is when i invoke this url i see the output that task is created but in development console i can not see any task in the queue. If i remove doPost function from both servlets then i see the task in the queue but when i run it nothing happens and tasks remains there. Why is it happening so and how can i solve this problem. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您很可能看不到该任务,因为它已经执行了。检查您的日志(开发中的控制台或生产中管理控制台上的日志记录页面)以查看任务处理程序抛出的异常(如果有)并修复它。
Most likely you can't see the task because it has already executed. Check your logs (the console in development, or the logging page on the admin console in production) to see what exception is being thrown by your task handler (if any) and fix it.
不确定您是否应该使用这个 doGet 方法。你有 2 个具有相同签名的,但是如果你说它打印成功消息,这应该不能解决你的问题。
Not sure if you should have this doGet method. You have 2 with the same signature, but this should not solve your problem has you say it prints the success message.
GAEJSignupSubscriberServlet 不需要设置响应内容类型,或写入响应。当从任务中调用 servlet 时,永远不会看到响应,想象一下它被发送到 /dev/null。您应该从 servlet 登录。
GAEJSignupSubscriberServlet 不需要任何 doGet 方法,只需要 doPost,因为默认情况下使用 POST 方法创建任务。如果您更喜欢使用 GET,请使用 method(TaskOptions.Method.GET) 配置 TaskOptions
GAEJSignupSubscriberServlet need not set the response content type, or write to the response. As the servlet is being called from a task, the response is never seen, imagine it being sent to /dev/null. You should log from the servlet.
GAEJSignupSubscriberServlet doesn't need any doGet methods, only doPost, as tasks are created with method POST by default. If you prefer to use GET, then configure the TaskOptions with method(TaskOptions.Method.GET)