سؤال

I succeeded to make simple and small example which integrates between Spring MVC and Atmosphere framework. It works very well, but it behaves the way I'm failing to understand:

In my try example i'm trying to broadcust Date string every 10 seconds, so If I start my app at 10:10:20 the next message will be 10:10:30, etc....

but instead of it I have 4 callbacks within this period of time so my message flow looks like this: 10:10:20, 10:10:21, 10:10:22, 10:24, 10:10:30. This behavior occurs sequentially with the same perion of time:

Why do I have 4 callbacks in 10 seconds instead of single one every 10 seconds?

My relevant code is:

 @RequestMapping(value="/websockets", method=RequestMethod.GET)
 @ResponseBody
 public void websockets(final AtmosphereResource<HttpServletRequest,HttpServletResponse> event) {

      final HttpServletRequest  req = event.getRequest();
        final HttpServletResponse res = event.getResponse();
        event.suspend();

        final Broadcaster bc = event.getBroadcaster();
        bc.scheduleFixedBroadcast(new Callable<String>() {

            public String call() throws Exception {

                return (new Date()).toString();
            }
        }, 10, TimeUnit.SECONDS);
 }

and on my client code I'm doing the following:

<script type="text/javascript">
$(document).ready(function() {
     var callbackAdded = false;

          function callback(response)
          {

              $.atmosphere.log('info', ["response.state: " + response.state]);
              $.atmosphere.log('info', ["response.transport: " + response.transport]);

              if (response.transport != 'polling' && response.state != 'connected' && response.state != 'closed') {
                  $.atmosphere.log('info', ["response.responseBody: " + response.responseBody]);
                  if (response.status == 200) {
                      var data = response.responseBody;

                      if (data) {
                          $("#date").text(data);
                      }
                  }
              }
          }
          $.atmosphere.subscribe("websockets",
                  !callbackAdded? callback : null,
          $.atmosphere.request = {transport: 'websocket'});
          connectedEndpoint = $.atmosphere.response;
          callbackAdded = true;
});
</script>
هل كانت مفيدة؟

المحلول

After adding the Non-Blocking support I get the following message from the web application:

60347 [http-nio-8080-exec-1] WARN org.atmosphere.cpr.AtmosphereServlet - failed
using comet support: org.atmosphere.container.TomcatCometSupport, error: Tomcat
failed to detect this is a Comet application because context.xml is missing or t
he Http11NioProtocol Connector is not enabled.
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib
/atmosphere-compat-tomcat.jar
60348 [http-nio-8080-exec-1] WARN org.atmosphere.cpr.AtmosphereServlet - Using B
lockingIOCometSupport.
60401 [http-nio-8080-exec-1] INFO com.hillert.atmosphere.HomeController - Welcom
e home!

And after this message I get the timeout errors like before. You can see from the log now the Server is using http-nio-8080. What should I do now ?

نصائح أخرى

I know whats the problem with this your code. Every request with start a broadcast Thread what every 10 seconds will brodcast your date. Every time you send a new request (start a new browser) a new Thread will start. Unfortunately seems the Threads will never stop. I dont know how the Atmosphere handles such cases. Who is responsable to stop the brodcast threads. I will read more about this. You can test easy what I said, restart web-server, just start one browser and you should see only one call every 10 seconds. In my case it works like this. I start second third browser then I get brodcast as many times as browser clients. After I stop close the browser clients the web server never stops the brodcast threads.

Second case was when really instead of 10 seconds wait time I had only 1 second. This I could only hard reproduce with Tomcat so I was trying another web-server: JBOSS 7.0. With JBOSS was always reproducable. The first problems remain if you starts a second browser request then you get 2 calls pro second :).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top