KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > mail > inflow > NewMsgsWorker


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.mail.inflow;
23
24 import javax.resource.spi.work.Work JavaDoc;
25 import javax.resource.spi.work.WorkManager JavaDoc;
26 import javax.resource.spi.work.WorkException JavaDoc;
27 import javax.resource.spi.work.WorkEvent JavaDoc;
28 import javax.resource.spi.work.WorkListener JavaDoc;
29
30 import EDU.oswego.cs.dl.util.concurrent.BoundedPriorityQueue;
31 import org.jboss.logging.Logger;
32
33 /**
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 37459 $
36  */

37 public class NewMsgsWorker
38    implements Work JavaDoc, WorkListener JavaDoc
39 {
40    private static Logger log = Logger.getLogger(NewMsgsWorker.class);
41    private boolean released;
42    private WorkManager JavaDoc mgr;
43    private BoundedPriorityQueue pollQueue;
44    private boolean trace;
45
46    public NewMsgsWorker(WorkManager JavaDoc mgr)
47    {
48       this.mgr = mgr;
49       // The capacity needs to be externalized
50
this.pollQueue = new BoundedPriorityQueue(1024);
51       this.trace = log.isTraceEnabled();
52    }
53
54    public void watch(MailActivation activation) throws InterruptedException JavaDoc
55    {
56       long now = System.currentTimeMillis();
57       activation.updateNextNewMsgCheckTime(now);
58       pollQueue.put(activation);
59    }
60
61    public void release()
62    {
63       released = true;
64       if( trace )
65          log.trace("released");
66    }
67    public void run()
68    {
69       if( trace )
70          log.trace("Begin run");
71       while( released == false )
72       {
73          try
74          {
75             MailActivation ma = (MailActivation) pollQueue.take();
76             if( ma.isReleased() )
77                continue;
78             // Wait until its time to check for new msgs
79
long now = System.currentTimeMillis();
80             long nextTime = ma.getNextNewMsgCheckTime();
81             long sleepMS = nextTime - now;
82             Thread.sleep(sleepMS);
83             if( released )
84                break;
85
86             // Now schedule excecution of the new msg check
87
mgr.scheduleWork(ma, WorkManager.INDEFINITE, null, this);
88          }
89          catch(InterruptedException JavaDoc e)
90          {
91             log.warn("Interrupted waiting for new msg check", e);
92          }
93          catch (WorkException JavaDoc e)
94          {
95             log.warn("Failed to schedule new msg check", e);
96          }
97       }
98       if( trace )
99          log.trace("End run");
100    }
101
102    // --- Begin WorkListener interface methods
103
public void workAccepted(WorkEvent JavaDoc e)
104    {
105       if( trace )
106          log.trace("workAccepted, e="+e);
107    }
108
109    public void workRejected(WorkEvent JavaDoc e)
110    {
111       if( trace )
112          log.trace("workRejected, e="+e);
113    }
114
115    public void workStarted(WorkEvent JavaDoc e)
116    {
117       if( trace )
118          log.trace("workStarted, e="+e);
119    }
120
121    public void workCompleted(WorkEvent JavaDoc e)
122    {
123       if( trace )
124          log.trace("workCompleted, e="+e);
125       MailActivation activation = (MailActivation) e.getWork();
126       try
127       {
128          watch(activation);
129       }
130       catch(InterruptedException JavaDoc ex)
131       {
132          log.warn("Failed to reschedule new msg check", ex);
133       }
134    }
135    // --- End WorkListener interface methods
136

137 }
138
Popular Tags