X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=mythtv%2Ftimer%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftimer%2FTimerBean.java;fp=mythtv%2Ftimer%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftimer%2FTimerBean.java;h=b7ee2bf3da0eb4818c0033a65630092b283deb46;hb=f068b3e2c5cbb4498f5d223e97fb8e34cbae60cb;hp=0000000000000000000000000000000000000000;hpb=c1f188a01f9e7d9f389b2203a6f22a59128ce1c6;p=utils diff --git a/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java b/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java new file mode 100644 index 00000000..b7ee2bf3 --- /dev/null +++ b/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java @@ -0,0 +1,75 @@ +/* + * Copyright 2006 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.timer; + +import javax.annotation.Resource; +import javax.ejb.MessageDriven; +import javax.ejb.Timeout; +import javax.ejb.Timer; +import javax.ejb.TimerService; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; +import javax.jms.StreamMessage; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wamblee.general.BeanKernel; +import org.wamblee.io.DirectoryMonitor; + +/** + * + */ +@MessageDriven(mappedName = "jms/MythtvTimer") +public class TimerBean implements MessageListener { + + private static final Log LOG = LogFactory.getLog(TimerBean.class); + + @Resource + private TimerService _timerService; + + /** + * Initialization of the time interval. The initialization is done through a + * StreamMessage with a single integer containing the time interval to use. + */ + public void onMessage(Message aInitMessage) { + ObjectMessage msg = (ObjectMessage) aInitMessage; + try { + int interval = (Integer)msg.getObject(); + LOG.info("Initializing timer with interval " + interval + " seconds"); + _timerService.createTimer(interval*1000, interval*1000, null); + } catch (JMSException e) { + throw new RuntimeException(e.getMessage()); + } + } + + @Timeout + private void timeout(Timer aTimer) { + LOG.debug("Timer expired!!!"); + try { + DirectoryMonitor monitor = BeanKernel.getBeanFactory().find( + DirectoryMonitor.class); + monitor.poll(); + } catch (Throwable t) { + LOG + .error( + "something terrible happend, ignoring it and hoping for the best", + t); + } + } +}