2013-02-21 27 views
0

我是GWT的新手,我有一個很大的問題。 我有一個gwt/extjs gwt2.2.0應用程序。我需要建立一個圖表,每秒顯示關於傳入消息數量的實時信息。像系統監視器。 我發現的唯一方法是添加從服務器要求messageQueueSize並將其添加到該地區圖表定時器模塊與timer.shedule(1000)和所有的這帶定時器的gwt重繪

$while(true) 

塊中的代碼。當我試圖運行我的應用程序時,我的應用程序正在循環(因爲佔用所有線程)。請幫我解決這個問題或找到實現這部分應用程序的想法。 這裏是我想要做的代碼:

$ public class ExampleChart extends ContentPanel{ 

    ArrayList<String> timeList = new ArrayList<String>(); 
    ArrayList<Long> queueSize = new ArrayList<Long>(); 
    private final ServerManagementAsync serverManagementSvc = GWT.create(ServerManagement.class); 
     public ContentPanel createChart(final String customerId, boolean warrant){ 
     setHeading("Messages by domain"); 
     setFrame(true); 
     setSize(550, 400); 
     setLayout(new FitLayout()); 

     String url = "/gxt/chart/open-flash-chart.swf"; 
     final Chart chart = new Chart(url); 

     chart.setBorders(true); 

     timeList = getTimeList(timeList); 
     while (warrant){ 
      Timer t = new Timer(){ 
       public void run(){ 
        ExampleChart ec = new ExampleChart(); 
        AsyncCallback<Long> ac = new AsyncCallback<Long>() { 
         @Override 
         public void onFailure(Throwable throwable) { 
         } 

         @Override 
         public void onSuccess(Long integer) { 
          timeList = getTimeList(timeList); 
          if (queueSize.size()<11){ 
           queueSize.add(integer); 
          } else{ 
           for (int i = 0; i<queueSize.size()-1; i++){ 
            queueSize.set(i,queueSize.get(i+1)); 
           } 
          } 
          chart.setChartModel(getAreaChart(timeList, queueSize)); 
          add(chart); 
          repaint(); 
         } 
        }; 
        serverManagementSvc.getMessageQueueSize(customerId, ac); 
       } 
      }; 
      t.schedule(2000); 
     } 
     chart.setChartModel(getAreaChart(timeList, queueSize)); 
     add(chart); 
     repaint(); 
     return this; 

    } 

    public ArrayList<String> getTimeList(ArrayList<String> TimeList){ 
     ArrayList<String> timeList = TimeList; 
     if (timeList.size() < 11){ 
      timeList.add(getCurrentTime()); 
     } else { 
      for (int i = 0; i<timeList.size()-1; i++){ 
       timeList.set(i, timeList.get(i+1)); 
      } 
      timeList.set(10, getCurrentTime()); 
     } 
     return timeList; 
    } 

    public String getCurrentTime(){ 
     Date date = new Date(); 
     String hrs = Integer.toString(date.getHours()); 
     String min = Integer.toString(date.getMinutes()); 
     String sec = Integer.toString(date.getSeconds()); 
     return hrs + ":" + min + ":" + sec; 
    } 
    public ChartModel getAreaChart(ArrayList<String> dateList, ArrayList<Long> queueSize) 
    { 
     ChartModel cm = new ChartModel("Messages per domain", "font-size: 14px; font-family: Verdana;"); 
     cm.setBackgroundColour("#ffffff"); 
     XAxis xa = new XAxis(); 
     xa.setLabels(dateList); 
     cm.setXAxis(xa); 
     AreaChart area1 = new AreaChart(); 
     area1.setFillAlpha(0.3f); 
     area1.setColour("#ff0000"); 
     area1.setFillColour("#ff0000"); 
     for (int n = 0; n<queueSize.size(); n++){ 
       area1.addValues(queueSize.get(n)); 
     } 
     cm.addChartConfig(area1); 

     return cm; 
    } 

} 

回答

0

謝謝,但我找到了另一種解決方案。我可以創建計時器而不是t.shedule(2000)我使用t.scheduleRepeating(2000)。所以我不需要if()或while()。不管怎樣,謝謝你。如果需要,寫信給我,我會發布工作代碼。

1

「如果(權證)」聲明,而不是「而(權證)」您應該使用'雖然'聲明不需要在這裏,因爲定時器每2000毫秒重複執行run()方法。

不應該在不知道以前返回的情況下觸發異步調用。

用下面的代碼塊替換你的while塊。

 boolean isAsyncCallReturned = true; 
     if (warrant) 
     { 
      Timer t = new Timer() 
      { 
       public void run() 
       { 
        if(isAsyncCallReturned) 
        { 
         ExampleChart ec = new ExampleChart(); 
         AsyncCallback<Long> ac = new AsyncCallback<Long>() 
         { 
          @Override 
          public void onFailure(Throwable throwable) 
          { 
          } 

          @Override 
          public void onSuccess(Long integer) 
          { 
            timeList = getTimeList(timeList); 
            if (queueSize.size()<11) 
            { 
             queueSize.add(integer); 
            } 
            else 
            { 
              for (int i = 0; i<queueSize.size()-1; i++) 
              { 
               queueSize.set(i,queueSize.get(i+1)); 
              } 
            } 
            chart.setChartModel(getAreaChart(timeList, queueSize)); 
            add(chart); 
            repaint(); 
            isAsyncCallReturned = true; 
          } 
        }; 
        isAsyncCallReturned = false; 
        serverManagementSvc.getMessageQueueSize(customerId, ac); 
        } 
       } 
      }; 
      t.schedule(2000); 
     } 
+0

謝謝,但我找到了另一種解決方案。我可以創建計時器adn而不是t.shedule(2000)我使用t.scheduleRepeating(2000)。所以我不需要if()或while()。不管怎樣,謝謝你。如果需要,寫信給我,我會發布工作代碼。 – 2013-02-25 14:13:46