001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.scheduler;
034:
035: import org.quartz.Job;
036: import org.quartz.JobDetail;
037: import org.quartz.JobExecutionContext;
038: import org.quartz.JobExecutionException;
039:
040: import java.util.Map;
041:
042: import javax.jms.Message;
043: import javax.jms.Topic;
044: import javax.jms.TopicConnection;
045: import javax.jms.TopicConnectionFactory;
046: import javax.jms.TopicPublisher;
047: import javax.jms.TopicSession;
048:
049: import javax.naming.InitialContext;
050:
051: public class ThrowMessageJob implements Job {
052: private static Topic jobsTopic;
053: private static TopicConnectionFactory connectionFactory;
054:
055: public void execute(JobExecutionContext ctx)
056: throws JobExecutionException {
057: try {
058: TopicConnectionFactory factory = getConnectionFactory();
059: Topic topic = getJobsTopic();
060: TopicConnection topicConnection = factory
061: .createTopicConnection();
062: TopicSession topicSession = topicConnection
063: .createTopicSession(true,
064: javax.jms.Session.AUTO_ACKNOWLEDGE);
065: TopicPublisher topicPublisher = topicSession
066: .createPublisher(topic);
067: Message message = topicSession.createMessage();
068: JobDetail jobDetail = ctx.getJobDetail();
069: String name = jobDetail.getName();
070: String type = jobDetail.getJobDataMap().getString(
071: LibresourceJob.TYPE_KEY);
072: Map metaData = jobDetail.getJobDataMap().getWrappedMap();
073: LibresourceJob libresourceJob = new LibresourceJob(name,
074: metaData);
075: libresourceJob.toMessage(message);
076: topicPublisher.publish(message);
077: topicPublisher.close();
078: topicSession.close();
079: topicConnection.close();
080: } catch (Exception e) {
081: throw new JobExecutionException();
082: }
083: }
084:
085: public static Topic getJobsTopic() throws Exception {
086: if (jobsTopic == null) {
087: jobsTopic = (Topic) new InitialContext()
088: .lookup(org.libresource.Libresource
089: .getLibresourceConfiguration("job.topic.lookup"));
090: }
091:
092: return jobsTopic;
093: }
094:
095: public static TopicConnectionFactory getConnectionFactory()
096: throws Exception {
097: if (connectionFactory == null) {
098: connectionFactory = (TopicConnectionFactory) new InitialContext()
099: .lookup(org.libresource.Libresource
100: .getLibresourceConfiguration("topic.connection.factory"));
101: }
102:
103: return connectionFactory;
104: }
105: }
|