Is it possible to refresh all clients with a new thymeleaf model? - java

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

Related

how can i keep backend Data synced with my fronted

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.

auto reload content of a postgreSQL table

I am using this code:
my code
to output my table. My question is how is possible to updating it in the page without reloading?
Thank you
Either use AJAX requests or if you feel good, try HTML5 websockets : add a trigger to the table UPDATE / INSERT / DELETE, and keep your clients up-to-date.
AJAX means you'll be doing a request from client to server every few seconds, to check if anything have changed and if so, you'll download the new data (usually devs use JSON), and update your table using javascript with this data.
With websockets, the event starts server side : whenever the db table is updated, you send a request from the server to the clients to tell them to update the table with the new data.
You'll find plenty of examples on the web, searching either for AJAX or websockets.
If you don't know what they are, I'd recommend you to go for AJAX, as setting-up a websockets server with PHP might be tricky.

Best practice to update web application on client side (GWT)

Now I have a problem:
user opens web app page, gets javaScript (ModuleName.nocache.js);
Then I update client side (roc requests, view etc).
user didn't close web app tab in browser, didn't update page. He clicks somewhere and gets random explosion. For example, RPC doesn't work, servlets moves anywhere, there is many errors or not.
Now I want to implement scenario:
User must have cookie attribute with web app version.
By request I see it and in response force him to update page (don't know how).
If user's request can't be delivered gwt force him to update page (don't know how).
But I think there must be a best-practice-way to solve this problem.
Catch IncompatibleRemoteServiceExceptions and StatusCodeExceptions in your AsynCallbacks. The first one tells you the client-side code is not compatible with the server-side code; the second can tell you that there no longer is a RPC servlet there (look for a 404 status code).
You can then show a message to the user prompting him to reload the page (this is what Google Groups does for example).
That said, there are some ways to mitigate this if the changes are relatively small: you can keep the old serialization policy files around server-side so the server can process requests from different client versions. The changes have to be somehow backwards compatible though.
You could then detect the client version on the server-side (either using a list of the latest serialization policy files and checking whether the client is using one of them or an older one; or using a request header or cookie) and include something in the response (response header or cookie) telling it there's a new version.
Or you could regularly poll the server (obviously not using RPC though) for the latest version of the app.
Another approach:
If it is empty (if not, go to next step), save the com.google.gwt.core.client.GWT.getPermutationStrongName(); in your LocalStorage or in your Cookies and finish the flow.
When your app loads, get the permutationStrongName again, and check if the saved one is different.
If it is different force to request to the server everything (if you want/need "break" async process you can use GQuery - promises). Then, replace in your LocalStorage or Cookies the new gwt's permutation id.
You can do this always that your app is loaded (is not a big deal). And you can now when the version on your server has changed.

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.

Handling Post Requests

yesterday i started brainstorm for a project of mine and I'm not sure if its the correct approach.
On my website I'm having an (kind of an order form) which sends a post to a target URL, which works with a simple curl php script. The target is an external service (where I have no access no rights, nothing). I only know that I will get a POST with further processed data back from the service, which I have to save into my DB.
in steps:
Users fills out the (order) form and posts data to an external url on my website.
data gets externally handled and after finishing that resents a post.
read incoming post data.
save data into DB.
Success page on my website.
My thoughts were to handle the incoming data with a servlet (spring maven project) but I'm not sure if this is a correct approach. Is there a better why to do this. Or is the first step with the php scripts wrong. thx for any help.
A simplest workflow could be
1. Forward the initial (Order form with values) request to a servlet
2. Invoke a post request using java to an external url inside this servlet (Using Apache http client, or libraries such as HTMLUnit)
3. Once you get the incoming response in your servlet, you can update your database.
If you are using spring, the controller could forward initial request to a business class which will handle this post processing and delegate the database update to respective DAO.
There are a number of suitable ways to handle this, and the decision is largely a matter of preference and what you're familiar with. Spring can handle this sort of job quite well.
Note: Maven is a build system for Java and some other JVM languages. I recommend using it, but it's not part of Spring; what you're probably looking for is Spring MVC.

Categories

Resources