From 9e0f847646d5700547a751f96fad5ef018e2f829 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 5 Feb 2008 22:11:12 +0000 Subject: [PATCH] --- mythtv/ear/pom.xml | 6 ++ mythtv/monitor/pom.xml | 8 +- .../java/org/wamblee/mythtv/Application.java | 44 ++++++++--- .../org/wamblee/mythtv/MonitorScheduler.java | 67 ----------------- .../org/wamblee/mythtv/PollDirectoryJob.java | 57 -------------- .../org/wamblee/mythtv/ScheduleConfig.java | 37 +++++++++ mythtv/pom.xml | 1 + mythtv/timer/pom.xml | 30 ++++++++ .../java/org/wamblee/timer/TimerBean.java | 75 +++++++++++++++++++ .../src/main/resources/META-INF/ejb-jar.xml | 21 ++++++ mythtv/war/pom.xml | 4 + .../org.wamblee.mythtv.application.xml | 4 +- mythtv/war/src/webapp/WEB-INF/sun-web.xml | 18 +++++ mythtv/war/src/webapp/WEB-INF/web.xml | 22 +++++- 14 files changed, 248 insertions(+), 146 deletions(-) delete mode 100644 mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java delete mode 100644 mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java create mode 100644 mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java create mode 100644 mythtv/timer/pom.xml create mode 100644 mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java create mode 100644 mythtv/timer/src/main/resources/META-INF/ejb-jar.xml create mode 100644 mythtv/war/src/webapp/WEB-INF/sun-web.xml diff --git a/mythtv/ear/pom.xml b/mythtv/ear/pom.xml index da812ad3..81746141 100644 --- a/mythtv/ear/pom.xml +++ b/mythtv/ear/pom.xml @@ -27,6 +27,12 @@ wamblee-mythtv-monitor ${project.version} + + org.wamblee + wamblee-mythtv-timer + ${project.version} + ejb + diff --git a/mythtv/monitor/pom.xml b/mythtv/monitor/pom.xml index 658d6724..52333044 100644 --- a/mythtv/monitor/pom.xml +++ b/mythtv/monitor/pom.xml @@ -20,10 +20,6 @@ org.wamblee wamblee-support - - quartz - quartz - javax.servlet servlet-api @@ -33,6 +29,10 @@ log4j provided + + javaee + javaee-api + diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java index 01d03026..53955751 100644 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java @@ -16,19 +16,31 @@ package org.wamblee.mythtv; +import javax.annotation.Resource; +import javax.jms.Connection; +import javax.jms.ConnectionFactory; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Queue; +import javax.jms.Session; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.quartz.SchedulerException; import org.wamblee.general.BeanKernel; /** - * f + * */ public class Application implements ServletContextListener { private static final Log LOG = LogFactory.getLog(Application.class); + + @Resource(mappedName = "jms/MythtvConnectionFactory") + private ConnectionFactory connectionFactory; + + @Resource(mappedName = "jms/MythtvTimer") + private Queue timerQueue; /* * (non-Javadoc) @@ -37,12 +49,23 @@ public class Application implements ServletContextListener { */ public void contextInitialized(ServletContextEvent arg0) { LOG.info("initializing"); + + // Get application configuration. + ScheduleConfig config = BeanKernel.getBeanFactory().find( + ScheduleConfig.class); + + // Send object message to the timer with the timer interval. try { - BeanKernel.getBeanFactory().find(MonitorScheduler.class) - .initialize(); - } catch (SchedulerException e) { - LOG.error("Error starting scheduler", e); + Connection connection = connectionFactory.createConnection(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + ObjectMessage msg = session.createObjectMessage(); + msg.setObject(config.getIntervalSeconds()); + MessageProducer producer = session.createProducer(timerQueue); + producer.send(msg); + } catch (Exception e) { + LOG.fatal("Error sending message", e); } + } /* @@ -52,11 +75,8 @@ public class Application implements ServletContextListener { */ public void contextDestroyed(ServletContextEvent arg0) { LOG.info("terminating"); - try { - BeanKernel.getBeanFactory().find(MonitorScheduler.class) - .shutdown(); - } catch (SchedulerException e) { - LOG.error("Error stopping scheduler", e); - } + + // TODO check if timer will be automatically stopped. + // Empty. } } diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java deleted file mode 100644 index 321a1c23..00000000 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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; - -import java.io.File; -import java.util.Date; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.quartz.JobDetail; -import org.quartz.Scheduler; -import org.quartz.SchedulerException; -import org.quartz.SchedulerFactory; -import org.quartz.Trigger; -import org.quartz.TriggerUtils; -import org.quartz.impl.StdSchedulerFactory; - -/** - * - */ -public class MonitorScheduler { - - private static final Log LOG = LogFactory.getLog(MonitorScheduler.class); - private static final String JOB_NAME = "vcrmonitor"; - private static final String TRIGGER_NAME = "trigger"; - - private Scheduler _scheduler; - private int _intervalSeconds; - - public MonitorScheduler(int aInterval) throws SchedulerException { - SchedulerFactory schedulerFactory = new StdSchedulerFactory(); - _scheduler = schedulerFactory.getScheduler(); - _intervalSeconds = aInterval; - } - - public void initialize() throws SchedulerException { - LOG.info("Starting scheduler"); - _scheduler.start(); - - JobDetail jobDetail = new JobDetail(JOB_NAME, null, PollDirectoryJob.class); - Trigger trigger = TriggerUtils.makeSecondlyTrigger(_intervalSeconds); - //trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date())); - trigger.setStartTime(new Date()); - trigger.setName(TRIGGER_NAME); - - _scheduler.scheduleJob(jobDetail, trigger); - } - - public void shutdown() throws SchedulerException { - LOG.info("Stopping scheduler"); - _scheduler.shutdown(); - } -} diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java deleted file mode 100644 index e75ed119..00000000 --- a/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.quartz.JobExecutionContext; -import org.quartz.JobExecutionException; -import org.quartz.StatefulJob; -import org.wamblee.general.BeanKernel; -import org.wamblee.io.DirectoryMonitor; - -/** - * - */ -public class PollDirectoryJob implements StatefulJob { - - private static final Log LOG = LogFactory.getLog(PollDirectoryJob.class); - - public PollDirectoryJob() { - // Empty - } - - /* - * (non-Javadoc) - * - * @see org.quartz.Job#execute(org.quartz.JobExecutionContext) - */ - public void execute(JobExecutionContext aContext) - throws JobExecutionException { - 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); - } - } - -} diff --git a/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java b/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java new file mode 100644 index 00000000..3478ff41 --- /dev/null +++ b/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java @@ -0,0 +1,37 @@ +/* + * 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; + +/** + * + */ +public class ScheduleConfig { + + private int _intervalSeconds; + + public ScheduleConfig(int aIntervalSeconds) { + _intervalSeconds = aIntervalSeconds; + } + + /** + * @return the intervalSeconds + */ + public int getIntervalSeconds() { + return _intervalSeconds; + } + +} diff --git a/mythtv/pom.xml b/mythtv/pom.xml index 8765147d..fd3ed9a1 100644 --- a/mythtv/pom.xml +++ b/mythtv/pom.xml @@ -18,6 +18,7 @@ monitor war + timer ear diff --git a/mythtv/timer/pom.xml b/mythtv/timer/pom.xml new file mode 100644 index 00000000..bcd1ef88 --- /dev/null +++ b/mythtv/timer/pom.xml @@ -0,0 +1,30 @@ + + + + org.wamblee + wamblee-mythtv + 0.2-SNAPSHOT + + + 4.0.0 + org.wamblee + wamblee-mythtv-timer + jar + wamblee.org mythtv directory monitor TIMER + http://wamblee.org + + + + org.wamblee + wamblee-mythtv-monitor + ${project.version} + + + javaee + javaee-api + + + + diff --git a/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java b/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java new file mode 100644 index 00000000..b7ee2bf3 --- /dev/null +++ b/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java @@ -0,0 +1,75 @@ +/* + * 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.timer; + +import javax.annotation.Resource; +import javax.ejb.MessageDriven; +import javax.ejb.Timeout; +import javax.ejb.Timer; +import javax.ejb.TimerService; +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; + +/** + * + */ +@MessageDriven(mappedName = "jms/MythtvTimer") +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"); + _timerService.createTimer(interval*1000, interval*1000, null); + } catch (JMSException e) { + throw new RuntimeException(e.getMessage()); + } + } + + @Timeout + private void timeout(Timer aTimer) { + LOG.debug("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); + } + } +} diff --git a/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml b/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml new file mode 100644 index 00000000..b3c04cfd --- /dev/null +++ b/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml @@ -0,0 +1,21 @@ + + + + + \ No newline at end of file diff --git a/mythtv/war/pom.xml b/mythtv/war/pom.xml index 30c9c477..d34be658 100644 --- a/mythtv/war/pom.xml +++ b/mythtv/war/pom.xml @@ -21,6 +21,10 @@ wamblee-mythtv-monitor ${project.version} + + javaee + javaee-api + diff --git a/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml b/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml index 35681dc7..1017fffa 100644 --- a/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml +++ b/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml @@ -33,8 +33,8 @@ - - ${org.wamblee.mythtv.pollinterval} + + ${org.wamblee.mythtv.pollinterval} diff --git a/mythtv/war/src/webapp/WEB-INF/sun-web.xml b/mythtv/war/src/webapp/WEB-INF/sun-web.xml new file mode 100644 index 00000000..e7b2c108 --- /dev/null +++ b/mythtv/war/src/webapp/WEB-INF/sun-web.xml @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/mythtv/war/src/webapp/WEB-INF/web.xml b/mythtv/war/src/webapp/WEB-INF/web.xml index d6961ea7..eb64edc6 100644 --- a/mythtv/war/src/webapp/WEB-INF/web.xml +++ b/mythtv/war/src/webapp/WEB-INF/web.xml @@ -1,12 +1,26 @@ - + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> org.wamblee.mythtv.Application + + + + -- 2.31.1