-
Notifications
You must be signed in to change notification settings - Fork 8
/
Clock.java
99 lines (87 loc) · 2.83 KB
/
Clock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (C) 2019 2024 gregory higgins.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
package com.fluxtion.runtime.time;
import com.fluxtion.runtime.annotations.Initialise;
import com.fluxtion.runtime.annotations.OnEventHandler;
import com.fluxtion.runtime.audit.Auditor;
import com.fluxtion.runtime.event.Event;
import com.fluxtion.runtime.time.ClockStrategy.ClockStrategyEvent;
/**
* A clock instance in a static event processor, use the @Inject annotation to
* ensure the same of instance of the clock is used for all nodes. Clock
* provides time query functionality for the processor as follows:
*
* <ul>
* <li>WallClock - current time UTC milliseconds</li>
* <li>ProcessTime - the time the event was received for processing</li>
* <li>EventTime - the time the event was created</li>
* </ul>
*
* @author 2024 gregory higgins.
*/
public class Clock implements Auditor, Auditor.FirstAfterEvent {
private transient long eventTime;
private transient long processTime;
private ClockStrategy wallClock;
public static final Clock DEFAULT_CLOCK = new Clock();
@Override
public void eventReceived(Event event) {
processTime = getWallClockTime();
eventTime = event.getEventTime();
}
@Override
public void eventReceived(Object event) {
processTime = getWallClockTime();
eventTime = processTime;
}
@Override
public void nodeRegistered(Object node, String nodeName) {/*NoOp*/
}
@OnEventHandler(propagate = false)
public void setClockStrategy(ClockStrategyEvent event) {
this.wallClock = event.getStrategy();
}
/**
* The time the last event was received by the processor
*
* @return time the last event was received for processing
*/
public long getProcessTime() {
return processTime;
}
/**
* The time the latest event was created
*
* @return time the latest event was created
*/
public long getEventTime() {
return eventTime;
}
/**
* Current wallclock time in milliseconds UTC
*
* @return time in milliseconds UTC
*/
public long getWallClockTime() {
return wallClock.getWallClockTime();
}
@Initialise
@Override
public void init() {
wallClock = System::currentTimeMillis;
}
}