Send notification from Google App Engine Task to UI - java

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.

Related

I need to wait for a request response in the main thread Android Studio

I need to make a request to an endpoint in the app startup, i really need to wait for the response to let the app continue running, so i need to make a sync request
how i can do it?
Any long-running sync request on the main thread will definitely trigger Application Not Responding (ANR) error.
It's highly recommended to move each IO (network, storage) operation to a worker thread. To achieve this you can use a stack of Kotlin Coroutines, Retrofit and Moshi.
To prevent the app from running until the network response, you should implement splash screen. Configure its lifetime as explained here.
i really need to wait for the response to let the app continue running, so i need to make a sync request
No, you don't. You need to provide some temporary UI that does nothing (like a loading spinner) while the async request completes.
Read up on coroutines for background work.
Making a "sync" request is just going to hang your app.

JAVA web application Asynchronous push notification to let user know status of long running job

I have a spring mvc/struts, hibernate application. We ran into a problem where in we are executing a long running on one of application page. We would like to execute this process in background however we want to show status of the process to end client when process gets completed. The condition is user can navigate to a different page and even on different page he/she should be notified with a dialog box or so for a long running process which was initiated bye him/her.
Any help would be appreciated
Thanks in advance
Anant
Execute the task in a thread, and either poll with ajax through an iframe, or push updates to the client using websockets

Perform Asynchronus db tasks in java servlet application

I have a jsp/servlet based web app.
I have a button "Clean Up" which calls a servlet and the request goes till DAO class.DAO class performs different DB activities like, moving data from Master table to backup table, then deleting data from master table etc.
As of now this activity is Synchronous and user needs to wait until a response is sent.
I want to implement the same scenario as an Asynch task with user just getting a message as
" Clean Up Activities Triggered"
What could be the best/easiest way to perform this task. I cannot use scheduler.
My Container is TomCat.
Simplest but a different solution for this could be to use some AJAX behavior in the client side. There are lot of simple/powerful frameworks(JS files) to help you achieve AJAX in your page. Using AJAX, you just submit the request asynchronously and display the client side message "Clean Up Activities Triggered", while request is being processed in the server side. If user wait, server process will return and display a "success" message otherwise user is free to navigate other pages or perform other actions.
ExecutorService is the most robust solution. Creating a simple thread is enough as well. However the bigger problem is synchronization. Use Semaphore to control whether two users aren't cleaning up simultaneously.
we did this for our project once and it worked pretty well.
We sent the 200 ok to the user as long as there no issues processing the request. And we used the java executorservice to do the cleanup.
And in case something went wrong notified the user separately.

How to execute task in Google App Engine

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.

Best method of triggering a shell script from Java

