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(); } } } }