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

The following examples show how to use com.google.appengine.api.taskqueue.QueueFactory#getQueue() . 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 testParams() throws Exception {
    class ParamHandler implements PrintServlet.RequestHandler {
        private String paramValue;

        public void handleRequest(ServletRequest req) {
            paramValue = req.getParameter("single_value");
        }
    }

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

    final Queue queue = QueueFactory.getQueue("tasks-queue");
    queue.add(withUrl(URL).param("single_value", "param_value"));
    sync();

    assertEquals("param_value", handler.paramValue);
}
 
Example 2
Source File: WorkerServlet.java    From io2014-codelabs with Apache License 2.0 6 votes vote down vote up
private void doPolling() {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");

  Worker worker = new Worker(notificationQueue);
  while (!LifecycleManager.getInstance().isShuttingDown()) {
    boolean tasksProcessed = worker.processBatchOfTasks();
    ApiProxy.flushLogs();

    if (!tasksProcessed) {
      // Wait before trying to lease tasks again.
      try {
        Thread.sleep(MILLISECONDS_TO_WAIT_WHEN_NO_TASKS_LEASED);
      } catch (InterruptedException e) {
        return;
      }
    }
  }

  log.info("Instance is shutting down");
}
 
Example 3
Source File: AsyncTasksTest.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();
    waitOnFuture(defaultQueue.addAsync(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");
    waitOnFuture(testQueue.addAsync(withTaskName(name2)));
    sync();

    request = TestQueueServlet.getLastRequest();
    assertEquals("test", request.getHeader(QUEUE_NAME));
    assertEquals(name2, request.getHeader(TASK_NAME));
}
 
Example 4
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMultipleWithSameTag() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th1 = queue.add(withMethod(PULL).tag("barfoo2").payload("foobar").etaMillis(15000));
    TaskHandle th2 = queue.add(withMethod(PULL).tag("barfoo2").payload("foofoo").etaMillis(10000));
    try {
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksByTagAsync(30, TimeUnit.MINUTES, 100, "barfoo2"));
        assertEquals(2, handles.size());

        Set<String> expectedTasks = taskHandlesToNameSet(th1, th2);
        Set<String> returnedTasks = taskHandleListToNameSet(handles);
        assertEquals(expectedTasks, returnedTasks);

    } finally {
        queue.deleteTask(th1);
        queue.deleteTask(th2);
    }
}
 
Example 5
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testPullMultipleWithDiffTag() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th1 = queue.add(withMethod(PULL).tag("barfoo3").payload("foobar").etaMillis(15000));
    TaskHandle th2 = queue.add(withMethod(PULL).tag("qwerty").payload("foofoo").etaMillis(10000));
    TaskHandle th3 = queue.add(withMethod(PULL).tag("barfoo3").payload("foofoo").etaMillis(10000));
    try {
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksByTagAsync(30, TimeUnit.MINUTES, 100, "barfoo3"));
        assertEquals(2, handles.size());
        Set<String> expectedTasks = taskHandlesToNameSet(th1, th3);
        Set<String> returnedTasks = taskHandleListToNameSet(handles);
        assertEquals(expectedTasks, returnedTasks);

        handles = queue.leaseTasksByTag(30, TimeUnit.MINUTES, 100, "qwerty");
        assertEquals(1, handles.size());
        Set<String> expectedTasks2 = taskHandlesToNameSet(th2);
        Set<String> returnedTasks2 = taskHandleListToNameSet(handles);
        assertEquals(expectedTasks2, returnedTasks2);

    } finally {
        queue.deleteTask(th1);
        queue.deleteTask(th2);
        queue.deleteTask(th3);
    }
}
 
Example 6
Source File: TaskQueuesLogic.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the given task, to be run after the specified time, to the specified queue.
 *
 * @param task the task object containing the details of task to be added
 * @param countdownTime the time delay for the task to be executed
 */
public void addDeferredTask(TaskWrapper task, long countdownTime) {
    Queue requiredQueue = QueueFactory.getQueue(task.getQueueName());
    TaskOptions taskToBeAdded = TaskOptions.Builder.withUrl(task.getWorkerUrl());
    if (countdownTime > 0) {
        taskToBeAdded.countdownMillis(countdownTime);
    }

    for (Map.Entry<String, String[]> entry : task.getParamMap().entrySet()) {
        String name = entry.getKey();
        String[] values = entry.getValue();

        for (String value : values) {
            taskToBeAdded = taskToBeAdded.param(name, value);
        }
    }

    requiredQueue.add(taskToBeAdded);
}
 
