We are building analytics tool which collect events from web site and mobile apps. We want to process the request with Task queue in Google App engine. When i refer the doc in Google Developers site
Queue queue = QueueFactory.getDefaultQueue();
queue.add(withUrl("/analytics").param("id", String.valueOf(id)));
It has option to give only param. But how do i pass entire HTTP request to task queue and process them?
I assume the main problem here is how to pass all the request parameters and the request body to the task.
Unfortunately there is no simple "relay" or "redirect" method to move your request to the task queue (but it would be nice). You have to use the Queue.add(TaskOptions taskOptions) method to add your task.
The recommended way to instantiate a TaskOptions object is to statically import TaskOptions.Builder.* and invoke a static creation method followed by an instance mutator (if needed).
And use one of the following (or any other payload() methods):
TaskOptions.payload(byte[] payload);
TaskOptions.payload(byte[] payload, String contentType);
TaskOptions.payload(String payload);
to set the content of the request. You can get the payload by reading it from the request.getInputStream().
The request parameters (if they are part of the URL and not the result of a form POST for example) you have to manually copy each with e.g. TaskOptions.param(String name, String value).
Related
I want to add a parameter to the Cloud Tasks that can then be retrieved from the Task handler using:
request.getParameter("paramName");
Previously in App Engine Standard I would do the following:
TaskOptions options = TaskOptions.Builder.withUrl(backURL)
.param("paramName", "value")
.method(Method.POST);
How do I accomplish the same using the Cloud Tasks java client library. It seems like in the AppEngineHttpRequest builder there should be a setParameter option but that doesn't exist.
AppEngineHttpRequest request = AppEngineHttpRequest.newBuilder()
.setRelativeUri(backURL)
.setHttpMethod(HttpMethod.POST)
.build();
Looking at the article called HTTP Target tasks we see an example of building a task in Java. Within the configuration, we see two primary setters .. namely body and url. I am thinking that what you are wanting to set are request query parameters. If this were a plain request it would be:
https://somehost.com/somepath?someParam=someValue
If this holds, then it is likely that if you want to pass in query parameters with your task, you would add them to the uri used to invoke the task handler.
I am writing a REST API in JAX-RS 2.0, JDK 8 for the below requirement
POST API /server/fileUpload/ (Multipart Form data) where I need to send a Big .AI (Adobe Illustrator) File in this.
The Server, takes the file and return Status 202 (Accepted), Acknowledging that file transfer happened Successfully. (From endpoint to Server)
Now at the Server, I am using Java + Imagemagik to convert .AI File (20-25 MB File) to small JPG Thumbnail, place on a Apache HTTP Server and share the location (like http://happyplace/thumbnail0987.jpg)
Now the Second Response should come from Server with Status 200 OK and Thumbnail URL
is it feasible with one REST API? (Async/similar)
or should I split it to 2 API calls, Please suggest
No. In http, one request gets one response. The client must send a second request to get a second response.
You can use WebSockets for that.
If you are calling from script the call will be async you can handle the Thumbnail URL when you get a response. When you are calling from java program i suggest to run it on a different thread, If the execution is not sequential i.e ( Remaining lines can be executed without getting URL). If url is needed for the remaining section of code you can make one call and wait for the response then execute remaining code.
You need to make different APIs for both scenarios. One for showing file upload status and another for all file conversion and manipulation.
On the client side second request must be callback of first request.
The best way to handle these kind of scenario is to use Java Reactive (Project Reactor, WebFlux).
You can return two response using custom middlewares in asp.net (however not recommended).
Return response from one middleware and subsequently you can invoke next middleware and return second response from second middleware
I saw the below post from "cmd" which was posted couple of years back. And "Wojtek Owczarczyk" was answered this one. I am good with all the answer, except last line.
My Confusion is, If we return immediately with ACCEPTED status. Then, we will lost the track of the request.
So i am planning to implement below steps. Please correct me if i am wrong.
1) As soon as the request hits service api - I will create one Job Id and persist my request detail and send back the client with ACCEPTED status code along with Job id.
2) Then, i will create the new thread for that request to continue with the requested operation.
3) After successful completion of Operation, I will send back the client with all status of the request.
4) Finally, in callbackCompletion register i will remove the job id from my persistence list.
To implement the above logic, i need client to send his listener information along with request (basically URI). This is to update the request status to client back, after processing the request.
REST with JAX-RS - Handling long running operations
This is not how REST is meant to work in my opinion. I would do the following approach instead:
Client makes a request for a long operation
Create a job id and run the job asynchronously
Return the accepted status together with the a URI to request the status for the job. For example: http://.../resources/jobs/1234
The client is now responsible e.g. to poll the URI to get the current status of the job execution.
I'm trying to send message to online user by user's servlet response object which is already stored in map. For Example if user A is to send message to B then i will get data from A's request object and write it to B's response object(from map). I'm trying it for avoid timed ajax call. Any suggestion and help. I'm getting message when reload the jsp page.can we have object listener in jsp.
IMHO,The servlet response will be sent back to the client when the doGet or Post method terminates, it won't wait for your asynchronous call(time that getting data from A's ) to finish.
You may face the issue "response alredy has been committed"
if user A is to send message to B then i will get data from A's request object and write it to B's response object(from map).
The request object of A and the response object of B, will be in different threads running the Servlet's service() method .I think you need to store the data sent using A's request in some app context probably and push it to B using AJAX or when user B makes a request fetch the data from context and send it to response to B.
Don't do this - don't mix requests and responses from different servlet calls.
Either use Ajax or periodic refresh using javascript or something similar.
You can use Comet (server push) but really Ajax or refresh are natural for your use case.
(Unless you have other concerns which you didn't share)
I have a question about ActiveMQ and the AJAX Interface concerning the life span of a message. In the AMQ web interface, I can set a TimeToLive Value for a message in milliseconds.
I've already found out, that I can use this parameter via REST:
curl -vd body="test" "http://localhost:8161/demo/message/TESTQUEUE?type=queue&JMSTimeToLive=500&JMSPersistent=-1"
This example message will live 500ms
But how can I use the AMQ Ajax Interface to set those parameters?
The JavaScript function to send a message provides only two parameters
amq.sendMessage(myDestination,myMessage);
Info: http://activemq.apache.org/ajax.html
myDestination is unfortunately not an URL, it's something like this "queue://"
Thanks four your help
Regards
Rolf
The current implementation of the AJAX client does not offer the possibility to send a message with a time to live.
The time to leave of the message is basically set in the message property (headers), via the property "JMSExpiration"
Currently if you go through the amq.js code, you see there is no API that allows you to define the headers or Time to Live.
It should be relatively easy to add this feature to the client. Check the code, you could probably just hardcode the TTL for your application. At the end, it just does a post command in the same way that you do your REST call.