KickJava   Java API By Example, From Geeks To Geeks.

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


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.Topic;
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>JavaMailTopic</code> agent is an agent hosting a MOM queue, and
42  * which behaviour is provided by a <code>JavaMailTopicImpl</code> instance.
43  *
44  * @see JavaMailTopicImpl
45  */

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

60   public JavaMailTopic() {}
61
62   public DestinationImpl createsImpl(AgentId adminId, Properties JavaDoc prop) {
63     JavaMailTopicImpl topicImpl = new JavaMailTopicImpl(getId(), adminId, prop);
64     return topicImpl;
65   }
66
67   private transient PopTask poptask;
68
69   /**
70    * Gives this agent an opportunity to initialize after having been deployed,
71    * and each time it is loaded into memory.
72    *
73    * @param firstTime true when first called by the factory
74    *
75    * @exception Exception
76    * unspecialized exception
77    */

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

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