changeset 1831:747a32443e86

Add progress notification for thread monitoring starting/stopping Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/016408.html http://icedtea.classpath.org/pipermail/thermostat/2015-October/016742.html PR2670
author Andrew Azores <aazores@redhat.com>
date Tue, 13 Oct 2015 11:44:25 -0400
parents 237257431e93
children ffd37dfffbdd
files thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/LocaleResources.java thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationController.java thread/client-controllers/src/main/resources/com/redhat/thermostat/thread/client/controller/impl/strings.properties thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationControllerTest.java
diffstat 4 files changed, 78 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/LocaleResources.java	Fri Oct 09 10:13:28 2015 -0400
+++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/LocaleResources.java	Tue Oct 13 11:44:25 2015 -0400
@@ -47,6 +47,9 @@
 
     CHECKING_FOR_DEADLOCKS,
     NO_DEADLOCK_DETECTED,
+
+    STARTING_MONITORING,
+    STOPPING_MONITORING,
     ;
 
     static final String RESOURCE_BUNDLE = "com.redhat.thermostat.thread.client.controller.impl.strings";
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationController.java	Fri Oct 09 10:13:28 2015 -0400
+++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationController.java	Tue Oct 13 11:44:25 2015 -0400
@@ -37,6 +37,7 @@
 package com.redhat.thermostat.thread.client.controller.impl;
 
 import com.redhat.thermostat.client.core.controllers.InformationServiceController;
+import com.redhat.thermostat.client.core.progress.ProgressHandle;
 import com.redhat.thermostat.client.core.progress.ProgressNotifier;
 import com.redhat.thermostat.client.core.views.UIComponent;
 import com.redhat.thermostat.common.ActionEvent;
