Java Code Examples for com.google.appengine.api.taskqueue.QueueFactory#getDefaultQueue()

The following examples show how to use com.google.appengine.api.taskqueue.QueueFactory#getDefaultQueue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testTaskHandleContainsAllNecessaryProperties() throws Exception {
    String name = "testTaskHandleContainsAllNecessaryProperties-" + System.currentTimeMillis();
    Queue queue = QueueFactory.getDefaultQueue();

    TaskOptions options = withTaskName(name).payload("payload");
    options.etaMillis(0); // TODO -- remove this once NPE is fixewd

    TaskHandle handle = queue.add(options);

    assertEquals("default", handle.getQueueName());
    assertEquals(name, handle.getName());
    assertEquals("payload", new String(handle.getPayload(), "UTF-8"));
    assertNotNull(handle.getEtaMillis());
    assertEquals(0, (int) handle.getRetryCount());
}
 
Example 2
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
private static void deferredTask(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
  String queue = req.getParameter("queue");
  Queue q;
  if (queue == null) {
    q = QueueFactory.getDefaultQueue();
  } else {
    q = QueueFactory.getQueue(queue);
  }
  final String data = req.getParameter("deferredData");

  TaskOptions opts =
      TaskOptions.Builder.withPayload(
          new DeferredTask() {
            @Override
            public void run() {
              gotCalledBack(data);
            }
          });

  latch = new CountDownLatch(1);
  TaskHandle handle = q.add(opts);
  resp.getWriter().print(handle.getQueueName());
}
 
Example 3
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryLimitIsHonored() throws Exception {
    long numTimesToFail = 10;
    int retryLimit = 2;
    String key = "testRetryLimitIsHonored-" + System.currentTimeMillis();

    Queue queue = QueueFactory.getDefaultQueue();
    waitOnFuture(queue.addAsync(withUrl("/_ah/retryTest")
        .param("testdata-key", key)
        .param("times-to-fail", String.valueOf(numTimesToFail))
        .retryOptions(RetryOptions.Builder.withTaskRetryLimit(retryLimit))));

    Long expectedAttempts = (long) (retryLimit + 1);
    String countKey = RetryTestServlet.getInvocationCountKey(key);
    Long actualAttempts = waitForTestData(countKey, expectedAttempts);

    // Ideally this would be the assert, but when a task fails with 500, the test framework
    // cannot capture it for the attempt count.
    // assertEquals(expectedAttempts, actualAttempts);

    // Allow room for one task to fail with 500.
    assertTrue("Task retries lower than specified via withTaskRetryLimit()",
        actualAttempts == expectedAttempts || actualAttempts == expectedAttempts - 1);
}
 
Example 4
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetry() throws Exception {
    long numTimesToFail = 1;
    String key = "testRetry-" + System.currentTimeMillis();

    Queue queue = QueueFactory.getDefaultQueue();
    waitOnFuture(queue.addAsync(withUrl("/_ah/retryTest")
        .param("testdata-key", key)
        .param("times-to-fail", String.valueOf(numTimesToFail))
        .retryOptions(RetryOptions.Builder.withTaskRetryLimit(5))));

    String countKey = RetryTestServlet.getInvocationCountKey(key);
    Long expectedAttempts = (long) (numTimesToFail + 1);
    Long actualAttempts = waitForTestData(countKey, expectedAttempts);
    assertEquals(expectedAttempts, actualAttempts);

    String requestKey1 = RetryTestServlet.getRequestDataKey(key, 1);
    RequestData request1 = waitForTestDataToExist(requestKey1);
    assertEquals("0", request1.getHeader(TASK_RETRY_COUNT));
    assertEquals("0", request1.getHeader(TASK_EXECUTION_COUNT));

    String requestKey2 = RetryTestServlet.getRequestDataKey(key, 2);
    RequestData request2 = waitForTestDataToExist(requestKey2);
    assertEquals("1", request2.getHeader(TASK_RETRY_COUNT));
    assertEquals("1", request2.getHeader(TASK_EXECUTION_COUNT));
}
 
Example 5
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestHeaders() throws Exception {
    String name = "testRequestHeaders-1-" + System.currentTimeMillis();
    Queue defaultQueue = QueueFactory.getDefaultQueue();
    defaultQueue.add(withTaskName(name));
    sync();

    RequestData request = DefaultQueueServlet.getLastRequest();
    assertEquals("default", request.getHeader(QUEUE_NAME));
    assertEquals(name, request.getHeader(TASK_NAME));
    assertNotNull(request.getHeader(TASK_RETRY_COUNT));
    assertNotNull(request.getHeader(TASK_EXECUTION_COUNT));
    assertNotNull(request.getHeader(TASK_ETA));

    String name2 = "testRequestHeaders-2-" + System.currentTimeMillis();
    Queue testQueue = QueueFactory.getQueue("test");
    testQueue.add(withTaskName(name2));
    sync();

    request = TestQueueServlet.getLastRequest();
    assertEquals("test", request.getHeader(QUEUE_NAME));
    assertEquals(name2, request.getHeader(TASK_NAME));
}
 
Example 6
Source File: DeferSampleServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Basic demonstration of adding a deferred task.
 *
 * @param request servlet request
 * @param resp servlet response
 */
@Override
public void doGet(final HttpServletRequest request, final HttpServletResponse resp)
    throws IOException {
  // Add the task to the default queue.
  Queue queue = QueueFactory.getDefaultQueue();

  // Wait 5 seconds to run for demonstration purposes
  queue.add(
      TaskOptions.Builder.withPayload(new ExpensiveOperation())
          .etaMillis(System.currentTimeMillis() + DELAY_MS));

  resp.setContentType("text/plain");
  resp.getWriter().println("Task is backgrounded on queue!");
}
 
