package org.jboss.test.timer.ejb;
import java.util.Date;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.ejb.TimerHandle;
import javax.ejb.TimerService;
import javax.ejb.EJBException;
import javax.ejb.NoSuchObjectLocalException;
import org.apache.log4j.Logger;
public class TimerSLSBean
implements SessionBean, TimedObject
{
private static HashMap timeoutCounts = new HashMap();
private static Logger log = Logger.getLogger(TimerSLSBean.class);
private SessionContext context;
public byte[] startSingleTimer(long pPeriod)
{
log.info("TimerSLSBean.startSingleTimer(), try to get a Timer Service from the Session Context");
TimerService ts = context.getTimerService();
long exp = System.currentTimeMillis() + pPeriod;
Timer timer = ts.createTimer(new Date(exp), "TimerSLSBean.startSingleTimer");
log.info("TimerSLSBean.startSingleTimer(), create a timer: "+timer);
byte[] handle = getHandle(timer);
return handle;
}
public byte[] startTimer(long pPeriod)
{
log.info("TimerSLSBean.startTimer(), try to get a Timer Service from the Session Context");
TimerService ts = context.getTimerService();
long exp = System.currentTimeMillis() + pPeriod;
Timer timer = ts.createTimer(new Date(exp), pPeriod, "TimerSLSBean.startTimer");
log.info("TimerSLSBean.startTimer(), create a timer: "+timer);
byte[] handle = getHandle(timer);
return handle;
}
public void stopTimer(byte[] handle)
{
Timer timer = getTimer(handle);
timer.cancel();
log.info("TimerSLSBean.stopTimer(), create a timer: "+timer);
synchronized( TimerSLSBean.class )
{
Long key = getKey(handle);
timeoutCounts.remove(key);
}
}
public int getTimeoutCount(byte[] handle)
{
Integer count = null;
try
{
Long key = getKey(handle);
count = (Integer) timeoutCounts.get(key);
}
catch(NoSuchObjectLocalException e)
{
}
log.info("TimerSLSBean.getTimeoutCount(): " + count);
return count != null ? count.intValue() : 0;
}
public Date getNextTimeout(byte[] handle)
{
Timer timer = getTimer(handle);
return timer.getNextTimeout();
}
public long getTimeRemaining(byte[] handle)
{
Timer timer = getTimer(handle);
return timer.getTimeRemaining();
}
public Object getInfo(byte[] handle)
{
Timer timer = getTimer(handle);
return timer.getInfo();
}
public void ejbCreate()
{
log.info("TimerSLSBean.ejbCreate()");
}
public void ejbTimeout(Timer timer)
{
Integer count = null;
Long key = null;
synchronized( TimerSLSBean.class )
{
byte[] handle = getHandle(timer);
key = getKey(handle);
count = (Integer) timeoutCounts.get(key);
if( count == null )
count = new Integer(1);
else
count = new Integer(1 + count.intValue());
timeoutCounts.put(key, count);
}
log.info("ejbTimeout(), timer: " + timer+", key: "+key+", count: "+count);
}
public String toString()
{
return "TimerSLSBean [ " + " ]";
}
public void setSessionContext(SessionContext aContext)
{
context = aContext;
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void ejbRemove()
{
}
private Long getKey(byte[] handle)
{
long key = 0;
for(int n = 0; n < handle.length; n ++)
key += handle[n];
log.info("HandleKey: "+key);
return new Long(key);
}
private byte[] getHandle(Timer timer)
throws EJBException
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(timer.getHandle());
oos.close();
byte[] handle = baos.toByteArray();
return handle;
}
catch (Exception e)
{
throw new EJBException("Failed to get timer from handle", e);
}
}
private Timer getTimer(byte[] handle)
throws NoSuchObjectLocalException, EJBException
{
try
{
ByteArrayInputStream bais = new ByteArrayInputStream(handle);
ObjectInputStream ois = new ObjectInputStream(bais);
TimerHandle th = null;
th = (TimerHandle) ois.readObject();
ois.close();
Timer timer = th.getTimer();
return timer;
}
catch(NoSuchObjectLocalException e)
{
throw e;
}
catch (Exception e)
{
throw new EJBException("Failed to get timer from handle", e);
}
}
}