now using JPA annotations.
[utils] / mythtv / timer / src / main / java / org / wamblee / 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.timer;
18
19 import javax.annotation.Resource;
20 import javax.ejb.MessageDriven;
21 import javax.ejb.Timeout;
22 import javax.ejb.Timer;
23 import javax.ejb.TimerService;
24 import javax.ejb.TransactionAttribute;
25 import javax.ejb.TransactionAttributeType;
26 import javax.ejb.TransactionManagement;
27 import javax.ejb.TransactionManagementType;
28 import javax.jms.JMSException;
29 import javax.jms.Message;
30 import javax.jms.MessageListener;
31 import javax.jms.ObjectMessage;
32 import javax.jms.StreamMessage;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.wamblee.general.BeanKernel;
37 import org.wamblee.io.DirectoryMonitor;
38
39 /**
40  * 
41  */
42 @MessageDriven(name = "MythtvTimer")
43 // Spring's JTA transaction manager does not work with container managed transactions
44 // because it uses the UserTransaction object which glassfish forbids.
45 @TransactionManagement(TransactionManagementType.BEAN)
46 public class TimerBean implements MessageListener {
47     
48     private static final Log LOG = LogFactory.getLog(TimerBean.class);
49     
50     @Resource
51     private TimerService _timerService; 
52
53     /**
54      * Initialization of the time interval. The initialization is done through a
55      * StreamMessage with a single integer containing the time interval to use.
56      */
57     public void onMessage(Message aInitMessage) {
58         ObjectMessage msg = (ObjectMessage) aInitMessage;
59         try {
60             int interval = (Integer)msg.getObject();
61             LOG.info("Initializing timer with interval " + interval + " seconds");
62             _timerService.createTimer(interval*1000, interval*1000, null);
63         } catch (JMSException e) {
64             throw new RuntimeException(e.getMessage());
65         }
66     }
67   
68     @Timeout
69     private void timeout(Timer aTimer) {
70         LOG.debug("Timer expired!!!");
71         try {
72             DirectoryMonitor monitor = BeanKernel.getBeanFactory().find(
73                     DirectoryMonitor.class);
74             monitor.poll();
75         } catch (Throwable t) {
76             LOG
77                     .error(
78                             "something terrible happend, ignoring it and hoping for the best",
79                             t);
80         }
81     }
82 }