<queue-entries>
<!--Set the number of max concurrent requests to 10--> <queue> <name>optimize-queue</name> <rate>20/s</rate> <bucket-size>40</bucket-size> <max-concurrent-requests>10</max-concurrent-requests> </queue>
</queue-entries>
樣例代碼 ?
這是一個非常簡單的例子。 如前所述,任務隊列基本上是一個URL處理程序。 在此Servlet中,GET將處理入隊任務。 該任務將POST到同一servlet,并執行執行任務的doPost()方法。 在這種情況下,它只是一個簡單的計數器。 請注意,計數器是一個易失性屬性。 如果您將此Servlet作為GET請求訪問,它將排隊另一個任務。 因此,您將看到兩個任務都將計數器增加。 public class TaskQInfo extends HttpServlet {private static volatile int TASK_COUNTER = 0;// Executed by user menu clickpublic void doGet(HttpServletRequest req, HttpServletResponse resp)throws IOException {// Build a task using the TaskOptions Builder pattern from ** aboveQueue queue = QueueFactory.getDefaultQueue();queue.add(withUrl("/taskq_demo").method(TaskOptions.Method.POST)); resp.getWriter().println("Task have been added to default queue...");resp.getWriter().println("Refresh this page to add another count task");}// Executed by TaskQueue@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// This is the body of the taskfor(int i = 0; i < 1000; i++) {log.info("Processing: " + req.getHeader("X-AppEngine-TaskName") + "-" + TASK_COUNTER++); try { // Sleep for a second (if the rate is set to 1/s this will allow at // most 1 more task to be processed)Thread.sleep(1000); } catch (InterruptedException e) { // ignore}}}
}