1 10 11 package org.mule.providers; 12 13 import javax.resource.spi.work.Work ; 14 import javax.resource.spi.work.WorkException ; 15 import javax.resource.spi.work.WorkManager ; 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 32 public abstract class PollingMessageReceiver extends AbstractMessageReceiver implements Work 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 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 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 e) 74 { 75 return; 76 } 77 catch (Exception e) 78 { 79 handleException(e); 80 } 81 Thread.sleep(frequency); 82 } 83 } 84 catch (InterruptedException e) 85 { 86 } 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 ; 113 114 } 115 | Popular Tags |