moved socketproxy to https://wamblee.org/svn/public/socketproxy
[utils] / mythtv / timer / src / main / java / org / wamblee / mythtv / timer / TimerBean.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.mythtv.timer;
18
19 import java.util.Collection;
20
21 import javax.annotation.Resource;
22 import javax.ejb.MessageDriven;
23 import javax.ejb.Timeout;
24 import javax.ejb.Timer;
25 import javax.ejb.TimerService;
26 import javax.ejb.TransactionAttribute;
27 import javax.ejb.TransactionAttributeType;
28 import javax.ejb.TransactionManagement;
29 import javax.ejb.TransactionManagementType;
30 import javax.jms.JMSException;
31 import javax.jms.Message;
32 import javax.jms.MessageListener;
33 import javax.jms.ObjectMessage;
34 import javax.jms.StreamMessage;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.wamblee.general.BeanKernel;
39 import org.wamblee.io.DirectoryMonitor;
40
41 /**
42  * 
43  *
44  * @author Erik Brakkee
45  */
46 @MessageDriven(name = "TimerBean")
47 // Spring's JTA transaction manager does not work with container managed transactions
48 // because it uses the UserTransaction object which glassfish forbids.
49 @TransactionManagement(TransactionManagementType.BEAN)
50 public class TimerBean implements MessageListener {
51     
52     private static final Log LOG = LogFactory.getLog(TimerBean.class);
53     
54     @Resource
55     private TimerService _timerService; 
56
57     /**
58      * Initialization of the time interval. The initialization is done through a
59      * StreamMessage with a single integer containing the time interval to use.
60      */
61     public void onMessage(Message aInitMessage) {
62         ObjectMessage msg = (ObjectMessage) aInitMessage;
63         try {
64             int interval = (Integer)msg.getObject();
65             LOG.info("Initializing timer with interval " + interval + " seconds");
66             for (Timer timer: (Collection<Timer>)_timerService.getTimers()) { 
67                 LOG.info("Canceling old timers: " + timer);
68                 timer.cancel();
69             }
70             _timerService.createTimer(1000, interval*1000, null);
71         } catch (JMSException e) {
72             throw new RuntimeException(e.getMessage());
73         }
74     }
75   
76     @Timeout
77     private void timeout(Timer aTimer) {
78         LOG.info("Timer expired!!!");
79         try {
80             DirectoryMonitor monitor = BeanKernel.getBeanFactory().find(
81                     DirectoryMonitor.class);
82             monitor.poll();
83         } catch (Throwable t) {
84             LOG
85                     .error(
86                             "something terrible happend, ignoring it and hoping for the best",
87                             t);
88         }
89     }
90 }