rev version
[lwes-java.git] / src / org / lwes / emitter / AbstractEventEmitter.java
bloba72efa8597dcd0097556d81a053275a42d72e72e
1 package org.lwes.emitter;
2 /**
3 * @author fmaritato
4 */
6 import org.lwes.Event;
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 {
25 try {
26 factory.initialize();
27 lastBeatTime = System.currentTimeMillis();
28 Event e = factory.createEvent("System::Startup", false);
29 emit(e);
31 catch (EventSystemException e) {
32 Log.error(e.getMessage(), e);
36 public void shutdown() throws IOException {
37 try {
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 {
50 eventCount++;
51 totalEventCount++;
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);
58 eventCount = 0;
59 lastBeatTime = time;
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);
69 emit(e.serialize());
72 protected abstract void emit(byte[] bytes) throws IOException;
74 public boolean isEmitHeartbeat() {
75 return emitHeartbeat;
78 public void setEmitHeartbeat(boolean emitHeartbeat) {
79 this.emitHeartbeat = emitHeartbeat;
82 public long getEventCount() {
83 return eventCount;
86 public EventFactory getFactory() {
87 return factory;
90 public void setFactory(EventFactory factory) {
91 this.factory = factory;
94 public long getFrequency() {
95 return frequency;
98 public void setFrequency(long frequency) {
99 this.frequency = frequency;
102 public long getLastBeatTime() {
103 return lastBeatTime;
106 public long getSequence() {
107 return sequence;
110 public long getTotalEventCount() {
111 return totalEventCount;