2017-09-25 61 views
0

我想以同步的方式使用Vertx,爲什麼我試圖得到垂直同步和函數使用像awaitEvent,awatResult。 我跟着這個link來做到這一點。vertx:錯誤通過使用awaitResult函數

這裏是行我試圖運行:

long tid = awaitEvent(h -> vertx.setTimer(1000, h)); 
System.out.println("Timer has now fired"); 

不過,我得到了folloing錯誤:

sept. 25, 2017 11:25:41 PM io.vertx.ext.web.impl.RoutingContextImplBase 
GRAVE: Unexpected exception in route 
java.lang.StackOverflowError 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 
    at io.vertx.ext.web.impl.RoutingContextWrapper.request(RoutingContextWrapper.java:57) 

你知道我怎麼能解決這個問題?

回答

1

這個簡單的例子的工作原理:

import co.paralleluniverse.fibers.Suspendable; 
import io.vertx.core.Vertx; 
import io.vertx.ext.sync.Sync; 
import io.vertx.ext.sync.SyncVerticle; 

public class SyncExample extends SyncVerticle { 

    public static void main(String[] args) { 
     Vertx vertx = Vertx.vertx(); 

     vertx.deployVerticle(SyncExample.class.getName()); 
    } 

    @Suspendable 
    @Override 
    public void start() throws Exception { 
     System.out.println("Waiting for single event"); 
     long tid = Sync.awaitEvent(h -> vertx.setTimer(1000, h)); 
     System.out.println("Single event has fired with timerId=" + tid); 
    } 
} 

所得控制檯輸出是:

Waiting for single event 
Waiting for single event 
Single event has fired with timerId=0 

相關的依賴關係(表示爲行家座標)是:

<dependency> 
    <groupId>io.vertx</groupId> 
    <artifactId>vertx-sync</artifactId> 
    <version>3.4.1</version> 
</dependency> 
<dependency> 
    <groupId>co.paralleluniverse</groupId> 
    <artifactId>quasar-core</artifactId> 
    <version>0.7.9</version> 
</dependency> 

這個例子是相當獨立,所以你應該能夠'按原樣'抓住它。如果這不適合你,那麼也許你可以用更多的細節來更新你的問題,理想的情況是提供一個MCVE,但至少向我們展示了(a)定義你的verticle的代碼(所有這些代碼不僅僅是同步周圍的幾行呼叫)和(b)部署這個Verticle的代碼。

+0

它適合我。但是,我試圖在一條路線上做同樣的事情,也許這是原因,但我仍然無法解決它! –

+0

你也許可以更新你的問題來向我們展示(a)定義你的verticle的代碼(全部不僅僅是同步調用的幾行)和(b)部署這個verticle的代碼。 – glytching