@@ -62,7 +63,7 @@
 public class ThreadInformationController implements InformationServiceController<VmRef> {
 
     private static final Logger logger = LoggingUtils.getLogger(ThreadInformationController.class);
-    private static final Translate<LocaleResources> t = LocaleResources.createLocalizer();
+    private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
     
     private ThreadView view;
     private ThreadCollector collector;
@@ -102,7 +103,7 @@
     
     @Override
     public LocalizedString getLocalizedName() {
-        return t.localize(LocaleResources.CONTROLLER_NAME);
+        return translator.localize(LocaleResources.CONTROLLER_NAME);
     }
 
     @Override
@@ -114,24 +115,13 @@
 
         @Override
         public void actionPerformed(ActionEvent<ThreadAction> actionEvent) {
-
-            boolean result = false;
-            
             switch (actionEvent.getActionId()) {
             case START_LIVE_RECORDING:
-                result = collector.startHarvester();
-                if (!result) {
-                    view.displayWarning(t.localize(LocaleResources.WARNING_CANNOT_ENABLE));
-                    view.setRecording(false, false);
-                }
+                startHarvester();
                 break;
             
             case STOP_LIVE_RECORDING:
-                result = collector.stopHarvester();
-                if (!result) {
-                    view.displayWarning(t.localize(LocaleResources.WARNING_CANNOT_DISABLE));
-                    view.setRecording(true, false);
-                }
+                stopHarvester();
                 break;
                 
             default:
@@ -139,6 +129,51 @@
                 break;
             }
         }
+
+        private void startHarvester() {
+            submitTask(new Runnable() {
+                @Override
+                public void run() {
+                    boolean result = collector.startHarvester();
+                    if (!result) {
+                        view.displayWarning(translator.localize(LocaleResources.WARNING_CANNOT_ENABLE));
+                        view.setRecording(false, false);
+                    }
+                }
+            }, translator.localize(LocaleResources.STARTING_MONITORING));
+        }
+
+        private void stopHarvester() {
+            submitTask(new Runnable() {
+                @Override
+                public void run() {
+                    boolean result = collector.stopHarvester();
+                    if (!result) {
+                        view.displayWarning(translator.localize(LocaleResources.WARNING_CANNOT_DISABLE));
+                        view.setRecording(true, false);
+                    }
+                }
+            }, translator.localize(LocaleResources.STOPPING_MONITORING));
+        }
+
+        private void submitTask(final Runnable task, final LocalizedString taskName) {
+            appService.getApplicationExecutor().execute(new Runnable() {
+                @Override
+                public void run() {
+                    final ProgressHandle handle = new ProgressHandle(taskName);
+                    handle.setTask(taskName);
+                    handle.setIndeterminate(true);
+                    notifier.register(handle);
+
+                    handle.start();
+                    try {
+                        task.run();
+                    } finally {
+                        handle.stop();
+                    }
+                }
+            });
+        }
     }
     
     private class ThreadSelectionActionListener implements ActionListener<ThreadSelectionAction> {
--- a/thread/client-controllers/src/main/resources/com/redhat/thermostat/thread/client/controller/impl/strings.properties	Fri Oct 09 10:13:28 2015 -0400
+++ b/thread/client-controllers/src/main/resources/com/redhat/thermostat/thread/client/controller/impl/strings.properties	Tue Oct 13 11:44:25 2015 -0400
@@ -2,4 +2,6 @@
 WARNING_CANNOT_ENABLE = Cannot enable Thread recording
 WARNING_CANNOT_DISABLE = Cannot disable Thread recording
 CHECKING_FOR_DEADLOCKS = Checking for deadlocks\u2026
-NO_DEADLOCK_DETECTED = No Deadlocks Detected.
\ No newline at end of file
+NO_DEADLOCK_DETECTED = No Deadlocks Detected.
+STARTING_MONITORING = Starting thread monitoring\u2026
+STOPPING_MONITORING = Stopping thread monitoring\u2026
\ No newline at end of file
--- a/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationControllerTest.java	Fri Oct 09 10:13:28 2015 -0400
+++ b/thread/client-controllers/src/test/java/com/redhat/thermostat/thread/client/controller/impl/ThreadInformationControllerTest.java	Tue Oct 13 11:44:25 2015 -0400
@@ -62,6 +62,8 @@
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
+import java.util.concurrent.ExecutorService;
+
 import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.doNothing;
@@ -87,6 +89,7 @@
     private VmDeadLockView deadLockView;
     private ThreadTimelineView threadTimelineView;
     private ThreadCountView threadCountView;
+    private ExecutorService appExecutor;
 
     @Before
     public void setUp() {
@@ -98,6 +101,7 @@
         when(vmInfoDao.getVmInfo(isA(VmRef.class))).thenReturn(vmInfo);
         setUpTimers();
         setUpView();
+        setupExecutor();
     }
 
     private void setUpView() {
@@ -116,6 +120,11 @@
         when(view.createThreadCountView()).thenReturn(threadCountView);
 
     }
+
+    private void setupExecutor() {
+        appExecutor = mock(ExecutorService.class);
+        when(appService.getApplicationExecutor()).thenReturn(appExecutor);
+    }
     
     private void setUpTimers() {
         Timer timer = mock(Timer.class);
@@ -169,7 +178,7 @@
     
     @Test
     public void verifyLiveRecording() {
-        
+
         ActionListener<ThreadView.ThreadAction> threadActionListener;
         ArgumentCaptor<ActionListener> viewArgumentCaptor = ArgumentCaptor.forClass(ActionListener.class);
         doNothing().when(view).addThreadActionListener(viewArgumentCaptor.capture());
@@ -193,6 +202,9 @@
 
         ProgressNotifier notifier = mock(ProgressNotifier.class);
 
+        ArgumentCaptor<Runnable> longRunningTaskCaptor = ArgumentCaptor.forClass(Runnable.class);
+        doNothing().when(appExecutor).execute(longRunningTaskCaptor.capture());
+
         controller = new ThreadInformationController(ref, appService, vmInfoDao,
                                                      collectorFactory,
                                                      viewFactory, notifier);
@@ -203,16 +215,25 @@
         threadActionListener = viewArgumentCaptor.getValue();
         threadActionListener.actionPerformed(new ActionEvent<>(view, ThreadView.ThreadAction.START_LIVE_RECORDING));
 
+        verify(appExecutor, times(1)).execute(isA(Runnable.class));
+        longRunningTaskCaptor.getValue().run();
+
         verify(view, times(1)).setRecording(false, false);
         verify(collector).startHarvester();
 
         threadActionListener.actionPerformed(new ActionEvent<>(view, ThreadView.ThreadAction.STOP_LIVE_RECORDING));
 
+        verify(appExecutor, times(2)).execute(isA(Runnable.class));
+        longRunningTaskCaptor.getValue().run();
+
         verify(collector).stopHarvester();
         verify(view, times(1)).setRecording(false, false);
 
         threadActionListener.actionPerformed(new ActionEvent<>(view, ThreadView.ThreadAction.STOP_LIVE_RECORDING));
 
+        verify(appExecutor, times(3)).execute(isA(Runnable.class));
+        longRunningTaskCaptor.getValue().run();
+
         verify(collector, times(2)).stopHarvester();
         verify(view, times(1)).setRecording(true, false);