Java Agent - Timer ActionListener problem

I have developed an agent that processes a bunch of documents and displays the status via a progress bar. Works fine in Eclipse, and works OK the first time I run it in Notes. However, with subsequent runs in Notes, the actionListener associated with the timer never fires, so the progress bar never updates.

In troubleshooting the agent, I stripped it down to the bare minimum and it still shows the same behaviour. The following agent will print the various ‘update’ messages the first time it runs, but they will be ignored on any subsequent runs until Notes is restarted:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Timer;

import lotus.domino.*;

public class ProgressTestStart extends AgentBase {

int maxItems;

int currentItem;

int lastItem = -1;



public void NotesMain() {



	try {



		Session session = getSession();

		AgentContext agentContext = session.getAgentContext();



		maxItems = 500;

		

		ActionListener a = new ActionListener ()

                     {

                       public void actionPerformed (ActionEvent e)

                       {

						update();

                         }

          };





		Timer timer = new Timer(100, a);

		

		System.out.println("Starting timer");

		timer.start();

		

		for (int x = 0;x < maxItems;x++) {

	    currentItem = x;

		Thread.sleep(10);

	   	};

	    

	    timer.stop();	    

	    

	    System.out.println("Ending timer");

					

	    	

	} catch(Exception e) {

		e.printStackTrace();

	}

}



public void update() {

		

		if (currentItem != lastItem) {

			

			System.out.println("Update: " + currentItem);

			

			lastItem = currentItem;

				

		}



}

}

Can anyone help?

Thanks,

Karl

Subject: Java Agent - Timer ActionListener problem

Very curious.

I have an even simpler version of the same thing and it behaves the same way: after Domino server starts, the agent runs as expected - the first time; afterwards, actionPerformed(…) is never invoked.

I then tried re-creating the ActionListener in each loop:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.Timer;

import lotus.domino.*;

public class JavaAgent extends AgentBase

{ public void NotesMain()

{	ActionListener	al_InputPaused;

	Timer		tm_InputPaused;



	tm_InputPaused = new Timer( 1000, null );

	tm_InputPaused.setRepeats( false );



	for( int i__Loop = 0; i__Loop < 5; i__Loop++ )

	{	al_InputPaused = new ActionListener()

			{	public void actionPerformed( ActionEvent ae_AdsbPaused )

				{	System.out.println( "ActionListener called." );

				}

			};

		tm_InputPaused.addActionListener( al_InputPaused );

		tm_InputPaused.start();

		System.out.println( "Timer is" + (tm_InputPaused.isRunning() ? " " : " not ") + "running and has " + tm_InputPaused.getActionListeners().length + " ActionListener(s)" );



		try

		{	sleep( 5000 );

		} catch( Exception ex )

		{

		}

// tm_InputPaused.stop();

		tm_InputPaused.removeActionListener( al_InputPaused );

		System.out.println( "Timer is" + (tm_InputPaused.isRunning() ? " " : " not ") + "running and has " + tm_InputPaused.getActionListeners().length + " ActionListener(s)" );

		al_InputPaused = null;

	}

}

}

Stopping the Timer doesn’t help either.

This is from the log, just after startup:

12/02/2010 10:39:54 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 10:39:55 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: ActionListener called.

12/02/2010 10:39:59 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is not running and has 0 ActionListener(s)

12/02/2010 10:39:59 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 10:40:00 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: ActionListener called.

12/02/2010 10:40:04 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is not running and has 0 ActionListener(s)

12/02/2010 10:40:04 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 10:40:05 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: ActionListener called.

12/02/2010 10:40:09 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is not running and has 0 ActionListener(s)

12/02/2010 10:40:09 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 10:40:10 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: ActionListener called.

12/02/2010 10:40:14 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is not running and has 0 ActionListener(s)

12/02/2010 10:40:14 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 10:40:15 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: ActionListener called.

Subsequent entries, whether the agent runs every 5 minutes, 30 minutes, or 60 minutes look like this:

12/02/2010 11:10:38 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 11:10:43 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 0 ActionListener(s)

12/02/2010 11:10:43 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 11:10:48 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 0 ActionListener(s)

12/02/2010 11:10:48 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 11:10:53 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 0 ActionListener(s)

12/02/2010 11:10:53 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 11:10:58 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 0 ActionListener(s)

12/02/2010 11:10:58 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 1 ActionListener(s)

12/02/2010 11:11:03 AM AMgr: Agent (‘ListenerTest’ in ‘.nsf’) printing: Timer is running and has 0 ActionListener(s)

Timer.stop() and Timer.restart() are extremely handy in my actual code. java.util.Timer is not so flexible.

P.S. We’re running Domino 7.

Subject: More info, possible solution…

I changed the timer from javax.swing to java.util.Timer, and it seems to work now:

Timer timer = new Timer();

TimerTask task = new TimerTask(){

public void run() {

update();

}

};

timer.schedule(task,0,100);

Are there any issues with that? It seems to work, but I’d still be interested to know why the Javax.swing version didn’t work if anyone has any insight.

Thanks,

Karl