How to add a task to Push Queue from a different module?

49 views
Skip to first unread message

Paul Canning

unread,
Nov 16, 2015, 7:16:18 AM11/16/15
to Google App Engine
I have two "modules", one being the main application and the other acting as an API.

Both use the same Cloud SQL and Storage.

How can I, in my code, add a task, from the API, to the Push Queue and have it run code that sits in the main application module?

Is it simply a case of adding a task to a certain queue (by name) that targets the main app?

Kurt Hartmann

unread,
Nov 16, 2015, 8:51:55 AM11/16/15
to Google App Engine
I'm doing this very thing in my Java app. 

  • In the main module, you need to add a dispatch.xml file to define a mapping to your API module.  In my case, the other module is called tasks.
    • Below is a nnippet from my dispatch.xml showing the URL mapping to the other module called tasks
    • This file will be located in the same directory as appengine-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<dispatch-entries>
    ...
    <dispatch>
        <url>*/tasks/*</url>
        <module>tasks</module>
    </dispatch>
</dispatch-entries>

  • Then, I set up a little helper class to add a task to a push queue.  Be sure to set the Host property in the header, otherwise it won't work.
  • I then dependency inject the PushTaskHelper, wherever I need it and call the helper method

public class PushTaskHelper {
    public static final String MODULE_WEB_TASKS_NAME = "tasks";
    public static final String MODULE_WEB_TASKS_VERSION = "v1";

    public void addTaskToQueue(String controllerPath) {
        String url = TASKS_MODULE_CONTEXT_ROOT + controllerPath;
        TaskOptions task = TaskOptions.Builder.withUrl(url).method(TaskOptions.Method.GET)
                .header("Host", ModulesServiceFactory.getModulesService().getVersionHostname(MODULE_WEB_TASKS_NAME, MODULE_WEB_TASKS_VERSION));
        QueueFactory.getDefaultQueue().add(task);
    }
    ...

Paul Canning

unread,
Nov 16, 2015, 8:58:51 AM11/16/15
to Google App Engine
I just did some testing.

I didn't need to use the dispatch.yaml, I simply had a named queue that targets the main app module.

The code in the API then adds the Task to this named queue.

Didn't need to use a Host header in the Task options.

Kurt Hartmann

unread,
Nov 16, 2015, 9:09:14 AM11/16/15
to Google App Engine
Good to know.  It's been a while since I looked at that code, but I seem to recall the host header was necessary for me.
Reply all
Reply to author
Forward
0 new messages