1
1
/*
2
- * Copyright 2015-2022 the original author or authors.
2
+ * Copyright 2015-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
22
22
import org .springframework .integration .handler .advice .AbstractRequestHandlerAdvice ;
23
23
import org .springframework .messaging .Message ;
24
+ import org .springframework .util .Assert ;
24
25
25
26
/**
26
27
* Provides a key for the context holder.This key could for example be stored in a message header.
29
30
* @since 6.1
30
31
*/
31
32
public class ContextHolderRequestHandlerAdvice extends AbstractRequestHandlerAdvice {
32
- public Function <Message <?>, Object > keyProvider ;
33
33
34
- public Consumer < Object > contextSetHook ;
34
+ public final Function < Message <?>, Object > keyProvider ;
35
35
36
- public Consumer <Object > contextClearHook ;
36
+ public final Consumer <Object > contextSetHook ;
37
+
38
+ public final Consumer <Object > contextClearHook ;
37
39
38
40
/**
39
41
* Constructor for the ContextHolderRequestHandlerAdvice.
@@ -42,86 +44,27 @@ public class ContextHolderRequestHandlerAdvice extends AbstractRequestHandlerAdv
42
44
* @param contextSetHook The context set hook function.
43
45
* @param contextClearHook The context clear hook function.
44
46
*/
45
- public ContextHolderRequestHandlerAdvice (final Function <Message <?>, Object > keyProvider , final Consumer <Object > contextSetHook , final Consumer <Object > contextClearHook ) {
47
+ public ContextHolderRequestHandlerAdvice (Function <Message <?>, Object > keyProvider , Consumer <Object > contextSetHook , Consumer <Object > contextClearHook ) {
48
+ Assert .notNull (keyProvider , "'keyProvider' must not be null" );
49
+ Assert .notNull (contextSetHook , "'contextSetHook' must not be null" );
50
+ Assert .notNull (contextClearHook , "'contextClearHook' must not be null" );
46
51
this .keyProvider = keyProvider ;
47
52
this .contextSetHook = contextSetHook ;
48
53
this .contextClearHook = contextClearHook ;
49
54
}
50
55
51
- /**
52
- * Returns the key provider function.
53
- * @return The key provider function.
54
- *
55
- */
56
- public Function <Message <?>, Object > getKeyProvider () {
57
- return this .keyProvider ;
58
- }
59
-
60
- /**
61
- * Sets the key provider function.
62
- * @param keyProvider the key provider.
63
- */
64
- public void setKeyProvider (Function <Message <?>, Object > keyProvider ) {
65
- this .keyProvider = keyProvider ;
66
- }
67
-
68
- /**
69
- * Returns the context set hook function.
70
- * @return The context set hook function.
71
- *
72
- */
73
- public Consumer <Object > getContextSetHook () {
74
- return this .contextSetHook ;
75
- }
76
-
77
- /**
78
- * Sets the context set hook function.
79
- * @param contextSetHook the context set hook function.
80
- */
81
- public void setContextSetHook (Consumer <Object > contextSetHook ) {
82
- this .contextSetHook = contextSetHook ;
83
- }
84
-
85
- /**
86
- * Returns the context clear hook function.
87
- * @return The context clear hook function.
88
- */
89
- public Consumer <Object > getContextClearHook () {
90
- return this .contextClearHook ;
91
- }
92
-
93
- /**
94
- * Sets the context clear hook function.
95
- * @param contextClearHook the context clear hook function.
96
- */
97
- public void setContextClearHook (Consumer <Object > contextClearHook ) {
98
- this .contextClearHook = contextClearHook ;
99
- }
100
-
101
56
@ Override
102
57
protected Object doInvoke (ExecutionCallback callback , Object target , Message <?> message ) {
103
58
Object result ;
104
59
final Object key = this .keyProvider .apply (message );
105
60
try {
106
- setContext (key );
61
+ this . contextSetHook . accept (key );
107
62
result = callback .execute ();
108
63
}
109
- catch (Exception e ) {
110
- result = e ;
111
- logger .error ("Failure setting context for " + message + ": " + e .getMessage ());
112
- }
113
64
finally {
114
- contextClearHook (key );
65
+ this . contextClearHook . accept (key );
115
66
}
116
67
return result ;
117
68
}
118
69
119
- private void setContext (Object key ) {
120
- this .contextSetHook .accept (key );
121
- }
122
-
123
- private void contextClearHook (Object key ) {
124
- this .contextClearHook .accept (key );
125
- }
126
-
127
70
}
0 commit comments