1
2
3
4
5
6
7 package org.mule.example.notifications;
8
9 import org.mule.AbstractAgent;
10 import org.mule.api.DefaultMuleException;
11 import org.mule.api.MuleException;
12 import org.mule.api.lifecycle.InitialisationException;
13
14 import javax.resource.spi.work.Work;
15 import javax.resource.spi.work.WorkException;
16
17
18
19
20
21 public class HeartbeatAgent extends AbstractAgent
22 {
23 public static final String NAME = "Heartbeat";
24
25 private long frequency = 10000;
26
27 public HeartbeatAgent()
28 {
29 super(NAME);
30 }
31
32 public long getFrequency()
33 {
34 return frequency;
35 }
36
37 public void setFrequency(long frequency)
38 {
39 this.frequency = frequency;
40 }
41
42 public void initialise() throws InitialisationException
43 {
44
45 }
46
47 public void start() throws MuleException
48 {
49 try
50 {
51 muleContext.getWorkManager().scheduleWork(new Heartbeat());
52 }
53 catch (WorkException e)
54 {
55 throw new DefaultMuleException(e);
56 }
57 }
58
59 public void stop() throws MuleException
60 {
61
62 }
63
64 public void dispose()
65 {
66
67 }
68
69 public class Heartbeat implements Work
70 {
71 public void release()
72 {
73
74 }
75
76 @SuppressWarnings("synthetic-access")
77 public void run()
78 {
79 while(true)
80 {
81 muleContext.fireNotification(new HeartbeatNotification(muleContext));
82 try
83 {
84 Thread.sleep(frequency);
85 }
86 catch (InterruptedException e)
87 {
88 Thread.currentThread().interrupt();
89 break;
90 }
91 }
92 }
93 }
94 }