通过 Errai 框架在 GWT 中发送消息

Sending messages in GWT by Errai framework

here 你可以看到如何从客户端发送消息。

我有一个客户端 HelloServer.java,当我点击按钮时我想向服务器发送消息。

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是我要在回调方法中接收消息的服务器。

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");
      }
} 

当我点击按钮时,没有订阅者发送错误消息:

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.dispatch(ErraiBus.java:171)
  org.jboss.errai.bus.client.api.base.CommandMessage.sendNowWith(CommandMessage.java:349)
  org.jboss.errai.bus.client.api.base.DefaultMessageBuilder.sendNowWith(DefaultMessageBuilder.java:95)
  gwt.user.client.HelloServer.onClick(HelloServer.java:84)

如果我用 @Service("UserServiceImpl") 注释 UserServiceImpl.java class,它没有帮助,我得到同样的错误。

当我添加

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

MessageBuilder.createMessage() 之前 HelloServer.java

我收到错误消息

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

有人知道如何在 GWT 应用程序中使用客户端和服务器之间的消息传递或给我一个基本示例吗?

答案就在这个thread中。我忘了添加 ErraiApp.properties 文件。