001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.mail.inflow;
023:
024: import javax.resource.spi.work.Work;
025: import javax.resource.spi.work.WorkManager;
026: import javax.resource.spi.work.WorkException;
027: import javax.resource.spi.work.WorkEvent;
028: import javax.resource.spi.work.WorkListener;
029:
030: import EDU.oswego.cs.dl.util.concurrent.BoundedPriorityQueue;
031: import org.jboss.logging.Logger;
032:
033: /**
034: * @author Scott.Stark@jboss.org
035: * @version $Revision: 57189 $
036: */
037: public class NewMsgsWorker implements Work, WorkListener {
038: private static Logger log = Logger.getLogger(NewMsgsWorker.class);
039: private boolean released;
040: private WorkManager mgr;
041: private BoundedPriorityQueue pollQueue;
042: private boolean trace;
043:
044: public NewMsgsWorker(WorkManager mgr) {
045: this .mgr = mgr;
046: // The capacity needs to be externalized
047: this .pollQueue = new BoundedPriorityQueue(1024);
048: this .trace = log.isTraceEnabled();
049: }
050:
051: public void watch(MailActivation activation)
052: throws InterruptedException {
053: long now = System.currentTimeMillis();
054: activation.updateNextNewMsgCheckTime(now);
055: pollQueue.put(activation);
056: }
057:
058: public void release() {
059: released = true;
060: if (trace)
061: log.trace("released");
062: }
063:
064: public void run() {
065: if (trace)
066: log.trace("Begin run");
067: while (released == false) {
068: try {
069: MailActivation ma = (MailActivation) pollQueue.take();
070: if (ma.isReleased())
071: continue;
072: // Wait until its time to check for new msgs
073: long now = System.currentTimeMillis();
074: long nextTime = ma.getNextNewMsgCheckTime();
075: long sleepMS = nextTime - now;
076: Thread.sleep(sleepMS);
077: if (released)
078: break;
079:
080: // Now schedule excecution of the new msg check
081: mgr
082: .scheduleWork(ma, WorkManager.INDEFINITE, null,
083: this );
084: } catch (InterruptedException e) {
085: log.warn("Interrupted waiting for new msg check", e);
086: } catch (WorkException e) {
087: log.warn("Failed to schedule new msg check", e);
088: }
089: }
090: if (trace)
091: log.trace("End run");
092: }
093:
094: // --- Begin WorkListener interface methods
095: public void workAccepted(WorkEvent e) {
096: if (trace)
097: log.trace("workAccepted, e=" + e);
098: }
099:
100: public void workRejected(WorkEvent e) {
101: if (trace)
102: log.trace("workRejected, e=" + e);
103: }
104:
105: public void workStarted(WorkEvent e) {
106: if (trace)
107: log.trace("workStarted, e=" + e);
108: }
109:
110: public void workCompleted(WorkEvent e) {
111: if (trace)
112: log.trace("workCompleted, e=" + e);
113: MailActivation activation = (MailActivation) e.getWork();
114: try {
115: watch(activation);
116: } catch (InterruptedException ex) {
117: log.warn("Failed to reschedule new msg check", ex);
118: }
119: }
120: // --- End WorkListener interface methods
121:
122: }
|