KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > PollingMessageReceiver


1 /*
2  * $Id: PollingMessageReceiver.java 3192 2006-09-24 22:45:24Z holger $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers;
12
13 import javax.resource.spi.work.Work JavaDoc;
14 import javax.resource.spi.work.WorkException JavaDoc;
15 import javax.resource.spi.work.WorkManager JavaDoc;
16
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.umo.UMOComponent;
20 import org.mule.umo.UMOException;
21 import org.mule.umo.endpoint.UMOEndpoint;
22 import org.mule.umo.lifecycle.InitialisationException;
23 import org.mule.umo.provider.UMOConnector;
24
25 /**
26  * <p>
27  * <code>PollingMessageReceiver</code> implements a polling message receiver. The
28  * receiver provides a poll method that implementations should implement to execute
29  * their custom code. Note that the receiver will not poll if the associated
30  * connector is not started.
31  */

32 public abstract class PollingMessageReceiver extends AbstractMessageReceiver implements Work JavaDoc
33 {
34     public static final long DEFAULT_POLL_FREQUENCY = 1000;
35     public static final long STARTUP_DELAY = 1000;
36
37     protected long frequency = DEFAULT_POLL_FREQUENCY;
38
39     public PollingMessageReceiver(UMOConnector connector,
40                                   UMOComponent component,
41                                   final UMOEndpoint endpoint,
42                                   Long JavaDoc frequency) throws InitialisationException
43     {
44         super(connector, component, endpoint);
45         this.frequency = frequency.longValue();
46     }
47
48     public void doStart() throws UMOException
49     {
50         try
51         {
52             getWorkManager().scheduleWork(this, WorkManager.INDEFINITE, null, connector);
53         }
54         catch (WorkException JavaDoc e)
55         {
56             stopped.set(true);
57             throw new InitialisationException(new Message(Messages.FAILED_TO_SCHEDULE_WORK), e, this);
58         }
59     }
60
61     public void run()
62     {
63         try
64         {
65             Thread.sleep(STARTUP_DELAY);
66             while (!stopped.get())
67             {
68                 connected.whenTrue(null);
69                 try
70                 {
71                     poll();
72                 }
73                 catch (InterruptedException JavaDoc e)
74                 {
75                     return;
76                 }
77                 catch (Exception JavaDoc e)
78                 {
79                     handleException(e);
80                 }
81                 Thread.sleep(frequency);
82             }
83         }
84         catch (InterruptedException JavaDoc e)
85         {
86             // Exit thread
87
}
88     }
89
90     public void release()
91     {
92         this.stop();
93     }
94
95     public void setFrequency(long l)
96     {
97         if (l <= 0)
98         {
99             frequency = DEFAULT_POLL_FREQUENCY;
100         }
101         else
102         {
103             frequency = l;
104         }
105     }
106
107     public long getFrequency()
108     {
109         return frequency;
110     }
111
112     public abstract void poll() throws Exception JavaDoc;
113
114 }
115
Popular Tags