/* * 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.mythtv.timer; import java.util.Collection; import javax.annotation.Resource; import javax.ejb.MessageDriven; import javax.ejb.Timeout; import javax.ejb.Timer; import javax.ejb.TimerService; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; 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; /** * * * @author Erik Brakkee */ @MessageDriven(name = "TimerBean") // Spring's JTA transaction manager does not work with container managed transactions // because it uses the UserTransaction object which glassfish forbids. @TransactionManagement(TransactionManagementType.BEAN) 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"); for (Timer timer: (Collection)_timerService.getTimers()) { LOG.info("Canceling old timers: " + timer); timer.cancel(); } _timerService.createTimer(1000, interval*1000, null); } catch (JMSException e) { throw new RuntimeException(e.getMessage()); } } @Timeout private void timeout(Timer aTimer) { LOG.info("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); } } }