GWT中的同步RPC调用

时间:2011-09-08 19:18:57

标签: java gwt jsni

(仅此标题应该会让人们从木制品中走出来用棍棒打击我,但是请听我说。)

我有一个用例,我需要从异步调用中返回一个值。 (我正在使用GWT-Platform,但概念是相同的。)我声明了最终的JavaScriptObject数组,然后在AsyncCallback中分配了值。但是,我需要返回值,并且该方法在AsyncCallback完成之前返回。因此,我需要以某种方式阻止,直到AsyncCallback完成。我需要在另一个方法中返回值,或者我只是在onSuccess()中执行我需要的操作。

我尝试过循环,定时器和其他一些没有运气的方法。有人可以帮忙吗?

@Override
public JavaScriptObject doGetWhereAmIMarker(final double lng, final double lat) {

    final JavaScriptObject[] markerArray = new JavaScriptObject[1];  // ugly hack, I know
    dispatch.execute(new GetLocationDescriptionsAction(lng, lat), new AsyncCallback<GetLocationDescriptionsResult>() {
        @Override
        public void onFailure(Throwable caught) {
            caught.printStackTrace();
        }

        @Override
        public void onSuccess(GetLocationDescriptionsResult result) {
            Map<String, Location> resultMap = result.getResult();
            StringBuffer message = new StringBuffer();
            for (String key : resultMap.keySet()) {
                message.append(key).append(": ").append(resultMap.get(key)).append("\n");
            }

            Map tempMap = new HashMap();
            tempMap.put("TITLE","Location Information");
            tempMap.put("LAT", lat);
            tempMap.put("LNG", lng);
            tempMap.put("CONTENTS", message.toString());

            JavaScriptObject marker = GoogleMapUtil.createMarker(tempMap);
            markerArray[0] = marker;
            if (markerArray[0] != null) {
                GWT.log("Marker Array Updated");
            }

        }
    });

    return markerArray[0];
}

更新:根据要求,这是调用doGetWhereIAmMarker()的代码。我尝试使用Google Map对象(作为JavaScriptObject)作为参数使用单独的本机方法,但似乎在本机方法之间传递该对象会导致更新所述对象的能力。

public native void initMap(JavaScriptObject mapOptions, JavaScriptObject bounds, JavaScriptObject border, JsArray markerArray, Element e) /*-{

    // create the map and fit it within the given bounds
    map = new $wnd.google.maps.Map(e, mapOptions);
    if (bounds != null) {
        map.fitBounds(bounds);
    }

    // set the polygon for the borders
    if (border != null) {
        border.setMap(map);
    }

    // set up the info windows
    if (markerArray != null && markerArray.length > 0) {
        var infoWindow = new $wnd.google.maps.InfoWindow({
            content:"InfoWindow Content Goes Here"
        });

        for (var i = 0; i < markerArray.length; i++) {
            var marker = markerArray[i];
            marker.setMap(map);
            $wnd.google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(marker.content);
                infoWindow.open(map, this);
            });
        }
    }

    // need to reference the calling class inside the function(), so set a reference to "this"
    var that = this;

   $wnd.whereAmI=function(lng, lat) {
        that.@org.jason.mapmaker.client.view.MapmakerMapViewImpl::whereAmI(DD)(lng,lat);
   }

    $wnd.google.maps.event.addListener(map, 'click', function(event) {
        var lat = event.latLng.lat();
        var lng = event.latLng.lng();
        $wnd.whereAmI(lng, lat);
    });

}-*/;

2 个答案:

答案 0 :(得分:3)

在某些时候我不得不做类似的事情,但最终我删除了代码,转而支持异步。因此,我无法提供您需要使用的确切代码,但只提供了一些关于如何处理它的指示。

  • 首先,this blog描述了如何使用javascript进行同步AJAX。
  • 其次,您必须为同步通话提供支持。问题是GWT不支持提供同步AJAX调用的参数。最有可能的是他们不想鼓励使用它。因此,您需要使用JSNI将适当的方法添加到XMLHttpRequest(您可能会扩展),然后添加到RequestBuilder(也应该扩展它)。
  • 最后,使用扩展RequestBuilder修改您的服务。像
  • 这样的东西
  

((ServiceDefTarget)服务).setRpcRequestBuilder(requestBuilder);

最后 - 来自同一篇博客文章(稍微脱离背景):

  

由于请求丢失和挂起浏览器的危险,       除了以外的任何内容,不建议使用同步javascript       (onbefore)卸载事件处理程序。

答案 1 :(得分:0)

我认为这一切都是命运....... 我们不能在Gwt中捕获响应并发送它,因为在发送请求后立即开始执行下一个方法,既不打扰响应 然而,他们仍然满足我们使用计时器,我认为......

 Timer t = new Timer() {
      @Override
      public void run() {
        Window.alert("Nifty, eh?");
      }
    };
    t.schedule(5000);