2 Replies Latest reply on Apr 24, 2015 8:55 AM by John Johny

    Sending messages in GWT by Errai

    John Johny Newbie

      Hi, http://docs.jboss.org/errai/latest/errai/reference/html_single/#sid-5931280_MessagingAPIBasics-SendingMessageswiththeClientBus here is how to send a message from client.

       

      I have a client HelloServer.java, when i click on the button I want to send message to a server.

       

      package gwt.user.client;
      
      import org.jboss.errai.bus.client.ErraiBus;
      import org.jboss.errai.bus.client.api.base.CommandMessage;
      import org.jboss.errai.bus.client.api.base.MessageBuilder;
      import org.jboss.errai.bus.client.api.messaging.Message;
      import org.jboss.errai.bus.client.api.messaging.MessageBus;
      import org.jboss.errai.bus.client.api.messaging.MessageCallback;
      import org.jboss.errai.bus.client.api.messaging.RequestDispatcher;
      
      
      import com.google.gwt.core.client.EntryPoint;
      import com.google.gwt.core.client.GWT;
      import com.google.gwt.event.dom.client.ClickEvent;
      import com.google.gwt.event.dom.client.ClickHandler;
      import com.google.gwt.user.client.rpc.ServiceDefTarget;
      import com.google.gwt.user.client.ui.Button;
      import com.google.gwt.user.client.ui.DialogBox;
      import com.google.gwt.user.client.ui.RootPanel;
      import com.google.gwt.user.client.ui.VerticalPanel;
      
      
      public class HelloServer implements EntryPoint{
      
      
          private MyTable table;
          private MessageBus bus = ErraiBus.get();
          private RequestDispatcher dispatcher = ErraiBus.getDispatcher();
          UserService usrSer;
      
      
          private RequestDispatcher getDispatcher(){
              return this.dispatcher;
          }
      
            public void onModuleLoad() {
      
              table = new MyTable(null);
      
              Button button = new Button("Click me");
      
              // We can add style names
              button.addStyleName("pc-template-btn");
              // or we can set an id on a specific element for styling
      
      
              VerticalPanel vPanel = new VerticalPanel();
              vPanel.setWidth("100%");
              vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
              vPanel.add(button);
      
      
              vPanel.add(table);
      
      
              // add table and button to the RootPanel
              RootPanel.get().add(vPanel);
      
      
              // create the dialog box
              final DialogBox dialogBox = new DialogBox();
              dialogBox.setText("Welcome to GWT Server Communication!");
              dialogBox.setAnimationEnabled(true);
              Button closeButton = new Button("close");
              VerticalPanel dialogVPanel = new VerticalPanel();
              dialogVPanel.setWidth("100%");
              dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
              dialogVPanel.add(closeButton);
      
              closeButton.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                  dialogBox.hide();
                }
              });
      
      
              // Set the contents of the Widget
              dialogBox.setWidget(dialogVPanel);
      
      
              button.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                  UserServiceAsync service = (UserServiceAsync) GWT.create(UserService.class);
                  ServiceDefTarget serviceDef = (ServiceDefTarget) service;
                  serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL() + "userService");
                  UserCallback myUserCallback = new UserCallback(table);
      
      
                  MessageBuilder.createMessage()
                    .toSubject("UserServiceImpl") // (1)
                    .signalling() // (2)
                    .noErrorHandling() // (3)
                    .sendNowWith(getDispatcher()); // (4)
      
      
                  service.getUserList(myUserCallback);
                }
              });
      
            }
      
      }
      

       

      UserServiceImpl.java is the server where I want to receive the message in callback method.

       

      package gwt.user.server;
      
      import gwt.user.client.User;
      import gwt.user.client.UserService;
      
      import java.util.ArrayList;
      import java.util.List;
      
      
      import org.jboss.errai.bus.client.api.base.CommandMessage;
      import org.jboss.errai.bus.client.api.messaging.Message;
      import org.jboss.errai.bus.client.api.messaging.MessageCallback;
      import org.jboss.errai.bus.server.annotations.Service;
      
      
      import com.google.gwt.user.server.rpc.RemoteServiceServlet;
      
      
      @Service
      public class UserServiceImpl extends RemoteServiceServlet implements UserService, MessageCallback {
      
          private static final long serialVersionUID = 1L;
          private List<User> userList = new ArrayList<User>();
          public UserServiceImpl() {
      
      
              User user = new User();
              user.setId("1");
              user.setUsername("Peter");
              user.setNumberOfHits("15");
              userList.add(user);
      
      
              user = new User();
              user.setId("2");
              user.setUsername("Hanz");
              user.setNumberOfHits("25");
              userList.add(user);
      
      
          }
      
      
          public User getUser(String id) {
      
              for (Object object : userList) {
                if (((User) object).getId().equals(id))
                  return ((User) object);
              }
              return null;
      
          }
      
      
          public List<User> getUserList() {
              return userList;
          }
      
      
          @Override
          public void callback(Message message) {
              System.out.print("Message received");
          }
      
      
          public void callback(CommandMessage message) {
              System.out.print("Message received");
            }
      } 
      

       

      When I click the button I get no subscribers to deliver error message:

      org.jboss.errai.bus.client.api.base.NoSubscribersToDeliverTo: no subscribers to deliver to for subject: UserServiceImpl

        org.jboss.errai.bus.client.framework.ClientMessageBusImpl.send(ClientMessageBusImpl.java:812)

        org.jboss.errai.bus.client.ErraiBus$3.dispatch(ErraiBus.java:171)

        org.jboss.errai.bus.client.api.base.CommandMessage.sendNowWith(CommandMessage.java:349)

        org.jboss.errai.bus.client.api.base.DefaultMessageBuilder$1.sendNowWith(DefaultMessageBuilder.java:95)

        gwt.user.client.HelloServer$2.onClick(HelloServer.java:84)

       

      If I annotate UserServiceImpl.java class with

      @Service("UserServiceImpl")
      

      it doesn't help and I get the same error.

       

      When I add

      bus.subscribe("UserServiceImpl", new UserServiceImpl());
      

      before

      MessageBuilder.createMessage()
      

      method in HelloServer.java

       

      I get error message:

      [ERROR] No source code is available for type gwt.user.server.UserServiceImpl; did you forget to inherit a required module?

       

      http://stackoverflow.com/questions/29827445/sending-messages-in-gwt-by-errai-framework