简体   繁体   中英

How to build real time application in java website

I want to build realtime application using java. The web application always request data to server every 10 seconds, and display the fresh data on the web page.

do you have idea how to solve case like this ?

Use ajax to query the server every 10 seconds. You can easily achieve this by using jquery in the frontend.

Your backend don't need anything special but a good idea is use web service which will return an XML or JSON that will be parsed by the front end jquery to update the display.

Ajax will make your JavaScript code poll your site server to get the last value of the currency, say every 10 seconds. Meaning if 100 persons are connected, you'll get on average 100 accesses in 10 seconds.

Unless you use a Flash object or a Java Applet to establish a TCP-IP connection with your server (that can push the new value when it is available), Ajax is a better/easier option for you. The TCP alternative provides faster results (clients see the new value more in real time than with Ajax polling), and, usually, more efficient in terms of performance (Only push when a new value is available).

If you implement an Ajax polling system, you'll have to add server side an abuse detector: many people understand well JavaScript and some of them may change the polling frequency to have newer values faster... (like every second). Depends on the audience, the number of people accessing your site etc...
That detector would ensure that a given client does not exceed the polling frequency (eg more than once every 12 seconds, if the frequency is 10, with an error margin)

If you are looking for a java framework then playframework or webfirmframework can be used. Spring + Angularjs is another way to do. Thay all are providing websocket communication which can be useful for realtime update. Handling everything with an ajar or our own implementation will take a bit time to become stable, because to handle hearbeat, intime opening & closing, reconnecting socket etc.. might be a bit difficult. If you google you can get a lot of tutorials for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM