(no commit message)
[utils] / mythtv / monitor / src / main / java / org / wamblee / mythtv / Application.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;
18
19 import javax.annotation.Resource;
20 import javax.jms.Connection;
21 import javax.jms.ConnectionFactory;
22 import javax.jms.MessageProducer;
23 import javax.jms.ObjectMessage;
24 import javax.jms.Queue;
25 import javax.jms.Session;
26 import javax.servlet.ServletContextEvent;
27 import javax.servlet.ServletContextListener;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.wamblee.general.BeanKernel;
32
33 /**
34  * 
35  */
36 public class Application implements ServletContextListener {
37     private static final Log LOG = LogFactory.getLog(Application.class);
38     
39     @Resource(mappedName = "jms/MythtvConnectionFactory")
40     private ConnectionFactory connectionFactory; 
41     
42     @Resource(mappedName = "jms/MythtvTimer")
43     private Queue timerQueue; 
44
45     /*
46      * (non-Javadoc)
47      * 
48      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
49      */
50     public void contextInitialized(ServletContextEvent arg0) {
51         LOG.info("initializing");
52
53         // Get application configuration.
54         ScheduleConfig config = BeanKernel.getBeanFactory().find(
55                 ScheduleConfig.class);
56         
57         // Send object message to the timer with the timer interval.
58         try {
59             Connection connection = connectionFactory.createConnection();
60             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
61             ObjectMessage msg = session.createObjectMessage();
62             msg.setObject(config.getIntervalSeconds());
63             MessageProducer producer = session.createProducer(timerQueue);
64             producer.send(msg);
65         } catch (Exception e) {
66             LOG.fatal("Error sending message", e);
67         }
68         
69     }
70
71     /*
72      * (non-Javadoc)
73      * 
74      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
75      */
76     public void contextDestroyed(ServletContextEvent arg0) {
77         LOG.info("terminating");
78         
79         // TODO check if timer will be automatically stopped. 
80         // Empty. 
81     }
82 }