+/*
+ * 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);
+ }
+ }
+}