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