4 Replies Latest reply on Oct 13, 2011 5:23 PM by stab

    Question on broadcasting messages

    stab

      Hi,

       

      To test errai I used the quickstart guide with archetype:

       

      mvn archetype:generate \

      -DarchetypeGroupId=org.jboss.errai.archetypes \

      -DarchetypeArtifactId=bus-quickstart \

      -DarchetypeVersion=1.3.0.CR1 \

      -DarchetypeRepository=https://repository.jboss.org/nexus/content/groups/public/

       

      and it worked nicely

       

      My newbie question:

      What MessageBus am I supposed to specify in the servercode if I want

      to switch from conversation to broadcasting.

       

      In conversation the bus is, to my understanding, implicit present as you only specify reply()

           MessageBuilder.createConversation(message)

            .subjectProvided()

            .withValue("Hello, World!")

            .done().reply();

       

      When broadcasting you are oblighed to specify a MessageBus

          MessageBuilder.createMessage()

             .toSubject("HelloWorldService")

             .signalling()

             .noErrorHandling()

             .sendNowWith(viaThis);

       

      I tested to modify the example from the users guide as:

      @Service

      public class HelloWorldService implements MessageCallback {

       

        @Inject

        private MessageBus bus;

       

                @Override

                public void callback(Message message) {

              // Send a message to the 'HelloWorldClient'.

              MessageBuilder.createMessage()

                  .toSubject("HelloWorldService")             

                  .signalling()                              

                  .with("text", "Hi There")                  

                  .noErrorHandling()                         

                  .sendNowWith(bus);                  

                }

      }

       

      but when running the application I get a stackOverflowError at sendNowWith(bus)

       

      How do I specify the bus?

       

      regards

      stab

        • 1. Re: Question on broadcasting messages
          csa

          Yes, injecting the bus is fine. The problem is your subject. You are sending a message to "HelloWorldService" from within the HelloWorldService callback. You need to use a separate subject for the broadcast messages.

          • 2. Re: Question on broadcasting messages
            stab

            Thanks, I see now that I've misunderstood how to subscribe to a service(read the manual would a colleague had said to me).

             

            I modified the client code and added a subscription to "HelloWorldClient":

            @EntryPoint

            public class HelloWorldClient extends VerticalPanel {

              private int n = 0;

             

              @Inject

              private MessageBus bus;

             

             

              @PostConstruct

              public void init() {

                Button button = new Button("hello!");

             

             

                bus.subscribe("HelloWorldClient", new MessageCallback() {

                                @Override

                                public void callback(Message message) {

                                          n++;

                        Window.alert("From Server(messagenr:" + n + ") - " + message.get(String.class, MessageParts.Value));

                                }

                });

             

                button.addClickHandler(new ClickHandler() {

                  public void onClick(ClickEvent event) {

                    MessageBuilder.createMessage()

                        .toSubject("HelloWorldService")

                        .withValue("Hello, There!")

                        .done()

                        .sendNowWith(bus);

                  }

                });

             

             

                add(button);

                RootPanel.get().add(this);

              }

            }

             

            and the server code to:

            @Service

            public class HelloWorldService implements MessageCallback {

             

             

              @Inject

              private MessageBus bus;

             

             

                      @Override

                      public void callback(Message message) {

                    // Send a message to the 'HelloWorldClient'.

                    MessageBuilder.createMessage()

                        .toSubject("HelloWorldClient")             

                        .signalling()                              

                        .with("text", "Hi There")                  

                        .noErrorHandling()                         

                        .sendNowWith(bus);                  

                      }

            }

             

            Now I get a response in the client but no messsage, how come?

             

            broadcast.gif

            • 3. Re: Question on broadcasting messages
              csa

              That's because you set the message part "text" but ask for the value. In your client callback use message.get(String.class, "text").

              • 4. Re: Question on broadcasting messages
                stab

                Thanks a lot, working very nice!!!!