Example 7
Source File: TaskQueueUtilsTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteTasks_usesMultipleBatches() {
  Queue defaultQ = QueueFactory.getQueue("default");
  TaskOptions taskOptA = withUrl("/a").taskName("a");
  TaskOptions taskOptB = withUrl("/b").taskName("b");
  TaskOptions taskOptC = withUrl("/c").taskName("c");
  taskQueueUtils.enqueue(defaultQ, ImmutableList.of(taskOptA, taskOptB, taskOptC));
  assertThat(getQueueInfo("default").getTaskInfo()).hasSize(3);

  taskQueueUtils.deleteTasks(
      defaultQ,
      ImmutableList.of(
          new TaskHandle(taskOptA, "default"),
          new TaskHandle(taskOptB, "default"),
          new TaskHandle(taskOptC, "default")));
  assertThat(getQueueInfo("default").getTaskInfo()).hasSize(0);
}
 
Example 8
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 9
Source File: PullQueueTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    queue = QueueFactory.getQueue(E2E_TESTING_PULL);
    purgeAndPause(queue);

    timeStamp = Long.toString(System.currentTimeMillis());
    payload = "mypayload";
}
 
Example 10
Source File: PullQueueAsyncTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    queue = QueueFactory.getQueue(E2E_TESTING_PULL);
    purgeAndPause(queue);

    timeStamp = Long.toString(System.currentTimeMillis());
    payload = "mypayload";
}
 
Example 11
Source File: ReadDnsQueueActionTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private void run() {
  ReadDnsQueueAction action = new ReadDnsQueueAction();
  action.tldUpdateBatchSize = TEST_TLD_UPDATE_BATCH_SIZE;
  action.requestedMaximumDuration = Duration.standardSeconds(10);
  action.clock = clock;
  action.dnsQueue = dnsQueue;
  action.dnsPublishPushQueue = QueueFactory.getQueue(DNS_PUBLISH_PUSH_QUEUE_NAME);
  action.hashFunction = Hashing.murmur3_32();
  action.taskQueueUtils = new TaskQueueUtils(new Retrier(null, 1));
  action.jitterSeconds = Optional.empty();
  // Advance the time a little, to ensure that leaseTasks() returns all tasks.
  clock.advanceBy(Duration.standardHours(1));

  action.run();
}
 
Example 12
Source File: AsyncTasksTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void assertServletReceivesCorrectMethod(TaskOptions.Method method) {
    MethodRequestHandler handler = new MethodRequestHandler();
    PrintServlet.setRequestHandler(handler);

    Queue queue = QueueFactory.getQueue("tasks-queue");
    waitOnFuture(queue.addAsync(withUrl(URL).method(method)));
    sync();

    assertEquals("Servlet received invalid HTTP method.", method.name(), handler.method);
}
 
Example 13
Source File: PullAsyncTest.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").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 14
Source File: PullAsyncTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testPullWithTag() throws Exception {
    final Queue queue = QueueFactory.getQueue("pull-queue");
    TaskHandle th = queue.add(withMethod(PULL).tag("barfoo1").etaMillis(15000));
    try {
        List<TaskHandle> handles = waitOnFuture(queue.leaseTasksByTagAsync(30, TimeUnit.MINUTES, 100, "barfoo1"));
        assertFalse(handles.isEmpty());
        TaskHandle lh = handles.get(0);
        assertEquals(th.getName(), lh.getName());
    } finally {
        queue.deleteTask(th);
    }
}
 
Example 15
Source File: Utility.java    From io2014-codelabs with Apache License 2.0 4 votes vote down vote up
private static void enqueuePushAlertToDevices(String alertMessage, String devicesAsJson) {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");
  notificationQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
      .param("alert", alertMessage)
      .param("devices", devicesAsJson));
}
 
Example 16
Source File: PushNotificationUtility.java    From solutions-ios-push-notification-sample-backend-java 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)));
}
 
Example 17
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 18
Source File: DnsModule.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Provides
@Named(DNS_PUBLISH_PUSH_QUEUE_NAME)
static Queue provideDnsUpdatePushQueue() {
  return QueueFactory.getQueue(DNS_PUBLISH_PUSH_QUEUE_NAME);
}
 
Example 19
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)));
}
 
Example 20
Source File: PushNotificationUtility.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 4 votes vote down vote up
private static void enqueuePushAlertToDevices(String alertMessage, String devicesAsJson) {
  Queue notificationQueue = QueueFactory.getQueue("notification-delivery");
  notificationQueue.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
      .param("alert", alertMessage)
      .param("devices", devicesAsJson));
}