0

我是 GWT 的新手,我遇到了一个大问题。我有一个 gwt/extjs gwt2.2.0 应用程序。我需要建立一个图表,显示每秒传入消息数量的实时信息。系统监视器之类的东西。我发现的唯一方法是添加从服务器调用 messageQueueSize 的代码,并将其添加到区域图中,使用 timer.shedule(1000) 将其添加到计时器块中,并将所有这些添加到

$while(true) 

堵塞。当我尝试运行我的应用程序时,我的应用程序正在循环(因为 While 占用了所有线程)。请帮我解决这个问题或找到实现这部分应用程序的想法。这是我正在尝试做的代码:

$ 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;
    }

}
4

2 回答 2

1

您应该使用'if(warrant)'语句而不是'while(warrant)'。这里不需要'While'语句,因为定时器每 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);
       }
于 2013-02-21T15:03:45.197 回答
0

谢谢,但我找到了另一个解决方案。我可以创建计时器,而不是 t.shedule(2000),我使用 t.scheduleRepeating(2000)。所以我不需要 if() 或 while()。还是非常感谢。如果需要,写信给我,我会发布工作代码。

于 2013-02-25T14:14:05.670 回答