3 Replies Latest reply on Jan 4, 2012 4:30 PM by cbrock

    Broadcast problem

    krysp89

      In my application every serveral second send messago to every subscribed clients using broudcast. I would like to stop sending messages when there is no receivers. So Is there any method using errai api (or any other way) to check if there any subscription on specyfic subject.

        • 1. Re: Broadcast problem
          cbrock

          You should get a NoSubscribersToDeliverTo exception if there's nobody listening to a topic when you send the message.

          • 2. Re: Broadcast problem
            krysp89

            Thanks for replay but there is problem that i never get that exeption.

            This is example which i prepered

             

            @Service

            public class ServerTest implements MessageCallback {

             

              boolean ifrun=false;

                      private RequestDispatcher disaptcher;

             

                      @Inject

                      public ServerTest(RequestDispatcher disaptcher) {

                                this.disaptcher=disaptcher;

                      }

             

             

             

                      @Override

                      public void callback(Message message) {

                                String sessionId = ServerBusUtils.getSessionId(message);

                                 SessionContext injectionContext = SessionContext.get(message);

             

             

             

                                System.out.println("siema");

                                if(ifrun==false)

                                {

                                          System.out.println("dwa");

                                          ifrun=true;

                                          while(true){

                                                    try {

             

                                                    System.out.println("chce wyslac");

             

             

                                          MessageBuilder.createMessage()

                                    .toSubject("czekolada")

                                    .signalling()

                                    .with("text", "We're relaying a message!")

                                    .noErrorHandling().sendNowWith(disaptcher);

             

                                                    Thread.sleep(1000);

             

                                                    }catch(NoSubscribersToDeliverTo e1)

                                                    {

                                                              System.out.println("no subscibers");

                                                    }

                                           catch (InterruptedException e) {

                                                    // TODO Auto-generated catch block

                                                    //e.printStackTrace();

                                                    e.printStackTrace();

                                          }

             

                                          }

             

                                }

                      }

             

            }

             

             

             

             

            public class AppTest implements EntryPoint {

             

             

             

                      private MessageBus bus = ErraiBus.get();

             

             

             

                      Label wartos=new Label("do test");

             

                      @Override

                      public void onModuleLoad() {

                                RootPanel.get().add(new Label("testdisplay"));

                                RootPanel.get().add(wartos);

                                /*bus.subscribe("czekolada", new MessageCallback() {

             

                                          @Override

                                          public void callback(Message message) {

             

                                                    String messageText = message.get(String.class, "text");

                                                    wartos.setText(wartos.getText()+messageText);

                                          }

                                     });*/

                                Button b=new Button();

                                b.addClickHandler(new ClickHandler() {

                                          @Override

                                          public void onClick(ClickEvent arg0) {

                                              MessageBuilder.createMessage()

                                        .toSubject("ServerTest")

                                        .withValue("Hello, There!")

                                        .done()

                                        .sendNowWith(bus);

                                              wartos.setText("start");

             

                                          }

                                });

             

                                Button stop=new Button("stop");

                                stop.addClickHandler(new ClickHandler() {

             

                                          @Override

                                          public void onClick(ClickEvent arg0) {

                                                    bus.unsubscribeAll("czekolada");

                                          }

                                });

             

                                RootPanel.get().add(b);

                                RootPanel.get().add(stop);

                      }

             

            }

             

             

             

            On my conputer even when i click stop buton server still sending messages.

            • 3. Re: Broadcast problem
              cbrock

              One of the problems may be that the server doesn't know the client has disconnected for some time. Due to the long polling model, it takes ErraiBus some time to detect a disconnection. By default it's 45 seconds.