最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - how to push the data to the jsp with out requesting it for every 2 seconds? - Stack Overflow

programmeradmin2浏览0评论

I want to push the data to the jsp for every 2 seconds, with out client requesting it.
I am using Spring with Hibernate here.
I am displaying google maps marker, and I want to update the marker location for every 2 seconds by getting the data from database, however I have done getting the data from database for every 2 seconds, but I am unable to push that data to this jsp.

   @Scheduled(fixedRate = 2000)
   public void getData(){
                    // TODO Auto-generated method stub
                    DeviceDetails deviceDetails = realTimeDataDAO.getDeviceDetails(deviceId);
                    System.out.println(deviceDetails);
                }

I have to display some data after every 2 seconds. Can anyone tell me how to do that?

any one knows about Comet Ajax Push technology, will it work in this scenario?

I want to push the data to the jsp for every 2 seconds, with out client requesting it.
I am using Spring with Hibernate here.
I am displaying google maps marker, and I want to update the marker location for every 2 seconds by getting the data from database, however I have done getting the data from database for every 2 seconds, but I am unable to push that data to this jsp.

   @Scheduled(fixedRate = 2000)
   public void getData(){
                    // TODO Auto-generated method stub
                    DeviceDetails deviceDetails = realTimeDataDAO.getDeviceDetails(deviceId);
                    System.out.println(deviceDetails);
                }

I have to display some data after every 2 seconds. Can anyone tell me how to do that?

any one knows about Comet Ajax Push technology, will it work in this scenario?

Share Improve this question edited Jan 9, 2012 at 10:11 gnat 6,223115 gold badges55 silver badges75 bronze badges asked Dec 25, 2011 at 16:52 Ramesh KothaRamesh Kotha 8,32217 gold badges67 silver badges90 bronze badges 1
  • Just answered - it would be helpful if you said what app server you are using - or if you are flexible on which to choose. – Pablojim Commented Jan 9, 2012 at 19:51
Add a comment  | 

10 Answers 10

Reset to default 5

You have a number of choices.

Polling - as mentioned in other answers you could simply have javascript in the client constantly poll the server every 2 seconds. This is a very common approach, is simple and will work in the large majority browsers. While not as scaleable as some other approaches setup correctly this should still be able to easily scale to moderate volumes (probably more users than you'll have!).

Long polling - Also known as Comet this is essentially a long lived request. The implementation of this will vary depending on your app server. see here for Tomcat: http://wiki.apache.org/tomcat/WhatIsComet or Jetty bundles some examples.

HTML 5 solutions while the web is traditionally request response based - event based processing is part of the HTML 5 spec. As you events seem to be only one way (server -> client) Consider using Event sources. See: http://www.html5rocks.com/en/tutorials/eventsource/basics/ or again the Jetty examples. Caveats here are that only modern browsers and some app servers support these methods - e.g. Apache doesn't natively support websockets.

So to sum up - my gut feeling is that your needs and for simplicity a polling approach is fine - don't worry too much initially about performance issues.

If you want to be on the cutting edge, learn new thing and you have control over your app server and frameworks then I'd go for the HTML 5 approach.

Comet is kind of a half way house between these two.

Your best bet with Spring is to store the results of the scheduled query into a bean in memory, then have another request-scope bean get that stored result in a method that is web accessible, and return it as text (or JSON). Alternatively you could query the DB everytime an update is requested.

Then, you can make a timed async request from your page (You may want to use YUI Connection Manager for that), read the response and use the panTo method from google.maps.Map to update your map location.

As you can see, the solution is split in a Java and a JavaScript portion.

For the Java side, you must create a controller that performs the query to the database (or better yet, delegates that task to another layer) and returns the results as JSON, you can use http://spring-json.sourceforge.net/ for that. It's a bit complex in Spring so you might want to instead create a simple servlet that returns the data.

For the Javascript side, once you have a working endpoint that returns the JSON data, using YUI Connection Manager and the google maps api:

function update(){
    var callback = {
        success: function (o) {
            var response = YAHOO.lang.JSON.parse(o.responseText);
            map.panTo({lat: response.lat, lng: response.longi}); // map is the google.maps.Map representing your map
        },
        failure: function (o) {

        }
    }
    var sUrl = '/getData.htm'; // This is the request mapping for your bean
    YAHOO.util.Connect.asyncRequest('GET', sUrl,callback);
}

function init(){
    setTimeout("update()", 2000);
}

The best way to do it is to have the client send an new request every 2 second, and then display the new data.

Since you use HTTP i assume you use javascript on the client side, so you need a timer in your javascript which fire every 2 second, and then let the javascript perform an ajax call to the server to get the data which it can then display.

Try a TimerTask or ThreadExecutor (look at the scheduled implementation).

Well, if you want to implement above solution in web application I am not sure but I think you cannot do it this way. HTTP is a request/response protocol and when the server finish sending one response it cannot initiate on its own sending a new response. In short words: one request from client - one response from server.

I think that you should use AJAX (asynchronous Javascript requests) so as to ask server every 2 second for a new data and if necessary update the DOM (website HTML tags structure).

I have had good experience with WebSockets. Very fast, low overhead bi-directional protocol between server and browser. Not sure what's your backend but Jetty supports it very well. Just have a timer process on the backend which would iterate over all active WebSockets sessions and push updates. There are plenty example on the net of how to use Websockets.

Things to keep in mind:

  • WebSockets not supported by all browsers (Chrome and Safari seems to be the best supported)
  • WebSockets traffic doesn't traverse all proxies

Depending on your requirements it might or might not be acceptable.

There are some projects like Atmosphere which tries to abstract browser/server differences in websockets support with graceful fallback to Comet. It might worth to look at.

//Initialize this somewhere
ScheduledExecutorService exe = Executors.newScheduledThreadPool(1);

exe.scheduleWithFixedDelay(new Runnable() {

    @Override
    public void run() {
        //The executor service tries to run 2 seconds after it last finished
        //If you code takes 1 second to run this will effectively run every 3 seconds 
    }
}, 0, //this is the initial delay
2, //this is the consecutive delay
TimeUnit.SECONDS);

exe.scheduleAtFixedRate(new Runnable() {

    @Override
    public void run() {
        //The executor service tries to run this every 2 seconds
        //If you code takes 1 second to run this will still run evey 2 seconds
    }
}, 0, //this is the initial delay
2, //this is the period it tries to run in
TimeUnit.SECONDS);

You need to send the data from server to client for every 2 secs. And already you know how to gather the data for every 2 seconds at the server side.

If this is all you need, the "Ajax streaming" will help you. This is on the client side. From server side for every 2 seconds you need to write the data and flush it.

Searching for this term will give you lot of examples. But remember all modern browsers will use one approach and all IE browsers will use IFrame approach to implement streaming.

In the first case, you need to make XHR request and peek the response and process it.

Here are a few examples: (I didt have time to go through them completely)

http://ajaxpatterns.org/HTTP_Streaming

http://developers.cogentrts.com:8080/DH_ajax_1.asp

U can use ajax call. As you can write code from Javascript that will send the request for every 2 seconds,but for this your server should be quick responsive for this type of request.

Well I guess this will help you.

If your server gets more than 1000 users then your application server will fail. I recommend you use NON Blocking Input Output methods supported using Jetty Server only to host the requests made for this purpose and use your normal EE Server for other applications.

发布评论

评论列表(0)

  1. 暂无评论