0

我正在使用谷歌应用引擎任务队列。这是我的第一次。我找到了一个教程,但有一些问题。以下是我用来检查任务队列的两个 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/gaejcreatetask?emailid=romin@rocketmail.com

现在的问题是当我调用这个 url 时,我看到了创建任务的输出,但是在开发控制台中我看不到队列中的任何任务。如果我从两个 servlet 中删除 doPost 函数,那么我会看到队列中的任务,但是当我运行它时,什么也没有发生,任务仍然存在。为什么会这样,我该如何解决这个问题。提前致谢。

4

3 回答 3

0

您很可能看不到该任务,因为它已经执行了。检查您的日志(开发中的控制台,或生产中管理控制台上的日志记录页面)以查看您的任务处理程序(如果有)抛出了什么异常并修复它。

于 2011-08-01T04:03:19.330 回答
0

不确定您是否应该使用此 doGet 方法。您有 2 个具有相同签名,但是如果您说它打印成功消息,这不应该解决您的问题。

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
    this.doPost(req, resp);
}
于 2011-08-05T07:39:13.113 回答
0

GAEJSignupSubscriberServlet 不需要设置响应内容类型,或者写入响应。当 servlet 从任务中被调用时,响应永远不会被看到,想象它被发送到 /dev/null。您应该从 servlet 登录。

GAEJSignupSubscriberServlet 不需要任何 doGet 方法,只需要 doPost,因为默认情况下使用方法 POST 创建任务。如果您更喜欢使用 GET,请使用方法(TaskOptions.Method.GET)配置 TaskOptions

于 2012-06-25T11:48:18.630 回答