Execute and Wait for Rest Webservice - java

I need to implement an application that allow users to upload file. The application will then process the file and then return result.
My current application is using:
Spring
Hibernate
Rest web service
Now here is the problem.
What I want is to when the users upload a file, the web service should execute and wait for a response until the server return a result. On my client I should be able to get the current status of the file upload processing. If is still in processing, it should be return some value to denote processing. Timeout should not occur as well while waiting.
How do I go about doing it? Any advice?

Related

Jersey update user on front-end while a big request is running

Currently I'm working on a single page application with java/jersey running as my back-end. But at the moment I have some requests that take a while (over 10 seconds). I was wondering if its possible to send updates back to the client with jersey?
I wanna use like a status bar but I have no clue how far the request is without updates from the back-end.
I couldn't find anything about this topic searching on google/stackoverflow. Maybe I'm using the wrong search terms.
If you don't want to use websockets there are a few approaches you can take.
Provide API to client that takes clientId and optionally processId and gives status of the process running on server.
Then client can have Javascript to asynchronously call this API and update progress bar.
In addition you can have server side Jersey resource start long running process asynchronously and immediately return response with estimated time and processId.

Per user level synchronization in Spring MVC

I am designing an upload functionality with Spring MVC. All uploads from the client reach an endpoint which handles an ajax call. The controller that is mapped to the endpoint calls a function in a "#Service" class.
This function does the actual upload by uploading it to my cloud file system, and modifying a session variable. The problem is that it takes a while for the upload to be completed. Thus, the time for my controller to return prolongs. I want my controller to return right away by starting a thread to handle the upload and then return. If multiple uploads from the same client call the endpoint at the same time, I want to ensure a synchronized access to the session variable, how do I do this?
Spring uses servlet technology, different thread is created for each request on a servlet ( not different instances ) so what you really want to accomplish regarding the threads is already done.

Communication servlet to jsp without client request

I'm working on a project where we use Tomcat 8 as a application server and hence JSP and Servlets. Each JSP displays a kind of project list. The user of the application has the possibilty to simulate a project. What this means is not that important. When the user clicks the 'simulate'-button, the following process will be performed:
Via Ajax (javascript) a servlet will be executed which produces a zip file and stores the file on the hard disk of the server
a 'simulation'-flag will be set in the database for the respective project
a external java program (at the backend) listens for the 'simulation'-flag and if a flag for a project is set, the external program grabs the zip file stored by the web application
the external java program then runs the simulation for this project.
when the simulation is done, the external java program stores a new zip file on the hard disk of the server, sets a 'hey, I'm ready'-flag in the database and calls a servlet of the web application
this servlet saves the information of the data in the new zip file to the database
finish
So my question is: What is the best way to inform the JSP that the simulation is done?
The JSP still displays that the project is in simulation progress. My current idea is the following:
I could periodically send a Ajax request to the server to check the database for the 'hey I'm ready'-flag and if it is set, I could display the result on the JSP. But I've got the feeling that there are smarter solutions.
Maybe I could use the last servlet call (see point 5) to somehow inform the JSP that the simulation is done? The problem is that this servlet is not called by the client but by the external java program.
Do you have any suggestions? Or is there any technology I should read about?
Thank you.
using asynchronous ajax calls means that the function will return when the servlet finishes it's job and returns some HTTP code (200 for success).
so this example:
$.ajax({
url: Config.serverUrl+'/simulationServlet',
type: 'GET',
success: function(result) {
$('#infoBox').text("simulation is over");
}
});
will show that the simulation is done.

How to return from a process to a frontend backend process?

I need load 10.000 rows in my database google cloud sql using AppEngine with Java. For this case, i use a proccess using backend, but i want advertise to user, how rows was wrong load? But, i don't know as send a message from my backend proccess to my front to show a message to screen.
Regards.
Maybe you don't need to send callback (from backend to front-end). Maybe you should make the front-end poll to see when these results are ready at the back-end side. Maybe through some JS/Ajax code which keeps polling on the background and once the results are ready, pulls them and displays them in the designated area of the page. I assume your front-end is a web page.

detecting if a file is ready and serving it for download

I have a Spring MVC web application that is generating a report on the server, once the report is generated, I need to enable a button that allows the user to download it. I am not sure how to go about doing this.
I figured that I will have to spawn off a thread that will just keep checking for the existence of the file and use javascript (jQuery or prototype most likely) to handle the UI elements, but I'm just not sure how to tie these all together.
There are no threads in Javascript. Instead you'll set a timeout to do the polling. The polling would take the form of a URL that will respond with some sort of "ready" indicator when the file is ready. If the file is not ready, then the AJAX success handler will start another timeout. When the server says it's ready, your Javascript handler will make the button visible and no further polling will be necessary.
Check this example here http://forum.springsource.org/showthread.php?t=70489 and let know if it works
You could use some type of messaging on the server that tells the client when the file is ready e.g. we us a table for all report requests and the server writes the status into the table and the client is then asking for the status of the report job with an AJAX call every few seconds.

Categories

Resources