moved socketproxy to https://wamblee.org/svn/public/socketproxy
[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  * @author Erik Brakkee
37  */
38 public class Application implements ServletContextListener {
39     private static final Log LOG = LogFactory.getLog(Application.class);
40     
41     @Resource(name = "MythtvConnectionFactory")
42     private ConnectionFactory connectionFactory; 
43     
44     @Resource(name = "MythtvTimer")
45     private Queue timerQueue; 
46
47     /*
48      * (non-Javadoc)
49      * 
50      * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
51      */
52     public void contextInitialized(ServletContextEvent arg0) {
53         LOG.info("initializing");
54
55         // Get application configuration.
56         ScheduleConfig config = BeanKernel.getBeanFactory().find(
57                 ScheduleConfig.class);
58         
59         // Send object message to the timer with the timer interval.
60         try {
61             Connection connection = connectionFactory.createConnection();
62             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
63             ObjectMessage msg = session.createObjectMessage();
64             msg.setObject(config.getIntervalSeconds());
65             MessageProducer producer = session.createProducer(timerQueue);
66             producer.send(msg);
67             LOG.info("Message sent");
68         } catch (Exception e) {
69             LOG.fatal("Error sending message", e);
70         }
71         
72     }
73
74     /*
75      * (non-Javadoc)
76      * 
77      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
78      */
79     public void contextDestroyed(ServletContextEvent arg0) {
80         LOG.info("terminating");
81         
82         // Empty. 
83     }
84 }