<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>
<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>
<artifactId>log4j</artifactId>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>javaee</groupId>
+ <artifactId>javaee-api</artifactId>
+ </dependency>
</dependencies>
</project>
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)
*/
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);
}
+
}
/*
*/
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.
}
}
+++ /dev/null
-/*
- * 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();
- }
-}
+++ /dev/null
-/*
- * 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);
- }
- }
-
-}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
<modules>
<module>monitor</module>
<module>war</module>
+ <module>timer</module>
<module>ear</module>
</modules>
--- /dev/null
+<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>
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
--- /dev/null
+<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
<artifactId>wamblee-mythtv-monitor</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>javaee</groupId>
+ <artifactId>javaee-api</artifactId>
+ </dependency>
</dependencies>
<build>
<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>
--- /dev/null
+<?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
<?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>