Eurig Jones
Eurig Jones

Reputation: 8543

How to get a TaskQueue payload?

I'm using TaskOptions.payload(String ) method to write a small JSON object into a POST taskqueue.

But how do I get it out to read in the servlet when executing the task queue?

Upvotes: 0

Views: 986

Answers (2)

Eurig Jones
Eurig Jones

Reputation: 8543

Here's what I did in the end. Ran this code in the doPost()..

import org.codehaus.jackson.map.ObjectMapper;
import my.own.PayloadObject;

...

private static final ObjectMapper MAPPER = new ObjectMapper();

...

private PayloadObject getPayload(HttpServletRequest req) throws IOException
{
    InputStream inputStream = req.getInputStream();
    ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();

    int length;
    byte[] buffer = new byte[1024];

    while ((length = inputStream.read(buffer)) >= 0)
        byteArrayStream.write(buffer, 0, length);

    if (byteArrayStream.size() > 0)
        return MAPPER.readValue(byteArrayStream.toByteArray(), PayloadObject.class);

    return null;
}

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80340

If you are using servlets than you must implement a doPost(..) method, where you get the request body and parse it as JSON: HttpServletRequest get JSON POST data

Upvotes: 2

Related Questions