I have a shell script which I'd like to trigger from a J2EE web app.
The script does lots of things - processing, FTPing, etc - it's a legacy thing.
It takes a long time to run.
I'm wondering what is the best approach to this. I want a user to be able to click on a link, trigger the script, and display a message to the user saying that the script has started. I'd like the HTTP request/response cycle to be instantaneous, irrespective of the fact that my script takes a long time to run.
I can think of three options:
Spawn a new thread during the processing of the user's click. However, I don't think this is compliant with the J2EE spec.
Send some output down the HTTP response stream and commit it before triggering the script. This gives the illusion that the HTTP request/response cycle has finished, but actually the thread processing the request is still sat there waiting for the shell script to finish. So I've basically hijacked the containers HTTP processing thread for my own purpose.
Create a wrapper script which starts my main script in the background. This would let the request/response cycle to finish normally in the container.
All the above would be using a servlet and Runtime.getRuntime().exec().
This is running on Solaris using Oracle's OC4J app server, on Java 1.4.2.
Please does anyone have any opinions on which is the least hacky solution and why?
Or does anyone have a better approach? We've got Quartz available, but we don't want to have to reimplement the shell script as a Java process.
Thanks.
You mentioned Quartz so let's go for an option #4 (which is IMO the best of course):
Use Quartz Scheduler and a org.quartz.jobs.NativeJob
PS: The biggest problem may be to find documentation and this is the best source I've been able to find: How to use NativeJob?
I'd go with option 3, especially if you don't actually need to know when the script finishes (or have some other way of finding out other than waiting for the process to end).
Option 1 wastes a thread that's just going to be sitting around waiting for the script to finish. Option 2 seems like a bad idea. I wouldn't hijack servlet container threads.
Is it necessary for your application to evaluate output from the script you are starting, or is this a simple fire-and-forget job? If it's not required, you can 'abuse' the fact that Runtime.getRuntime().exec() will return immediately with the process continuing to run in the background. If you actually wanted to wait for the script/process to finish, you would have to invoke waitFor() on the Process object returned by exec().
If the process you are starting writes anything to stdout or stderr, be sure to redirect these to either log files or /dev/null, otherwise the process will block after a while, since stdout and stderr are available as InputStreams with limited buffering capabilites through the Process object.
My approach to this would probably be something like the following:
Set up an ExecutorService within the servlet to perform the actual execution.
Create an implementation of Callable with an appropriate return type, that wraps the actual script execution (using Runtime.exec()) to translate Java input variables to shell script arguments, and the script output to an appropriate Java object.
When a request comes in, create an appropriate Callable object, submit it to the executor service and put the resulting Future somewhere persistent (e.g. user's session, or UID-keyed map returning the key to the user for later lookups, depending on requirements). Then immediately send an HTTP response to the user implying that the script was started OK (including the lookup key if required).
Add some mechanism for the user to poll the progress of their task, returning either a "still running" response, a "failed" response or a "succeeded + result" response depending on the state of the Future that you just looked up.
It's a bit handwavy but depending on how your webapp is structured you can probably fit these general components in somewhere.
If your HTTP response / the user does not need to see the output of the script, or be aware of when the script completes, then your best option is to launch the thread in some sort of wrapper script as you mention so that it can run outside of the servlet container environment as a whole. This means you can absolve yourself from needing to manage threads within the container, or hijacking a thread as you mention, etc.
Only if the user needs to be informed of when the script completes and/or monitor the script's output would I consider options 1 or 2.
For the second option, you can use a servlet, and after you've responded to the HTTP request, you can use java.lang.Runtime.exec() to execute your script. I'd also recommend that you look here : http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
... for some of the problems and pitfalls of using it.
The most robust solution for asynchronous backend processes is using a message queue IMO. Recently I implemented this using a Spring-embedded ActiveMQ broker, and rigging up a producing and consuming bean. When a job needs to be started, my code calls the producer which puts a message on the queue. The consumer is subscribed to the queue and get kicked into action by the message in a separate thread. This approach neatly separates the UI from the queueing mechanism (via the producer), and from the asynchronous process (handled by the consumer).
Note this was a Java 5, Spring-configured environment running on a Tomcat server on developer machines, and deployed to Weblogic on the test/production machines.
Your problem stems from the fact that you are trying to go against the 'single response per request' model in J2EE, and have the end-user's page dynamically update as the backend task executes.
Unless you want to go down the introducing an Ajax-based solution, you will have to force the rendered page on the user's browser to 'poll' the server for information periodically, until the back-end task completes.
This can be achieved by:
When the J2EE container receives the request, spawn a thread which takes a reference to the session object (which will be used to write the output of your script)
Initialize the response servlet to write an html page which will contain a Javascript function to reload the page from the server at regular intervals (every 10 seconds or so).
On each request, poll the session object to display the output stored by the spawned thread in step 1
[clean-up logic can be added to delete the stored content from the session once the thread completes if needed, also you can set any additional flags in the session for mark state transitions of the execution of your script]
This is one way to achieve what you want - it isn't the most elegant of all approaches, but it is essentially due to needing to asynchronously update your page content from the server , with a request/response model.
There are other ways to achieve this, but it really depends on how inflexible your constraints are. I have heard of Direct Web Remoting (although I haven't played with it yet), might be worth taking a look at Developing Applications using Reverse-Ajax

Categories