<< 10 Easy Ways of Navigation in Eclipse | Home | Citrix and AJAX - one bad combo? >>

Show 'n Tell Thursday: Showing a progress bar / joining threads (24 May 2007)

A fellow developer in the developerWorks forum had an issue where he wanted to load a lot of data and needed a progress bar to indicate progress. His problem was that the main thread terminated before his data loading was done and he needed help.

The solution is to use a data loading thread and Thread.join () the thread to wait for it to complete to avoid the main thread from terminating the agent. If you do not join the thread the main thread will terminate and abort the loading. The joining is bolded in the below code.

To test it out try running the below code with and without the l.join() line below commented out and see how the progress bar never reaches the end when the thread isn't joined.

Also note that I'm extending NotesThread to avoid dealing with threads etc. in Notes and that the code is no where near production quality!

import lotus.domino.*;
import javax.swing.JDialog;
import javax.swing.JProgressBar;

public class JavaAgent extends AgentBase {
   private static int COUNT = 30;
   
   public void NotesMain() {

      try {
         // initialize
         Session session = this.getSession();
         AgentContext ac = session.getAgentContext();
         
         // start thread to similate loading
         Loader l = new Loader();
         l.start();
         for (int i=0; i<COUNT; i++) {
            System.out.println("i=" + i + " (" + session.getUserName() + ")");
            Thread.sleep(100);
         }
         
         // wait for progress thread
         l.join();
         
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
   
   public static class ProgressBar {
      private JDialog dialog = null;
      private JProgressBar bar = null;
      
      public ProgressBar() {
         dialog = new JDialog();
         bar = new JProgressBar(0, COUNT);
         dialog.getContentPane().add(bar);
         dialog.setSize(250, 40);
      }
      public void visible(boolean flag) {
         dialog.setVisible(flag);
      }
      public void update(int i) {
         this.bar.setValue(i);
         this.bar.updateUI();
      }
      public void dispose() {
         if (this.dialog.isVisible()) {
            this.visible(false);
         }
         this.dialog.dispose();
      }
   }
   
   public static class Loader extends NotesThread {
      private Session session = null;
      private Database db = null;
      private View view = null;
      
      public Loader() throws NotesException {
         session = NotesFactory.createSession();
         db = session.getDatabase(null, "names.nsf");
         view = db.getView("People");
      }

      public void runNotes() {
         try {
            ProgressBar b = new ProgressBar();
            b.visible(true);
            
            for (int i=1; i<=COUNT; i++) {
               Document doc = this.view.getNthDocument(i);
               if (null != doc) {
                  System.out.println("Loaded person: " + 
                     doc.getItemValueString("Firstname") + 
                     " " + doc.getItemValueString("Lastname"));
               }
               b.update(i);
               Thread.sleep(500);
            }
            b.dispose();
            
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      
   }
}



Avatar: Hans-Peter Kuessner

Re: Show 'n Tell Thursday: Showing a progress bar / joining threads (24 May 2007)

Hi Mikkel,
thanks, great example - I never dared to "play" with threads in Notes Java agents. But now I will ;-)

Just one remark: please don't use "getNthDocument", even in examples/non-productive code. A lot of people tend to copy such examples into their own agents. As of Notes 7 Lotus still uses this method in the Domino Designer help examples, even when they state that you should never use it in loops for performance reasons in other parts of the help. I hope that this changes (see Andre Guirard's blog).

And the default hint about recycle should be applied as well.

I know you know that, and I found out (the hard way ;-) ), but it will help others.
Avatar: PDay

Re: Show 'n Tell Thursday: Showing a progress bar / joining threads (24 May 2007)

Is it possible to get this window to keep focus ?

Add a comment Send a TrackBack