[jboss-user] [EJB/JBoss] - TimerService and Timer

gardon do-not-reply at jboss.com
Thu May 10 04:45:46 EDT 2007


Hello,

I need to create repetitive task using EJB in JBoss 4.0.5. I decide to use TimerService and Timer as introduced in the EJB 2.1 specification. 

Here is the bean code:


  | /**
  |  * @author Stanislav Gardon
  |  * @ejb.bean description = "OnlineTerminalsStat" display-name =
  |  *           "OnlineTerminalsStat" type = "Stateless" name =
  |  *           "OnlineTerminalsStat" view-type = "local" local-jndi-name =
  |  *           "local/OnlineTerminalsStat" transaction-type = "Container" generate =
  |  *           "true"
  |  */
  | public class OnlineTerminalsStatBean implements SessionBean, TimedObject {
  | 
  | 	/**
  | 	 * Interval spustania
  | 	 */
  | 	private static final long INTERVAL_DURATION = 5000;
  | 
  | 	/**
  | 	 * Aktualny SessionContext
  | 	 */
  | 	private SessionContext sessionContext;
  | 
  | 	/**
  | 	 * Logger
  | 	 */
  | 	private Logger logger = Logger.getLogger(OnlineTerminalsStatBean.class);
  | 
  | 	/**
  | 	 * 
  | 	 */
  | 	public OnlineTerminalsStatBean() {
  | 		super();
  | 	}
  | 
  | 	/*
  | 	 * (non-Javadoc)
  | 	 * 
  | 	 * @see javax.ejb.SessionBean#ejbActivate()
  | 	 */
  | 	public void ejbActivate() throws EJBException, RemoteException {
  | 	}
  | 
  | 	/*
  | 	 * (non-Javadoc)
  | 	 * 
  | 	 * @see javax.ejb.SessionBean#ejbPassivate()
  | 	 */
  | 	public void ejbPassivate() throws EJBException, RemoteException {
  | 	}
  | 
  | 	/*
  | 	 * (non-Javadoc)
  | 	 * 
  | 	 * @see javax.ejb.SessionBean#ejbRemove()
  | 	 */
  | 	public void ejbRemove() throws EJBException, RemoteException {
  | 	}
  | 
  | 	/**
  | 	 * 
  | 	 * @throws CreateException
  | 	 * @ejb.create-method view-type = "local"
  | 	 */
  | 	public void ejbCreate() throws CreateException {
  | 	}
  | 
  | 	/*
  | 	 * (non-Javadoc)
  | 	 * 
  | 	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
  | 	 */
  | 	public void setSessionContext(SessionContext arg0) throws EJBException,
  | 			RemoteException {
  | 		this.sessionContext = arg0;
  | 	}
  | 
  | 	/**
  | 	 * 
  | 	 * @ejb.interface-method view-type = "local"
  | 	 * @ejb.transaction type = "Required"
  | 	 */
  | 	public void createTimer() {
  | 		TimerService ts = sessionContext.getTimerService();
  | 		Timer t = ts.createTimer(new Date(System.currentTimeMillis() + 5000),
  | 				INTERVAL_DURATION, null);
  | 		logger.debug("timer created");
  | 		logger.debug("total timers: " + ts.getTimers().size());
  | 	}
  | 
  | 	/**
  | 	 * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
  | 	 * @ejb.interface-method view-type = "local"
  | 	 * @ejb.transaction type = "RequiresNew"
  | 	 */
  | 	public void ejbTimeout(Timer t) {
  | 		logger.debug("ejbTimout, Timer: " + t);
  | 		try {
  | 			DataSource ds = DbUtil.dataSourceTx();
  | 			Connection c = ds.getConnection();
  | 			try {
  | 				String query = "BEGIN terminal_tools.save_terminal_online_stat; END;";
  | 				CallableStatement cs = c.prepareCall(query);
  | 				try {
  | 					cs.execute();
  | 				} finally {
  | 					cs.close();
  | 				}
  | 			} finally {
  | 				c.close();
  | 			}
  | 		} catch (Exception e) {
  | 			logger.error(e.getMessage(), e);
  | 		}
  | 	}
  | 
  | }
  | 

The createTimer method is called just right after deployment.

The problem is, that ejbTimout method is called by more than 10 timers each second (see log bellow). But I create only ONE timer. Why? Do you have any ideas?

Log:

  | 10:33:03,814 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] timer created
  | 10:33:03,814 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] total timers: 13
  | .
  | .
  | .
  | 

Why 13?


  | 10:41:05,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:06,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:07,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:08,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:08,814 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=13,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=5000,periode=5000,in_timeout]
  | 10:41:09,486 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=2,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-507547,periode=5000,in_timeout]
  | 10:41:09,486 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=3,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-150109,periode=5000,in_timeout]
  | 10:41:09,502 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=5,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64654002,periode=5000,in_timeout]
  | 10:41:09,533 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=6,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64518924,periode=5000,in_timeout]
  | 10:41:09,564 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=7,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64031721,periode=5000,in_timeout]
  | 10:41:09,596 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=9,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-2566375,periode=5000,in_timeout]
  | 10:41:09,658 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=12,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-69297,periode=5000,in_timeout]
  | 10:41:09,705 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=11,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-578453,periode=5000,in_timeout]
  | 10:41:09,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:10,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:11,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:12,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:13,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 10:41:13,814 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=13,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=5000,periode=5000,in_timeout]
  | 10:41:14,486 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=2,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-507547,periode=5000,in_timeout]
  | 10:41:14,486 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=3,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-150109,periode=5000,in_timeout]
  | 10:41:14,502 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=5,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64654002,periode=5000,in_timeout]
  | 10:41:14,533 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=6,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64518924,periode=5000,in_timeout]
  | 10:41:14,564 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=7,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-64031721,periode=5000,in_timeout]
  | 10:41:14,596 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=9,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-2566375,periode=5000,in_timeout]
  | 10:41:14,658 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=12,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-69297,periode=5000,in_timeout]
  | 10:41:14,705 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=11,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-578453,periode=5000,in_timeout]
  | 10:41:14,721 DEBUG [com.play4win.terminal.ejb.OnlineTerminalsStatBean] ejbTimout, Timer: [id=10,target=[target=jboss.j2ee:jndiName=local/OnlineTerminalsStat,service=EJB],remaining=-65206112,periode=1000,in_timeout]
  | 

Why ejbTimeout is called so many times?

Thanks for any help.

     Stan

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4044603#4044603

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4044603



More information about the jboss-user mailing list