1

My project is based on GAE/J and utilizing the recent launched PULL queue, but I think the question can also be applied to Python.

Basically, when I put a task into the PULL queue, I need to set some params of the task for the later consumer to pick it up.

I have implemented in the params setting in both ways:

1) By using param():

TaskOptions taskOptions = TaskOptions.Builder.
                withMethod(TaskOptions.Method.PULL);
taskOptions.param("param", paramValue);
taskOptions.param("param2", paramValue2);

2) By using payload():

TaskOptions taskOptions = TaskOptions.Builder.
                    withMethod(TaskOptions.Method.PULL);
taskOptions.payload("payloadValue");

Both approaches are working, however, what I would like to know is what's the differences between the two, and which way should be the preferred way in terms of efficiency or convenience.

I can see that by using param(), it is easy to set multiple parameters and also easy to retrieve the parameters for the consumer. But for one parameter cases, then payload may come in more handy as it saves the code to catch Exceptions throwing out when the consumer extract parameters.

However, I would be happy to know any more differences between these two apart from what I have menitoned.

Yudong Li
  • 1,784
  • 2
  • 17
  • 32

2 Answers2

1

Per the python documentation, I would say that in your case is exactly the same.

In PULL requests, Do not specify params if you already specified a payload. Params are encoded as application/x-www-form-urlencoded and set to the payload.

Sebastian Kreft
  • 7,819
  • 3
  • 24
  • 41
1

There is difference in .param() and .payload() functions of TaskOptions. You can use these functions as follows;

  1. taskOptions.param("param1","Invoice_3344"); Now at receiver end, lets say you are calling a servlet , the in the HttpRequest, you can receive the sent parameters as request parameter.

    public class MyInvoiceTask extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String invoiceNum = request.getParameter("param1"); } }

  2. Now Assume you wanted to serialize your entire custom class object which has huge data. In such case you would need to user .payload() function as internally, it send the payload data in a request body.

    //**Custom class object Person person = new Person("Abc", "Mumbai", 22);

    //**Convert the object into JSON so that can be converted into String(required for Payload)

    //**Use Gson library Gson gson = new Gson(); String personObjString = gson.toJson(person); //**put the payload in task option as byte array taskOption.payload(personObjString.toByteArray());

    Now at receiver end lets say using servlet, then from HttpRequest object, we would need to get the payload byte array and convert it back into cutsom object i.e. "Person" class object in our case. private byte[] getPayloadFromHttpRequest(HttpServletRequest req) throws IOException { InputStream inputStream = req.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int length;
        byte[] buffer = new byte[1024];
    
        while ((length = inputStream.read(buffer)) >= 0)
            byteArrayOutputStream.write(buffer, 0, length);
    
        if (byteArrayOutputStream.size() > 0){
            return byteArrayOutputStream.toByteArray();
        }
        return null;
    }
    

    //**Now this received byteArray can be used with Gson to convert it back into Person object byte[] payload = getPayloadFromHttpRequest(request); Gson gson = new Gson(); String personJsonString = new String(payload); Person person = gson.fromJson(personJsonString, Person.class);

Aashish
  • 81
  • 2
  • 5