Java Code Examples for com.google.appengine.api.taskqueue.Queue#add()

The following examples show how to use com.google.appengine.api.taskqueue.Queue#add() . 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 testMultiValueParams() throws Exception {
    class ParamHandler implements PrintServlet.RequestHandler {
        private String[] paramValues;

        public void handleRequest(ServletRequest req) {
            paramValues = req.getParameterValues("multi_value");
        }
    }

    ParamHandler handler = new ParamHandler();
    PrintServlet.setRequestHandler(handler);

    final Queue queue = QueueFactory.getQueue("tasks-queue");
    queue.add(
        withUrl(URL)
            .param("multi_value", "param_value1")
            .param("multi_value", "param_value2"));
    sync();

    assertNotNull(handler.paramValues);
    assertEquals(
        new HashSet<>(Arrays.asList("param_value1", "param_value2")),
        new HashSet<>(Arrays.asList(handler.paramValues)));
}
 
Example 2
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 3
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 4
Source File: CheckInEndpoint.java    From MobileShoppingAssistant-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Sends personalized offers to a user that checked in at a place.
 * @param placeId the place from which we want to retrieve offers.
 * @param user the user to whom we send the personalized offers.
 */
private void pushPersonalizedOffers(final String placeId, final User user) {
    // insert a task to a queue
    LOG.info("adding a task to recommendations-queue");
    Queue queue = QueueFactory.getQueue("recommendations-queue");

    try {
        String userEmail = user.getEmail();
        queue.add(withUrl("/tasks/recommendations")
                .param("userEmail", userEmail).param("placeId", placeId));
        LOG.info("task added");
    } catch (RuntimeException e) {
        LOG.severe(e.getMessage());
    }
}
 
Example 5
Source File: PullTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPullPayload() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th = queue.add(withMethod(PULL).payload("foobar".getBytes()).etaMillis(15000));
    sync();
    try {
        List<TaskHandle> handles = queue.leaseTasks(30, TimeUnit.MINUTES, 100);
        assertFalse(handles.isEmpty());
        TaskHandle lh = handles.get(0);
        assertEquals(th.getName(), lh.getName());
    } finally {
        queue.deleteTask(th);
    }
}
 
Example 6
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 7
Source File: DeviceSubscription.java    From solutions-mobile-backend-starter-java with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueues device subscription entity to be deleted.
 *
 * @param time Threshold time before which entities created will be deleted
 * @param cursor Query cursor indicates query result position
 */
protected void enqueueDeleteDeviceSubscription(Date time, String cursor) {
  Queue deviceTokenCleanupQueue = QueueFactory.getQueue("subscription-removal");
  deviceTokenCleanupQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/devicesubscription/delete")
      .param("timeStamp", new Gson().toJson(time, Date.class))
      .param("cursor", cursor)
      .param("type", SubscriptionUtility.REQUEST_TYPE_DEVICE_SUB));
}
 
Example 8
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 9
Source File: PullTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeaseTasksOnlyReturnsSpecifiedNumberOfTasks() {
    Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th1 = queue.add(withMethod(PULL));
    TaskHandle th2 = queue.add(withMethod(PULL));
    sync();
    try {
        int countLimit = 1;
        List<TaskHandle> handles = queue.leaseTasks(10, TimeUnit.SECONDS, countLimit);
        assertEquals(countLimit, handles.size());
    } finally {
        queue.deleteTask(th1);
        queue.deleteTask(th2);
    }
}
 
Example 10
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeaseTasksOnlyReturnsSpecifiedNumberOfTasks() {
    Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th1 = queue.add(withMethod(PULL));
    TaskHandle th2 = queue.add(withMethod(PULL));
    sync(2000);
    try {
        int countLimit = 1;
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksAsync(10, TimeUnit.SECONDS, countLimit));
        assertEquals(countLimit, handles.size());
    } finally {
        queue.deleteTask(th1);
        queue.deleteTask(th2);
    }
}
 
Example 11
Source File: FanoutTask.java    From yawp with MIT License 5 votes vote down vote up
private void fanout() {
    Queue queue = QueueHelper.getPipeForkQueue(pipe);

    for (IdRef<?> sinkId : getSinkIds()) {
        payload.setSinkUri(sinkId);
        queue.add(TaskOptions.Builder.withPayload(new ForkTask(payload)));
    }
}
 
Example 12
Source File: PushNotificationUtility.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 5 votes vote down vote up
protected static void continueEnqueueingPushAlertToAllDevices(
    String alertMessage, String cursorString) {
  Queue preProcessingQueue = QueueFactory.getQueue("notification-preprocessing");
  preProcessingQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/preprocessing")
      .param("alert", alertMessage)
      .param("cursorString", cursorString)
      .param("queryType", "allDevices"));
}
 
Example 13
Source File: TaskQueueTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test(expected = TaskAlreadyExistsException.class)
public void testAddingTwoTasksWithSameNameThrowsException() {
    String taskName = "sameName";
    Queue queue = getDefaultQueue();
    // TODO -- perhaps change this with delay on servlet side?
    queue.add(TaskOptions.Builder.withTaskName(taskName).countdownMillis(10 * 1000L));
    queue.add(TaskOptions.Builder.withTaskName(taskName));
}
 
Example 14
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPullParams() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th = queue.add(withMethod(PULL).param("foo", "bar").etaMillis(15000));
    try {
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksAsync(30, TimeUnit.MINUTES, 100));
        assertFalse(handles.isEmpty());
        TaskHandle lh = handles.get(0);
        assertEquals(th.getName(), lh.getName());
    } finally {
        queue.deleteTask(th);
    }
}
 
Example 15
Source File: AppEngineTaskQueue.java    From appengine-pipelines with Apache License 2.0 5 votes vote down vote up
@Override
public void enqueue(Task task) {
  logger.finest("Enqueueing: " + task);
  TaskOptions taskOptions = toTaskOptions(task);
  Queue queue = getQueue(task.getQueueSettings().getOnQueue());
  try {
    queue.add(taskOptions);
  } catch (TaskAlreadyExistsException ingore) {
    // ignore
  }
}
 
Example 16
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 17
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 18
Source File: CheckInEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
private void pushPersonalizedOffers(String placeId, User user) {
  // insert a task to a queue
  log.info("adding a task to recommendations-queue");
  Queue queue = QueueFactory.getQueue("recommendations-queue");

  try {
    String userEmail = user.getEmail();
    queue.add(withUrl("/tasks/recommendations")
        .param("userEmail", userEmail).param("placeId", placeId));
    log.info("task added");
  } catch (RuntimeException e) {
    log.severe(e.getMessage());
  }
}
 
Example 19
Source File: FlowDropsNamespaceFanoutTask.java    From yawp with MIT License 4 votes vote down vote up
private void enqueueNextBatch(String cursor) {
    Queue queue = QueueHelper.getDefaultQueue();
    queue.add(TaskOptions.Builder.withPayload(new FlowDropsNamespaceFanoutTask(cursor)));
}
 
Example 20
Source File: Utility.java    From io2014-codelabs with Apache License 2.0 4 votes vote down vote up
static void enqueueRemovingDeviceTokens(List<String> deviceTokens) {
  Queue deviceTokenCleanupQueue = QueueFactory.getQueue("notification-device-token-cleanup");
  deviceTokenCleanupQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.POST)
      .url("/admin/push/device/cleanup")
      .param("devices", new Gson().toJson(deviceTokens)));
}