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 | 0 | public class HeartbeatAgent extends AbstractAgent |
22 | |
{ |
23 | |
public static final String NAME = "Heartbeat"; |
24 | |
|
25 | 0 | private long frequency = 10000; |
26 | |
|
27 | |
public HeartbeatAgent() |
28 | |
{ |
29 | 0 | super(NAME); |
30 | 0 | } |
31 | |
|
32 | |
public long getFrequency() |
33 | |
{ |
34 | 0 | return frequency; |
35 | |
} |
36 | |
|
37 | |
public void setFrequency(long frequency) |
38 | |
{ |
39 | 0 | this.frequency = frequency; |
40 | 0 | } |
41 | |
|
42 | |
public void initialise() throws InitialisationException |
43 | |
{ |
44 | |
|
45 | 0 | } |
46 | |
|
47 | |
public void start() throws MuleException |
48 | |
{ |
49 | |
try |
50 | |
{ |
51 | 0 | muleContext.getWorkManager().scheduleWork(new Heartbeat()); |
52 | |
} |
53 | 0 | catch (WorkException e) |
54 | |
{ |
55 | 0 | throw new DefaultMuleException(e); |
56 | 0 | } |
57 | 0 | } |
58 | |
|
59 | |
public void stop() throws MuleException |
60 | |
{ |
61 | |
|
62 | 0 | } |
63 | |
|
64 | |
public void dispose() |
65 | |
{ |
66 | |
|
67 | 0 | } |
68 | |
|
69 | 0 | public class Heartbeat implements Work |
70 | |
{ |
71 | |
public void release() |
72 | |
{ |
73 | |
|
74 | 0 | } |
75 | |
|
76 | |
@SuppressWarnings("synthetic-access") |
77 | |
public void run() |
78 | |
{ |
79 | |
while(true) |
80 | |
{ |
81 | 0 | muleContext.fireNotification(new HeartbeatNotification(muleContext)); |
82 | |
try |
83 | |
{ |
84 | 0 | Thread.sleep(frequency); |
85 | |
} |
86 | 0 | catch (InterruptedException e) |
87 | |
{ |
88 | 0 | Thread.currentThread().interrupt(); |
89 | 0 | break; |
90 | 0 | } |
91 | |
} |
92 | 0 | } |
93 | |
} |
94 | |
} |