Online application calling Web service - java

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.

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.

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.

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.

server restart notify to clients

I am working on web based applications.
Server : Tomcat
Technology : Simple Jsp/Servlet/JQuery
I need to restart my server as an when new updates are there. And these changes are frequent almost every 1 or 2 day. I think to create some mechanism like I can say to every logged in user to save their changes and server will start in few minutes. (Timer will be there). Popup should be open though user is ideal.
Is there any direct way to do this so? Or I need to implement ajax call on every few seconds to server on every jsp page to check if any message is there on server???
Any idea will be appreciated.
Thanks in advance.
For the approach you are taking, I would suggest you to use Async Serlvets(Req. min Servlet API 3.0) or Apache Tomcat's comet technology(Kind of Async Servlet).
You will make ajax call on every page when it(page) loads(ajax onload() for eg.) to Async Servlet and will idle until response from server comes. This Async servlet should send Server Restart notification to all connected clients- whenever you trigger notification manually. Once ajax client gets notification, it will display the Warning(or user friendly message).
This will remove the need to make unnecessary polling to server after fixed internal - A big plus resource wise.
Personally I wont suggest this way, better get agreed on specific timeframe for deployment everyday(every two days) with clients and perform deployments in this time.
If you are from India- You must be aware about IRCTC website- Which is not available for train reservation every night for 1 hour.

Categories

Resources