44
44
import org .springframework .statemachine .annotation .OnTransitionEnd ;
45
45
import org .springframework .statemachine .annotation .OnTransitionStart ;
46
46
import org .springframework .statemachine .annotation .WithStateMachine ;
47
+ import org .springframework .statemachine .config .configuration .StateMachineHandlerApplicationListener ;
47
48
import org .springframework .statemachine .state .State ;
48
49
import org .springframework .statemachine .support .StateMachineUtils ;
49
50
import org .springframework .util .Assert ;
@@ -64,6 +65,8 @@ public class StateMachineHandlerCallHelper<S, E> implements InitializingBean, Be
64
65
private final Log log = LogFactory .getLog (StateMachineHandlerCallHelper .class );
65
66
private final Map <String , List <CacheEntry >> cache = new HashMap <>();
66
67
private ListableBeanFactory beanFactory ;
68
+ private StateMachineHandlerApplicationListener stateMachineHandlerApplicationListener ;
69
+ private long last = Long .MIN_VALUE ;
67
70
68
71
@ SuppressWarnings ("unchecked" )
69
72
@ Override
@@ -72,6 +75,10 @@ public void afterPropertiesSet() throws Exception {
72
75
log .info ("Beanfactory is not instance of ListableBeanFactory, was " + beanFactory + " thus Disabling handlers." );
73
76
return ;
74
77
}
78
+ if (beanFactory .containsBean (StateMachineHandlerApplicationListener .BEAN_NAME )) {
79
+ this .stateMachineHandlerApplicationListener = beanFactory .getBean (StateMachineHandlerApplicationListener .BEAN_NAME ,
80
+ StateMachineHandlerApplicationListener .class );
81
+ }
75
82
for (StateMachineHandler <? extends Annotation , S , E > handler : beanFactory .getBeansOfType (StateMachineHandler .class ).values ()) {
76
83
Annotation annotation = handler .getAnnotation ();
77
84
Annotation metaAnnotation = handler .getMetaAnnotation ();
@@ -98,7 +105,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
98
105
public void callOnStateChanged (String stateMachineId , StateContext <S , E > stateContext ) {
99
106
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
100
107
String cacheKey = OnStateChanged .class .getName () + stateMachineId ;
101
- List <CacheEntry > list = cache . get (cacheKey );
108
+ List <CacheEntry > list = getCacheEntries (cacheKey );
102
109
if (list == null ) {
103
110
return ;
104
111
}
@@ -115,7 +122,7 @@ public void callOnStateChanged(String stateMachineId, StateContext<S, E> stateCo
115
122
public void callOnStateEntry (String stateMachineId , StateContext <S , E > stateContext ) {
116
123
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
117
124
String cacheKey = OnStateEntry .class .getName () + stateMachineId ;
118
- List <CacheEntry > list = cache . get (cacheKey );
125
+ List <CacheEntry > list = getCacheEntries (cacheKey );
119
126
if (list == null ) {
120
127
return ;
121
128
}
@@ -132,7 +139,7 @@ public void callOnStateEntry(String stateMachineId, StateContext<S, E> stateCont
132
139
public void callOnStateExit (String stateMachineId , StateContext <S , E > stateContext ) {
133
140
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
134
141
String cacheKey = OnStateExit .class .getName () + stateMachineId ;
135
- List <CacheEntry > list = cache . get (cacheKey );
142
+ List <CacheEntry > list = getCacheEntries (cacheKey );
136
143
if (list == null ) {
137
144
return ;
138
145
}
@@ -149,7 +156,7 @@ public void callOnStateExit(String stateMachineId, StateContext<S, E> stateConte
149
156
public void callOnEventNotAccepted (String stateMachineId , StateContext <S , E > stateContext ) {
150
157
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
151
158
String cacheKey = OnEventNotAccepted .class .getName () + stateMachineId ;
152
- List <CacheEntry > list = cache . get (cacheKey );
159
+ List <CacheEntry > list = getCacheEntries (cacheKey );
153
160
if (list == null ) {
154
161
return ;
155
162
}
@@ -170,7 +177,7 @@ public void callOnEventNotAccepted(String stateMachineId, StateContext<S, E> sta
170
177
public void callOnTransitionStart (String stateMachineId , StateContext <S , E > stateContext ) {
171
178
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
172
179
String cacheKey = OnTransitionStart .class .getName () + stateMachineId ;
173
- List <CacheEntry > list = cache . get (cacheKey );
180
+ List <CacheEntry > list = getCacheEntries (cacheKey );
174
181
if (list == null ) {
175
182
return ;
176
183
}
@@ -187,7 +194,7 @@ public void callOnTransitionStart(String stateMachineId, StateContext<S, E> stat
187
194
public void callOnTransition (String stateMachineId , StateContext <S , E > stateContext ) {
188
195
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
189
196
String cacheKey = OnTransition .class .getName () + stateMachineId ;
190
- List <CacheEntry > list = cache . get (cacheKey );
197
+ List <CacheEntry > list = getCacheEntries (cacheKey );
191
198
if (list == null ) {
192
199
return ;
193
200
}
@@ -204,7 +211,7 @@ public void callOnTransition(String stateMachineId, StateContext<S, E> stateCont
204
211
public void callOnTransitionEnd (String stateMachineId , StateContext <S , E > stateContext ) {
205
212
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
206
213
String cacheKey = OnTransitionEnd .class .getName () + stateMachineId ;
207
- List <CacheEntry > list = cache . get (cacheKey );
214
+ List <CacheEntry > list = getCacheEntries (cacheKey );
208
215
if (list == null ) {
209
216
return ;
210
217
}
@@ -221,7 +228,7 @@ public void callOnTransitionEnd(String stateMachineId, StateContext<S, E> stateC
221
228
public void callOnStateMachineStart (String stateMachineId , StateContext <S , E > stateContext ) {
222
229
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
223
230
String cacheKey = OnStateMachineStart .class .getName () + stateMachineId ;
224
- List <CacheEntry > list = cache . get (cacheKey );
231
+ List <CacheEntry > list = getCacheEntries (cacheKey );
225
232
if (list == null ) {
226
233
return ;
227
234
}
@@ -234,7 +241,7 @@ public void callOnStateMachineStart(String stateMachineId, StateContext<S, E> st
234
241
public void callOnStateMachineStop (String stateMachineId , StateContext <S , E > stateContext ) {
235
242
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
236
243
String cacheKey = OnStateMachineStop .class .getName () + stateMachineId ;
237
- List <CacheEntry > list = cache . get (cacheKey );
244
+ List <CacheEntry > list = getCacheEntries (cacheKey );
238
245
if (list == null ) {
239
246
return ;
240
247
}
@@ -247,7 +254,7 @@ public void callOnStateMachineStop(String stateMachineId, StateContext<S, E> sta
247
254
public void callOnStateMachineError (String stateMachineId , StateContext <S , E > stateContext ) {
248
255
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
249
256
String cacheKey = OnStateMachineError .class .getName () + stateMachineId ;
250
- List <CacheEntry > list = cache . get (cacheKey );
257
+ List <CacheEntry > list = getCacheEntries (cacheKey );
251
258
if (list == null ) {
252
259
return ;
253
260
}
@@ -260,7 +267,7 @@ public void callOnStateMachineError(String stateMachineId, StateContext<S, E> st
260
267
public void callOnExtendedStateChanged (String stateMachineId , Object key , Object value , StateContext <S , E > stateContext ) {
261
268
List <StateMachineHandler <? extends Annotation , S , E >> handlersList = new ArrayList <StateMachineHandler <? extends Annotation , S , E >>();
262
269
String cacheKey = OnExtendedStateChanged .class .getName () + stateMachineId ;
263
- List <CacheEntry > list = cache . get (cacheKey );
270
+ List <CacheEntry > list = getCacheEntries (cacheKey );
264
271
if (list == null ) {
265
272
return ;
266
273
}
@@ -272,6 +279,24 @@ public void callOnExtendedStateChanged(String stateMachineId, Object key, Object
272
279
getStateMachineHandlerResults (handlersList , stateContext );
273
280
}
274
281
282
+ private synchronized List <CacheEntry > getCacheEntries (String cacheKey ) {
283
+ if (stateMachineHandlerApplicationListener != null ) {
284
+ Long l = stateMachineHandlerApplicationListener .getLastRefreshTime ();
285
+ if (l != null && l < System .currentTimeMillis () ) {
286
+ if (last != l ) {
287
+ cache .clear ();
288
+ try {
289
+ afterPropertiesSet ();
290
+ } catch (Exception e ) {
291
+ log .error ("Unable to update handler cache" , e );
292
+ }
293
+ last = l ;
294
+ }
295
+ }
296
+ }
297
+ return cache .get (cacheKey );
298
+ }
299
+
275
300
private boolean annotationHandlerVariableMatch (Annotation annotation , Object key ) {
276
301
boolean handle = false ;
277
302
Map <String , Object > annotationAttributes = AnnotationUtils .getAnnotationAttributes (annotation );
0 commit comments