Example 7
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnlyPullTasksCanHaveTag() {
    Queue pullQueue = QueueFactory.getQueue("pull-queue");
    pullQueue.add(withMethod(PULL).tag("foo"));

    Queue pushQueue = QueueFactory.getDefaultQueue();
    try {
        pushQueue.add(withTag("foo"));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // pass
    }
}
 
Example 8
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private void purgeQueue(HttpServletRequest req) throws ServletException {
  String queue = req.getParameter("queue");
  Queue q;
  if (queue == null) {
    q = QueueFactory.getDefaultQueue();
  } else {
    q = QueueFactory.getQueue(queue);
  }
  q.purge();
}
 
Example 9
Source File: TransactionsTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionalTaskEnqueuing() throws Exception {
  // [START transactional_task_enqueuing]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  Queue queue = QueueFactory.getDefaultQueue();
  Transaction txn = datastore.beginTransaction();
  // ...

  queue.add(txn, TaskOptions.Builder.withUrl("/path/to/handler"));

  // ...

  txn.commit();
  // [END transactional_task_enqueuing]
}
 
Example 10
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
private void addTasks(HttpServletRequest req) throws ServletException {
  int numTasks = Integer.parseInt(req.getParameter("numTasks"));
  Queue q = QueueFactory.getDefaultQueue();

  latch = new CountDownLatch(numTasks);
  q.add(Collections.nCopies(numTasks, TaskOptions.Builder.withUrl(req.getServletPath())));
}
 
Example 11
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testTaskWithoutUrlIsSubmittedToDefaultUrl() throws Exception {
    Queue defaultQueue = QueueFactory.getDefaultQueue();
    waitOnFuture(defaultQueue.addAsync(withMethod(POST)));
    sync();
    assertTrue("DefaultQueueServlet was not invoked", DefaultQueueServlet.wasInvoked());

    Queue testQueue = QueueFactory.getQueue("test");
    waitOnFuture(testQueue.addAsync(withMethod(POST)));
    sync();
    assertTrue("TestQueueServlet was not invoked", TestQueueServlet.wasInvoked());
}
 
Example 12
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaders() throws Exception {
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(withHeader("header_key", "header_value"));
    sync();

    RequestData lastRequest = DefaultQueueServlet.getLastRequest();
    assertEquals("header_value", lastRequest.getHeader("header_key"));
}
 
Example 13
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test(expected = InvalidQueueModeException.class)
public void testLeaseTaskFromPushQueueThrowsException() {
    Queue pushQueue = QueueFactory.getDefaultQueue();
    pushQueue.leaseTasks(1000, TimeUnit.SECONDS, 1);
}
 
Example 14
Source File: TxTaskQueueTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private Queue getDefaultQueue() {
    return QueueFactory.getDefaultQueue();
}
 
Example 15
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testTaskHandleContainsAutoGeneratedTaskNameWhenTaskNameNotDefinedInTaskOptions() throws Exception {
    Queue queue = QueueFactory.getDefaultQueue();
    TaskHandle handle = waitOnFuture(queue.addAsync());
    assertNotNull(handle.getName());
}
 
Example 16
Source File: TasksTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testTaskHandleContainsAutoGeneratedTaskNameWhenTaskNameNotDefinedInTaskOptions() throws Exception {
    Queue queue = QueueFactory.getDefaultQueue();
    TaskHandle handle = queue.add();
    assertNotNull(handle.getName());
}
 
Example 17
Source File: TaskQueueTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private Queue getDefaultQueue() {
    return QueueFactory.getDefaultQueue();
}
 
Example 18
Source File: QueueHelper.java    From yawp with MIT License 4 votes vote down vote up
public static Queue getDefaultQueue() {
    return QueueFactory.getDefaultQueue();
}
 
Example 19
Source File: DeferredDatastoreSessionStore.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
public DeferredDatastoreSessionStore(String queueName) {
  this.queue =
      queueName == null ? QueueFactory.getDefaultQueue() : QueueFactory.getQueue(queueName);
}
 
Example 20
Source File: TaskQueueServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
private void addTask(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  String queue = req.getParameter("queue");
  Queue q;
  if (queue == null) {
    q = QueueFactory.getDefaultQueue();
  } else {
    q = QueueFactory.getQueue(queue);
  }
  TaskOptions.Method method = TaskOptions.Method.valueOf(req.getParameter("httpmethod"));
  String url = req.getParameter("taskUrl");
  if (url == null) {
    url = req.getServletPath();
  }
  TaskOptions opts =
      TaskOptions.Builder.withUrl(url).header("myheader", "blarg29").method(method);

  if (method == TaskOptions.Method.POST || method == TaskOptions.Method.PUT) {
    opts.payload("this=that");
  } else {
    opts.param("myparam", "yam28");
  }
  String execTimeMs = req.getParameter("execTimeMs");
  if (execTimeMs != null) {
    opts.etaMillis(Long.valueOf(execTimeMs));
  }
  String requestNamespace = req.getParameter("requestNamespace");
  if (requestNamespace != null) {
    /* We could override the current environment and set the request namespace
     * but that's a little overkill and already tested in
     * com.google.appengine.api.taskqueue.TaskQueueTest .
     */
    opts.header(DEFAULT_NAMESPACE_HEADER, requestNamespace);
  }
  String currentNamespace = req.getParameter("currentNamespace");
  if (currentNamespace != null) {
    /* We could also do this:
     * opts.header(CURRENT_NAMESPACE_HEADER, currentNamespace);
     */
    NamespaceManager.set(currentNamespace);
  }
  latch = new CountDownLatch(1);
  TaskHandle handle = q.add(opts);
  resp.getWriter().print(handle.getQueueName());
}