1 package org
.lwes
.emitter
;
7 import org
.lwes
.EventFactory
;
8 import org
.lwes
.EventSystemException
;
9 import org
.lwes
.util
.Log
;
11 import java
.io
.IOException
;
13 public abstract class AbstractEventEmitter
implements EventEmitter
{
15 private EventFactory factory
= new EventFactory();
17 private boolean emitHeartbeat
= false;
18 private long eventCount
= 0;
19 private long totalEventCount
= 0;
20 private long frequency
= 60000;
21 private long lastBeatTime
= 0;
22 private long sequence
= 0;
24 public void initialize() throws IOException
{
27 lastBeatTime
= System
.currentTimeMillis();
28 Event e
= factory
.createEvent("System::Startup", false);
31 catch (EventSystemException e
) {
32 Log
.error(e
.getMessage(), e
);
36 public void shutdown() throws IOException
{
38 Event e
= factory
.createEvent("System::Shutdown", false);
39 long time
= System
.currentTimeMillis();
40 long freqThisPeriod
= time
- lastBeatTime
;
41 sendEventWithStatistics(e
, freqThisPeriod
);
43 catch (EventSystemException e
) {
44 Log
.error(e
.getMessage(), e
);
48 public void collectStatistics() throws EventSystemException
, IOException
{
52 long time
= System
.currentTimeMillis();
53 long freqThisPeriod
= time
- lastBeatTime
;
55 if (emitHeartbeat
&& (freqThisPeriod
>= frequency
)) {
56 Event e
= factory
.createEvent("System::Heartbeat", false);
57 sendEventWithStatistics(e
, freqThisPeriod
);
63 public void sendEventWithStatistics(Event e
, long freq
)
64 throws EventSystemException
, IOException
{
65 e
.setInt64("freq", freq
);
66 e
.setInt64("seq", ++sequence
);
67 e
.setInt64("count", eventCount
);
68 e
.setInt64("total", totalEventCount
);
72 protected abstract void emit(byte[] bytes
) throws IOException
;
74 public boolean isEmitHeartbeat() {
78 public void setEmitHeartbeat(boolean emitHeartbeat
) {
79 this.emitHeartbeat
= emitHeartbeat
;
82 public long getEventCount() {
86 public EventFactory
getFactory() {
90 public void setFactory(EventFactory factory
) {
91 this.factory
= factory
;
94 public long getFrequency() {
98 public void setFrequency(long frequency
) {
99 this.frequency
= frequency
;
102 public long getLastBeatTime() {
106 public long getSequence() {
110 public long getTotalEventCount() {
111 return totalEventCount
;