I have a "user" class which contains a status attribute, I want to display this attribute in a dashboard.
when the backend changes this attribute I want the frondEnd to display the new value directly without reloading the page .
I use Spring and React
You can use a pub-sub mechanism to achieve this. for example, a kafka topic can be used to sync your changes with the front end.
while inserting a record in your backend DB, push a message to user-kafka-topic.
add a user-kafka-consumer in your front-end application. It would consume messages from the user-kafka-topic at runtime.
you can plan to add you own front-end DB and update the front-end DB.
use observer pattern in your frontend to to update your UI as soon as your json data for the page changes.
Related
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.
I need the front end to display the most recent record in my db (mysql if it makes a difference). Is there a way for my java app to send that value in the thymeleaf model to all existing sessions?
The only way I can think of is to send an ajax request every second to an endpoint that will respond with whether it needs to update or not but this seems a bit hacky
Basically I am hosting a database on server side using spring boot. And I wanted to write custom query from client side which is developed in angular and call a function from server side for giving me the needed results.
The function for server side will look like this:
List<rows> func(String customQuery){
//fetch rows from table using this custom query and return those rows
//which I can use in client side.
}
Below are examples of customQuery which I need to send from client side:
select * from table;
select * from table where id>10;
select * from table where id>20 and id<30;
So far I searched in Internet I could not find any solution. Please help.
Sending SQL directly from the client would be a complete security failure, because a malicious user could easily figure out how it works and send a delete from table to kill your entire app.
Much worse, they might even be able to run create user ... to give themselves complete access to your entire database, get sensitive information, install malware, etc.
Instead you'll want to create a REST service in your application with methods such as
GET /table
GET /table?minId=10
GET /table?minId=20&maxId=30
Return as application/json or a similar data format and only return the information your angular app really needs.
Angular would then be responsible for selectively updating the display with your data.
Edit:
This is a guide I have found for creating a basic web app based on Spring Boot and Angular. Might be a good starting point for you:
https://www.baeldung.com/spring-boot-angular-web
The best way is to send only the parameters (minID and maxID in your case) from the client-side and then build the query dynamically on server-side using Spring JPA Specifications.
So I am doing a Java web project using Spring as ORM and eclipse link.
The problem I am having is that after I insert, delete or update data in my database through my web application, the data is refreshed in the mySQL database, but when I go back to my view page the new data isn't there so I have to restart my whole program for the new data to be displayed.
How can I refresh my web application to display the newly inserted, updated, or deleted data without having to restart my whole program?
You aren't saying what front end tech you are using. Angular, React, jQuery?
With a library like Angular you just bind an array representing the data to the view. When you update the array the view auto updates.
You would typically update the array on a success response from the server. So, for example, on a 201 (created), in the promise callback (the .then block) you would update the array with the newly added data. Updating the bound array will update the view.
With libraries like jQuery there is no binding to the view. Instead, you would update the array and then update the HTML dynamically via JavaScript. This would typically be done in an AJAX request callback. See the jQuery.ajax method.
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.