View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * A simple agent that fire {@link org.mule.example.notifications.HeartbeatNotification} events at a given frequency to
19   * notify that the server is alive and well.
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          //No Op
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          //No Op
62      }
63  
64      public void dispose()
65      {
66          //No Op
67      }
68  
69      public class Heartbeat implements Work
70      {
71          public void release()
72          {
73              //No Op
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  }