How to execute task in Google App Engine - java

I am trying to execute task in one of Google App Engine project automatically using Java.
To create task , i did following steps.
Step 1) Created one servlet by extending HTTPServlet with url pattern in
web.xml is /task.
I written following code inside doGet() of this servlet to add task
to default queue.
Queue q = QueueFactory.getDefaultQueue();
q.add(TaskOptions.Builder.withUrl("/test").param("key", "123"));
Step 2) Created one more servlet by extending HTTPServlet with url pattern in
web.xml is /test.
I written sent email code when this servlet is exceuted.
So now, I added task to queue with url test.
When my task will execute.
When i execute servlet with url pattern /task it is adding task to queue. But task is not executing.
How my task will execute automatically by Google App Engine.
Thanks.

the task should execute.
go to your google app engine administration console and check your logs and task queues. it's either exceptions or task completed. if completed, it may not show up on the task queue (unless it finished within the past 1 min).
to verify that your task has been submitted successfully, you can try the following:
use the administration console and pause the task queue
submit your task to the task queue (by accessing the url e.g. http://your.appspot.com/task)
go to the administration console and check the task queue. Under the 'Tasks in Queue' column, you should see that the task is still pending.

Related

Creating an asynchronous processo in a java servlet

I want to create a java servlet that runs an .exe application in the server but as an asynchronous process and returns to the client a hash or token to indentify the process so the client can ask later if the process has finished.
For example:
A user writes in the web browser something like: http://server:8080/videomanager?action=process
The servlet receives the request and execute the .exe application but it doens't wait for it to finish, it inmediately responds to the user with a token/hash=12312312ddsadad31231
The user recieves this token in his web browser
The user can then send a request like: http://server:8080/videomanager?action=status&token=12312312ddsadad31231
The servlet recieves this request and checks if the process it run before (with token 12312312ddsadad31231) is still running and responds to the user with "Finished" or "Still running"
I know that I can use ProcessBuilder or Runtime.exec() to run the .exe application, but I don't know how to make the servlet leave the process running and responds.
Thanks!
Regards,
Xtian
The Process returned by ProcessBuilder is run asynchronously in the background (it's a separate process), so as soon as you call .start() you can have the servlet respond.
If you're seeing the subprocess never finish that would be because you're not reading its output, which you must do in order for the process to continue. You may want to do this in a separate background thread.
This is one of the ways to implement this:
Create a runnable that executes the command (Runtime.exec etc) (a few other things will follow)
Create an id lets say a UUID and initialize the status against the id on a persistant storage to say PENDING
Use a ThreadPoolExecutor, add the runnable to the workQueue allocated to the threadpool.
When the runnable starts executing, it may change the status in say DB to IN_PROGRESS
When the task is completed you can mark the status as COMPLETE
Now at any point of time for the request coming in you shall be able to check the status of the task and respond back accordingly.

App engine task queue security context

I am investigating how to run background tasks in App Engine.
It seems that task queue is a good choice. But how does task queue deal with user login? My background task requires a user to be logged in.
My understanding is when task queue runs a task, it sends a HTTP quest to my app, but if all my resources are protected and requires a user context, how should I handle that?
Task queues are allowed to use admin-only endpoints, so for example you can include a user ID in your task's payload, run it through an admin-only URL, then just trust that the user ID is correct when it runs.
This is explained in Google's documentation here
Your servlet could rely on task queue specific headers (see https://cloud.google.com/appengine/docs/java/taskqueue/push/creating-handlers#reading_request_headers). These are set internally by Google App Engine, so you can trust it.
Also you can use parameters (see https://cloud.google.com/appengine/docs/java/taskqueue/push/example) to pass any data to queue task processor (user id, token etc.).

Send notification from Google App Engine Task to UI

My Google App Engine JSP needs to perform a lengthy processing so it adds the task to Task Queue, then refreshes every 30 sec waiting for task completion. How the task can let JSP know about its status? I tried to use session but it seems session objects are not shared between JSP and tasks. I tried to throw exceptions from the task in case if it fails hoping to launch error page (I did configure error-page in web.xml) but it didn't work either.
The answer for this kind of problems is implement some kind of "Web Hook" where when tasks is finished it will call back the JSP.
Another option is to implement AJAX, where this asynchronous call will check the status of the task then will update the UI as necessary.
If you can give more context into the question it would be better.

poller process in activiti

I have created an activiti process for the lifecycle of an email which passes many service tasks.
Now I have another process that polls emails from a queue and starts the new activiti email process everytime a new email is polled.
the poller process should never stop polling.
I understand that an activiti process(the poller) is not meant to never end?
I tried to make this work, but when an error occurs in 1 of my activiti email processes, the poller process also stops.
How should this be made?
I am thinking about the REST API?
(I create my poller outside of activiti, and I call the api everytime a new email process needs to be started?)
I really want to keep everything within my activiti explorer, but is this possible?
You can set the timer for the polling process. such tat polling can happen at periodic intervals.
Do check about Activiti timer events. The timer can also be added at the process level, its not just for tasks.

Running a continuous background task in Play 2.4.2 server

I'm creating a web application that displays incoming events as strings onto the web browser. The events should be coming in from a listener that I have defined in another part of the program. These events will be emitted through a socket to the client side.
The problem: I need to keep the listener continually running in order to receive messages. The listener is essentially one block of code that's something like listener.run(), and it doesn't terminate. Right now, I am making a separate thread that runs this block of code and I have configured Global settings to run the thread on application startup.
I don't want my task to stop and start every time I refresh the page. How can I make the listener run right when the server starts?
Look at the Scheduling asynchronous tasks at the end of the doc: https://www.playframework.com/documentation/2.4.x/JavaAkka

Categories