From 7fdeb4db9854a2fe238489199c5415440b767e96 Mon Sep 17 00:00:00 2001 From: erik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0> Date: Tue, 5 Feb 2008 22:11:12 +0000 Subject: [PATCH] --- trunk/mythtv/ear/pom.xml | 6 ++ trunk/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 +++++++++ trunk/mythtv/pom.xml | 1 + trunk/mythtv/timer/pom.xml | 30 ++++++++ .../java/org/wamblee/timer/TimerBean.java | 75 +++++++++++++++++++ .../src/main/resources/META-INF/ejb-jar.xml | 21 ++++++ trunk/mythtv/war/pom.xml | 4 + .../org.wamblee.mythtv.application.xml | 4 +- .../mythtv/war/src/webapp/WEB-INF/sun-web.xml | 18 +++++ trunk/mythtv/war/src/webapp/WEB-INF/web.xml | 22 +++++- 14 files changed, 248 insertions(+), 146 deletions(-) delete mode 100644 trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java delete mode 100644 trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java create mode 100644 trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java create mode 100644 trunk/mythtv/timer/pom.xml create mode 100644 trunk/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java create mode 100644 trunk/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml create mode 100644 trunk/mythtv/war/src/webapp/WEB-INF/sun-web.xml diff --git a/trunk/mythtv/ear/pom.xml b/trunk/mythtv/ear/pom.xml index da812ad3..81746141 100644 --- a/trunk/mythtv/ear/pom.xml +++ b/trunk/mythtv/ear/pom.xml @@ -27,6 +27,12 @@ <artifactId>wamblee-mythtv-monitor</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.wamblee</groupId> + <artifactId>wamblee-mythtv-timer</artifactId> + <version>${project.version}</version> + <type>ejb</type> + </dependency> </dependencies> <build> diff --git a/trunk/mythtv/monitor/pom.xml b/trunk/mythtv/monitor/pom.xml index 658d6724..52333044 100644 --- a/trunk/mythtv/monitor/pom.xml +++ b/trunk/mythtv/monitor/pom.xml @@ -20,10 +20,6 @@ <groupId>org.wamblee</groupId> <artifactId>wamblee-support</artifactId> </dependency> - <dependency> - <groupId>quartz</groupId> - <artifactId>quartz</artifactId> - </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> @@ -33,6 +29,10 @@ <artifactId>log4j</artifactId> <scope>provided</scope> </dependency> + <dependency> + <groupId>javaee</groupId> + <artifactId>javaee-api</artifactId> + </dependency> </dependencies> </project> diff --git a/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java b/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java index 01d03026..53955751 100644 --- a/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/Application.java +++ b/trunk/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/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java b/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/MonitorScheduler.java deleted file mode 100644 index 321a1c23..00000000 --- a/trunk/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/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java b/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/PollDirectoryJob.java deleted file mode 100644 index e75ed119..00000000 --- a/trunk/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/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java b/trunk/mythtv/monitor/src/main/java/org/wamblee/mythtv/ScheduleConfig.java new file mode 100644 index 00000000..3478ff41 --- /dev/null +++ b/trunk/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/trunk/mythtv/pom.xml b/trunk/mythtv/pom.xml index 8765147d..fd3ed9a1 100644 --- a/trunk/mythtv/pom.xml +++ b/trunk/mythtv/pom.xml @@ -18,6 +18,7 @@ <modules> <module>monitor</module> <module>war</module> + <module>timer</module> <module>ear</module> </modules> diff --git a/trunk/mythtv/timer/pom.xml b/trunk/mythtv/timer/pom.xml new file mode 100644 index 00000000..bcd1ef88 --- /dev/null +++ b/trunk/mythtv/timer/pom.xml @@ -0,0 +1,30 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <parent> + <groupId>org.wamblee</groupId> + <artifactId>wamblee-mythtv</artifactId> + <version>0.2-SNAPSHOT</version> + </parent> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.wamblee</groupId> + <artifactId>wamblee-mythtv-timer</artifactId> + <packaging>jar</packaging> + <name>wamblee.org mythtv directory monitor TIMER</name> + <url>http://wamblee.org</url> + + <dependencies> + <dependency> + <groupId>org.wamblee</groupId> + <artifactId>wamblee-mythtv-monitor</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>javaee</groupId> + <artifactId>javaee-api</artifactId> + </dependency> + </dependencies> + +</project> diff --git a/trunk/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java b/trunk/mythtv/timer/src/main/java/org/wamblee/timer/TimerBean.java new file mode 100644 index 00000000..b7ee2bf3 --- /dev/null +++ b/trunk/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/trunk/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml b/trunk/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml new file mode 100644 index 00000000..b3c04cfd --- /dev/null +++ b/trunk/mythtv/timer/src/main/resources/META-INF/ejb-jar.xml @@ -0,0 +1,21 @@ +<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" + version="3.0"> + + <!-- enterprise-beans> + <message-driven> + <ejb-name>TimerBean</ejb-name> + <ejb-class>org.wamblee.timer.TimerBean</ejb-class> + <message-destination-link>jms/MythtvTimer</message-destination-link> + </message-driven> + </enterprise-beans> + <assembly-descriptor> + <message-destination> + <message-destination-name>jms/MythtvTimer</message-destination-name> + <mapped-name>jms/MythtvTimer</mapped-name> + </message-destination> + </assembly-descriptor --> + +</ejb-jar> \ No newline at end of file diff --git a/trunk/mythtv/war/pom.xml b/trunk/mythtv/war/pom.xml index 30c9c477..d34be658 100644 --- a/trunk/mythtv/war/pom.xml +++ b/trunk/mythtv/war/pom.xml @@ -21,6 +21,10 @@ <artifactId>wamblee-mythtv-monitor</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>javaee</groupId> + <artifactId>javaee-api</artifactId> + </dependency> </dependencies> <build> diff --git a/trunk/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml b/trunk/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml index 35681dc7..1017fffa 100644 --- a/trunk/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml +++ b/trunk/mythtv/war/src/main/resources/org.wamblee.mythtv.application.xml @@ -33,8 +33,8 @@ <constructor-arg><ref local="org.wamblee.mythtv.LinkStructure"/></constructor-arg> </bean> - <bean id="org.wamblee.mythtv.MonitorScheduler" class="org.wamblee.mythtv.MonitorScheduler"> - <constructor-arg><value>${org.wamblee.mythtv.pollinterval}</value></constructor-arg> + <bean id="org.wamblee.mythtv.ScheduleConfig" class="org.wamblee.mythtv.ScheduleConfig"> + <constructor-arg><value>${org.wamblee.mythtv.pollinterval}</value></constructor-arg> </bean> </beans> diff --git a/trunk/mythtv/war/src/webapp/WEB-INF/sun-web.xml b/trunk/mythtv/war/src/webapp/WEB-INF/sun-web.xml new file mode 100644 index 00000000..e7b2c108 --- /dev/null +++ b/trunk/mythtv/war/src/webapp/WEB-INF/sun-web.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 +Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd"> + + +<sun-web-app> + + <!-- resource-ref> + <res-ref-name>MythtvConnectionFactory</res-ref-name> + <jndi-name>jms/MythtvConnectionFactory</jndi-name> + </resource-ref> + + <resource-ref> + <res-ref-name>MythtvTimer</res-ref-name> + <jndi-name>jms/MythtvTimer</jndi-name> + </resource-ref --> + +</sun-web-app> \ No newline at end of file diff --git a/trunk/mythtv/war/src/webapp/WEB-INF/web.xml b/trunk/mythtv/war/src/webapp/WEB-INF/web.xml index d6961ea7..eb64edc6 100644 --- a/trunk/mythtv/war/src/webapp/WEB-INF/web.xml +++ b/trunk/mythtv/war/src/webapp/WEB-INF/web.xml @@ -1,12 +1,26 @@ <?xml version="1.0" encoding="UTF-8"?> -<web-app version="2.4" - xmlns="http://java.sun.com/xml/ns/j2ee" +<web-app version="2.5" + xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee - http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee + http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <listener> <listener-class>org.wamblee.mythtv.Application</listener-class> </listener> + + <!-- resource-ref> + <res-ref-name>MythtvConnectionFactory</res-ref-name> + <res-type>javax.jms.ConnectionFactory</res-type> + <res-auth>Container</res-auth> + <res-sharing-scope>Shareable</res-sharing-scope> + </resource-ref --> + + <!-- message-destination-ref> + <message-destination-ref-name>jms/MythtvTimer</message-destination-ref-name> + <message-destination-type>javax.jms.Queue</message-destination-type> + <message-destination-usage>Produces</message-destination-usage> + <message-destination-link>Timer</message-destination-link> + </message-destination-ref --> </web-app> -- 2.31.1