KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > joram > mom > dest > mail > JavaMailQueue


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 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): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package com.scalagent.joram.mom.dest.mail;
24
25 import java.util.Properties JavaDoc;
26
27 import org.objectweb.joram.mom.dest.DestinationImpl;
28 import org.objectweb.joram.mom.dest.Queue;
29 import org.objectweb.joram.mom.proxies.ConnectionManager;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32
33 import fr.dyade.aaa.agent.AgentId;
34 import fr.dyade.aaa.agent.Channel;
35 import fr.dyade.aaa.agent.Debug;
36 import fr.dyade.aaa.agent.Notification;
37 import fr.dyade.aaa.util.Timer;
38 import fr.dyade.aaa.util.TimerTask;
39
40 /**
41  * A <code>JavaMailQueue</code> agent is an agent hosting a MOM queue, and
42  * which behaviour is provided by a <code>JavaMailQueueImpl</code> instance.
43  *
44  * @see JavaMailQueueImpl
45  */

46 public class JavaMailQueue extends Queue {
47
48   public static Logger logger =
49     Debug.getLogger("com.scalagent.joram.mom.dest.mail.JavaMailQueue");
50   
51   public static final String JavaDoc MAIL_QUEUE_TYPE = "queue.mail";
52
53   public static String JavaDoc getDestinationType() {
54     return MAIL_QUEUE_TYPE;
55   }
56
57   /**
58    * Empty constructor for newInstance().
59    */

60   public JavaMailQueue() {}
61
62   /**
63    * Creates the <tt>JavaMailQueueImpl</tt>.
64    *
65    * @param adminId Identifier of the queue administrator.
66    * @param prop The initial set of properties.
67    */

68   public DestinationImpl createsImpl(AgentId adminId, Properties JavaDoc prop) {
69     return new JavaMailQueueImpl(getId(), adminId, prop);
70   }
71
72   private transient PopTask poptask;
73
74   /**
75    * Gives this agent an opportunity to initialize after having been deployed,
76    * and each time it is loaded into memory.
77    *
78    * @param firstTime true when first called by the factory
79    *
80    * @exception Exception
81    * unspecialized exception
82    */

83   protected void agentInitialize(boolean firstTime) throws Exception JavaDoc {
84     super.agentInitialize(firstTime);
85     poptask = new PopTask(getId());
86     poptask.schedule();
87   }
88
89   public void react(AgentId from, Notification not) throws Exception JavaDoc {
90     if (not instanceof WakeUpPopNot) {
91       if (poptask == null)
92         poptask = new PopTask(getId());
93       poptask.schedule();
94       ((JavaMailQueueImpl) destImpl).doPop();
95     } else {
96       super.react(from, not);
97     }
98   }
99
100   /**
101    * Timer task responsible for doing a pop.
102    */

103   private class PopTask extends TimerTask {
104     private AgentId to;
105   
106     public PopTask(AgentId to) {
107       this.to = to;
108     }
109   
110     /** Method called when the timer expires. */
111     public void run() {
112       try {
113         Channel.sendTo(to, new WakeUpPopNot());
114       } catch (Exception JavaDoc e) {}
115     }
116
117     public void schedule() {
118       long period = ((JavaMailQueueImpl) destImpl).getPopPeriod();
119
120       if (period > 0) {
121         try {
122           Timer timer = ConnectionManager.getTimer();
123           timer.schedule(this, period);
124         } catch (Exception JavaDoc exc) {
125           if (logger.isLoggable(BasicLevel.ERROR))
126             logger.log(BasicLevel.ERROR, "--- " + this + " Queue(...)", exc);
127         }
128       }
129     }
130   }
131 }
132
Popular Tags