com/scalagent/joram/mom/dest/scheduler/SchedulerQueueImpl.java

00001 /*
00002  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
00003  * Copyright (C) 2005 - 2008 ScalAgent Distributed Technologies
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00018  * USA.
00019  *
00020  * Initial developer(s): ScalAgent Distributed Technologies
00021  * Contributor(s): 
00022  */
00023 package com.scalagent.joram.mom.dest.scheduler;
00024 
00025 import java.util.Date;
00026 import java.util.Enumeration;
00027 import java.util.Properties;
00028 
00029 import org.objectweb.joram.mom.dest.QueueImpl;
00030 import org.objectweb.joram.mom.messages.Message;
00031 import org.objectweb.joram.mom.notifications.ClientMessages;
00032 import org.objectweb.util.monolog.api.BasicLevel;
00033 import org.objectweb.util.monolog.api.Logger;
00034 
00035 import com.scalagent.scheduler.AddConditionListener;
00036 import com.scalagent.scheduler.Condition;
00037 import com.scalagent.scheduler.RemoveConditionListener;
00038 import com.scalagent.scheduler.ScheduleEvent;
00039 import com.scalagent.scheduler.Scheduler;
00040 
00041 import fr.dyade.aaa.agent.AgentId;
00042 import fr.dyade.aaa.agent.Debug;
00043 
00044 public class SchedulerQueueImpl extends QueueImpl {
00046   private static final long serialVersionUID = 1L;
00047 
00048   public static Logger logger =
00049       Debug.getLogger("com.scalagent.joram.scheduler.SchedulerQueueImpl");
00050 
00051   public static final String SCHEDULE_DATE = "scheduleDate";
00052 
00053   public static final String SCHEDULED = "scheduled";
00054 
00061   public SchedulerQueueImpl(AgentId adminId, Properties prop) {
00062     super(adminId, prop);
00063     if (logger.isLoggable(BasicLevel.DEBUG))
00064       logger.log(BasicLevel.DEBUG, 
00065                  "SchedulerQueueImpl.<init>(" + getId() + ',' + adminId + ')');
00066   }
00067 
00068   public void postProcess(ClientMessages not) {
00069     if (logger.isLoggable(BasicLevel.DEBUG))
00070       logger.log(BasicLevel.DEBUG, "SchedulerQueueImpl.postProcess(" + not + ')');
00071     
00072     org.objectweb.joram.shared.messages.Message msg;
00073     for (Enumeration msgs = not.getMessages().elements(); msgs.hasMoreElements();) {
00074       msg = (org.objectweb.joram.shared.messages.Message) msgs.nextElement();
00075       long scheduleDate = getScheduleDate(msg);
00076       if (scheduleDate < 0) return;
00077       //DF: to improve
00078       // two notifs are necessary to subscribe
00079       forward(Scheduler.getDefault(), new AddConditionListener(msg.id));
00080       forward(Scheduler.getDefault(), new ScheduleEvent(msg.id, new Date(scheduleDate)));
00081     }
00082   }
00083 
00084   private static long getScheduleDate(org.objectweb.joram.shared.messages.Message msg) {
00085     Object scheduleDateValue = msg.getProperty(SCHEDULE_DATE);
00086     if (scheduleDateValue == null) return -1;
00087     try {
00088       return ((Long)scheduleDateValue).longValue();
00089     } catch (Exception exc) {
00090       if (logger.isLoggable(BasicLevel.WARN))
00091         logger.log(BasicLevel.WARN, "Scheduled message error", exc);
00092       return -1;
00093     }
00094   }
00095 
00096   public void condition(Condition not) {
00097     String msgId = not.name;
00098     for (int i = 0; i < messages.size(); i++) {
00099       Message msg = (Message) messages.elementAt(i);
00100       if (msg.getIdentifier().equals(msgId)) {
00101         try {
00102           msg.setObjectProperty(SCHEDULED, "" + System.currentTimeMillis());
00103         } catch (Exception exc) {}
00104         // Must remove the condition
00105         forward(Scheduler.getDefault(), 
00106                 new RemoveConditionListener(msg.getIdentifier()));
00107         break;
00108       }
00109     }
00110     deliverMessages(0);
00111   }
00112 
00113   protected boolean checkDelivery(org.objectweb.joram.shared.messages.Message msg) {
00114     if (logger.isLoggable(BasicLevel.DEBUG))
00115       logger.log(BasicLevel.DEBUG,
00116                  "SchedulerQueueImpl.checkDelivery(" + msg + ')');
00117     long scheduleDate = getScheduleDate(msg);
00118     if (scheduleDate < 0) {
00119       return true;
00120     } else {
00121       long currentTime = System.currentTimeMillis();
00122       return !(scheduleDate > currentTime);
00123     }
00124   }
00125 }

Generated on Tue Sep 16 16:14:23 2008 for joram by  doxygen 1.5.0