KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > mom > dest > scheduler > SchedulerQueueImpl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - 2007 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package com.scalagent.joram.mom.dest.scheduler;
24
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 import org.objectweb.joram.mom.dest.QueueImpl;
30 import org.objectweb.joram.mom.messages.Message;
31 import org.objectweb.joram.mom.notifications.ClientMessages;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34
35 import com.scalagent.scheduler.AddConditionListener;
36 import com.scalagent.scheduler.Condition;
37 import com.scalagent.scheduler.RemoveConditionListener;
38 import com.scalagent.scheduler.ScheduleEvent;
39 import com.scalagent.scheduler.Scheduler;
40
41 import fr.dyade.aaa.agent.AgentId;
42 import fr.dyade.aaa.agent.Debug;
43
44 public class SchedulerQueueImpl extends QueueImpl {
45   public static Logger logger =
46       Debug.getLogger("com.scalagent.joram.scheduler.SchedulerQueueImpl");
47
48   public static final String JavaDoc SCHEDULE_DATE = "scheduleDate";
49
50   public static final String JavaDoc SCHEDULED = "scheduled";
51
52   /**
53    * Constructs a <code>SchedulerQueueImpl</code> instance.
54    *
55    * @param destId Identifier of the agent hosting the queue.
56    * @param adminId Identifier of the administrator of the queue.
57    * @param prop The initial set of properties.
58    */

59   public SchedulerQueueImpl(AgentId destId, AgentId adminId, Properties JavaDoc prop) {
60     super(destId, adminId, prop);
61     if (logger.isLoggable(BasicLevel.DEBUG))
62       logger.log(BasicLevel.DEBUG,
63                  "SchedulerQueueImpl.<init>(" + destId + ',' + adminId + ')');
64   }
65
66   public void postProcess(ClientMessages not) {
67     if (logger.isLoggable(BasicLevel.DEBUG))
68       logger.log(BasicLevel.DEBUG, "SchedulerQueueImpl.postProcess(" + not + ')');
69     
70     org.objectweb.joram.shared.messages.Message msg;
71     for (Enumeration JavaDoc msgs = not.getMessages().elements(); msgs.hasMoreElements();) {
72       msg = (org.objectweb.joram.shared.messages.Message) msgs.nextElement();
73       long scheduleDate = getScheduleDate(msg);
74       if (scheduleDate < 0) return;
75       //DF: to improve
76
// two notifs are necessary to subscribe
77
forward(Scheduler.getDefault(), new AddConditionListener(msg.id));
78       forward(Scheduler.getDefault(), new ScheduleEvent(msg.id, new Date JavaDoc(scheduleDate)));
79     }
80   }
81
82   private static long getScheduleDate(org.objectweb.joram.shared.messages.Message msg) {
83     Object JavaDoc scheduleDateValue = msg.getProperty(SCHEDULE_DATE);
84     if (scheduleDateValue == null) return -1;
85     try {
86       return ((Long JavaDoc)scheduleDateValue).longValue();
87     } catch (Exception JavaDoc exc) {
88       if (logger.isLoggable(BasicLevel.WARN))
89         logger.log(BasicLevel.WARN, "Scheduled message error", exc);
90       return -1;
91     }
92   }
93
94   public void condition(Condition not) {
95     String JavaDoc msgId = not.name;
96     for (int i = 0; i < messages.size(); i++) {
97       Message msg = (Message) messages.elementAt(i);
98       if (msg.getIdentifier().equals(msgId)) {
99         try {
100           msg.setObjectProperty(SCHEDULED, "" + System.currentTimeMillis());
101         } catch (Exception JavaDoc exc) {}
102         // Must remove the condition
103
forward(Scheduler.getDefault(),
104                 new RemoveConditionListener(msg.getIdentifier()));
105         break;
106       }
107     }
108     deliverMessages(0);
109   }
110
111   protected boolean checkDelivery(org.objectweb.joram.shared.messages.Message msg) {
112     if (logger.isLoggable(BasicLevel.DEBUG))
113       logger.log(BasicLevel.DEBUG,
114                  "SchedulerQueueImpl.checkDelivery(" + msg + ')');
115     long scheduleDate = getScheduleDate(msg);
116     if (scheduleDate < 0) {
117       return true;
118     } else {
119       long currentTime = System.currentTimeMillis();
120       return !(scheduleDate > currentTime);
121
122     }
123   }
124 }
125
Popular Tags