Per user level synchronization in Spring MVC - java

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.

Related

Is Spring Rest Controllers Singleton?

I have a sample Spring Rest application.
I have several clients accessing an API in the Spring Rest application. The API checks whether a job has started or not against the MSSQL DB that we use.
If the Job status in DB is in started status, it will pick the record and update its status to inprogress and return the details of the Job as the response to the API and based on that the client will do some processing.
We have observed that, more than one client is picking up the same Job which is in started status and updates it to inprogress and pass the response to the client. So, it ends up like the same job is being processed by multiple clients.
We tried to resolve this by adding a synchronized block and enclosed the DB call that picks the record in started state and update it to inprogress. The DB call resides in the service layer. But still the duplicate issue is there.
If the controller and the subsequent layers like service and DAO layers are singleton then, when multiple API calls hit the web app, the synchronized block of code should be executed by one request at a time. But that is not what we see practically.
Could someone please help to resolve this issue?
Actually Rest Controllers are Thread Safe it means it is capable of handling each HTTP requests unique. Also when you try to access the Rest Controller it creates a separate session id in browser you can also check that using developer console. Here you are trying to access same DB and change the status as inprogress, so there is a chance for the next api to fetch details of the previous session.. so try to modify the Rest API without modifying status in DB every time or you can do something like, if the DB is accessed by one API then the other API should not be able to access DB until the first connection is closed. Hope this can work for you.

Execute and Wait for Rest Webservice

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?

Online application calling Web service

In an application where there are say 1000 simultaneous users, would it be advisable to call a web service as a part of the request or fetch data from web service offline using a job and store it in your database?
I am confused between these two options:
1) Call the web service as a part of your request: user clicks link, page submits to controller, web service called (substantial data is fetched). Data is displayed to user.
2) Create a timer bean/ batch job that calls the service every 15 minutes and fetches data updates for all users, update the database. User may not see latest updates(updates will be 15 min old) but this is acceptable.
Wouldn't way 2 allow for better performance always? Is there a situation in which calling online service may be more advisable?
Finally, if both the service code and client code are java, would you rather use JNI-RMI instead of web service ?
Both 1 and 2 should be combined in final approach with Cache as solution to fetch/update data
1) Offline job updates the Cache in Web Server by fetching data from database
2) When user call Web service, data will be rendered from Cache instead of database.

Spring MVC Rest URL and URL redirection

Am developing an app using SpringMVC. In that app, I have a list of crud screens(almost 20 screens).
Now, I designed my controller in the following pattern of request mapping
create
show
update
delete
Here , the problem is, I would like to expose this URL as both REST Service as Well as Normal Spring controller(directs to a new page after CRUD operations).
ie. When I use the application, it should do the CRUD operation and redirect to specific pages(Accordingly)
When I call as a rest service (using REST Clients). I should get the JSON data
Is it possible??
I would cleanly separate your AJAX/JSON calls from your page navigation. In other words, assign responsibility for the page navigation to one controller (or leverage an SPA routing mechanism on the client side), and the data access to another "service" controller. You then have a reusable and testable service and an independent navigation flow (which could evolve, change technologies etc).
As far as know, I don't think so. But one way will be like, each time your controller will produce JSON response. But for web application you need to add extra call for each request which will load desired page and then call your CRUD methods on load of your page and parse the JSON response to fill the data.
I think what you are looking for is content negotiation. Google recommends this article: http://blog.springsource.org/2013/05/11/content-negotiation-using-spring-mvc/

JAX-RS / Servlet Programmatic Login

I'm creating a restful service with jax-rs which will be used via ajax calls. i need to use programmatic security as i need fine grained control. i've created a login method (that calls request.login) but when subsequent methods are called the security context method getUserPrincipal is always null.
because i'm using programmatic login do i need to do something else for getUserPrincipal to work in subsequent calls? do i have to set something in the session or have a custom interceptor on calls to check or should it be managed by the container?
i'm using glassfish 4
If you are aiming to have user sessions, need to keep track of logins using sessions or your own cookie.
If you are aiming to have a RESTful service, you should authenticate every request because REST is designed to be stateless.

Categories

Resources