以下代码在开发环境中完美运行。获得" / _啊/ api / worker / v1 / tasks"调用refresh()方法,该方法将任务添加到POST为" / _ ah / api / worker / v1 / tasks"的默认任务队列中。这是由handle()方法处理的。
@Api(name = "worker", version = "v1", scopes = { Constants.EMAIL_SCOPE }, clientIds = { Constants.API_EXPLORER_CLIENT_ID, Constants.WEB_CLIENT_ID })
public class WorkerApi
{
@ApiMethod( name="refresh", path = "tasks", httpMethod = HttpMethod.GET)
public void refresh()
{
MyObject myObject = new MyObject()
Queue queue = QueueFactory.getDefaultQueue();
TaskOptions o = TaskOptions.Builder.withUrl("/_ah/api/worker/v1/tasks");
o.payload(ApiHelper.ObjectToJson(myObject ));
queue.add(o);
}
@ApiMethod( name="handle", path = "tasks", httpMethod = HttpMethod.POST)
public void handle(HttpServletRequest req, MyObject myObject)
{
// This handler is called if running in dev in environment
// If running on GAE this handler is never called.
}
}
可悲的是,当部署到实际的Google App Engine时,任务队列永远不会调用handle方法。
有趣的是,我可以使用API Explorer手动成功调用处理程序,它在GAE控制台中生成以下日志:
2014-11-24 12:01:30.914 /_ah/spi/my.test.application.WorkerApi.handle 200 1394ms 0kb Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36 module=default version=1
198.66.21.14 - - [24/Nov/2014:12:01:30 -0800] "POST /_ah/spi/my.test.application.WorkerApi.handle HTTP/1.1" 200 116 - "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36" "my-test-application.appspot.com" ms=1394 cpu_ms=1003 cpm_usd=0.000013 app_engine_release=1.9.16 instance=00c61b117c39338c333cff447edea78fa0f90d
但是当由任务队列调用时,总会返回404,在GAE控制台中生成以下日志:
2014-11-24 12:07:54.727 /_ah/api/worker/v1/tasks 404 147ms 0kb AppEngine-Google; (+http://code.google.com/appengine) module=default version=1
0.1.0.2 - - [24/Nov/2014:12:07:54 -0800] "POST /_ah/api/worker/v1/tasks HTTP/1.1" 404 234 "https://my-test-application.appspot.com/_ah/spi/my.test.application.WorkerApi.refresh" "AppEngine-Google; (+http://code.google.com/appengine)" "my-test-application.appspot.com" ms=148 cpu_ms=23 cpm_usd=0.000026 queue_name=default task_name=9271899958322278984 pending_ms=61 app_engine_release=1.9.16 instance=00c61b117c7544e23aeb8d8b0c996da6d286d539
同样,一切都在开发环境中按预期工作。部署到GAE时损坏。我做错了什么?
比较日志条目,我认为我发现了问题并将代码更改为:
TaskOptions o = TaskOptions.Builder.withUrl("/_ah/spi/my.test.application.WorkerApi.handle");
不。这不起作用。现在它在开发环境和GAE部署中被打破了。
从开发环境登录:
[INFO] INFO: Web hook at http://127.0.0.1:8080/_ah/spi/my.test.application.WorkerApi.handle returned status code 404. Rescheduling..
从GAE控制台登录:
2014-11-24 12:24:10.872 /_ah/spi/my.test.application.WorkerApi.handle 404 396ms 0kb AppEngine-Google; (+http://code.google.com/appengine) module=default version=1
0.1.0.2 - - [24/Nov/2014:12:24:10 -0800] "POST /_ah/spi/my.test.application.WorkerApi.handle HTTP/1.1" 404 96 "https://my-test-application.appspot.com/_ah/spi/my.test.application.WorkerApi.refresh" "AppEngine-Google; (+http://code.google.com/appengine)" "my-test-application.appspot.com" ms=397 cpu_ms=23 cpm_usd=0.000011 queue_name=default task_name=7132899958322278984 app_engine_release=1.9.16 instance=00c61b117c565c0a8aa81e1b86e50d78f0346330
BTW,我正在使用GAE SDK 1.9.16。
答案 0 :(得分:5)
我有同样的问题。 在本地PC中,这很有效。
也许,GAE服务器无法找到未明确写入web.xml的url(端点)。
要解决此问题,请为worker创建另一个servlet。
例如,端点方法是
Queue queue = QueueFactory.getDefaultQueue();
TaskOptions o = TaskOptions.Builder.withUrl("/work");
o.param( "target" , target) ; // target is String in this case
queue.add(o);
war / WEB-INF / web.xml是
<web-app ...>
... default servet (SystemServiceServlet) ...
<servlet>
<servlet-name>WorkerServlet</servlet-name>
<servlet-class>com.example.your.package.WorkerServlet</servlet-class>
</servlet>
.... default mapping file ...
<servlet-mapping>
<servlet-name>WorkerServlet</servlet-name>
<url-pattern>/work</url-pattern>
</servlet-mapping>
并将WorkerServlet.java文件创建为
public class WorkerServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String target = req.getParameter("target");
// do something
resp.setStatus( resp.SC_OK );
}
}
在这种情况下,GAE服务器可以找到url / work,并且Queue运行良好。
答案 1 :(得分:0)
只是添加到KNaito回答
RequestDispatcher dispatcher = req.getRequestDispatcher(target);
dispatcher.forward(req, resp);
最后使用这两个参数将请求重定向到所需位置。