Skip to content

Commit e1f0bcd

Browse files
committed
spring-projectsGH-3869: Add some minor improvements.
1 parent 1708bdd commit e1f0bcd

File tree

2 files changed

+17
-78
lines changed

2 files changed

+17
-78
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice;
2323
import org.springframework.messaging.Message;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* Provides a key for the context holder.This key could for example be stored in a message header.
@@ -29,11 +30,12 @@
2930
* @since 6.1
3031
*/
3132
public class ContextHolderRequestHandlerAdvice extends AbstractRequestHandlerAdvice {
32-
public Function<Message<?>, Object> keyProvider;
3333

34-
public Consumer<Object> contextSetHook;
34+
public final Function<Message<?>, Object> keyProvider;
3535

36-
public Consumer<Object> contextClearHook;
36+
public final Consumer<Object> contextSetHook;
37+
38+
public final Consumer<Object> contextClearHook;
3739

3840
/**
3941
* Constructor for the ContextHolderRequestHandlerAdvice.
@@ -42,86 +44,27 @@ public class ContextHolderRequestHandlerAdvice extends AbstractRequestHandlerAdv
4244
* @param contextSetHook The context set hook function.
4345
* @param contextClearHook The context clear hook function.
4446
*/
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");
4651
this.keyProvider = keyProvider;
4752
this.contextSetHook = contextSetHook;
4853
this.contextClearHook = contextClearHook;
4954
}
5055

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-
10156
@Override
10257
protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) {
10358
Object result;
10459
final Object key = this.keyProvider.apply(message);
10560
try {
106-
setContext(key);
61+
this.contextSetHook.accept(key);
10762
result = callback.execute();
10863
}
109-
catch (Exception e) {
110-
result = e;
111-
logger.error("Failure setting context for " + message + ": " + e.getMessage());
112-
}
11364
finally {
114-
contextClearHook(key);
65+
this.contextClearHook.accept(key);
11566
}
11667
return result;
11768
}
11869

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-
12770
}

spring-integration-file/src/test/java/org/springframework/integration/file/remote/session/ContextHolderRequestHandlerAdviceTests.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2022 the original author or authors.
2+
* Copyright 2013-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.springframework.integration.annotation.ServiceActivator;
3030
import org.springframework.integration.config.EnableIntegration;
3131
import org.springframework.integration.dsl.MessageChannels;
32+
import org.springframework.integration.test.util.TestUtils;
3233
import org.springframework.messaging.Message;
3334
import org.springframework.messaging.MessageChannel;
3435
import org.springframework.messaging.PollableChannel;
@@ -61,18 +62,16 @@ public class ContextHolderRequestHandlerAdviceTests {
6162
public void testFlow() {
6263
final Map<String, Object> headers = Map.of("test-key", "foo");
6364
config.in().send(new GenericMessage<>("any-payload", headers));
65+
6466
Message<?> received = config.out().receive(0);
6567
assertThat(received).isNotNull();
66-
assertThat(Config.testSessionDuringHandling).isNotNull().isEqualTo(foo.getSession());
67-
assertThat(config.dsf().getSession()).isEqualTo(bar.getSession());
68+
assertThat(TestUtils.getPropertyValue(config.dsf(), "threadKey", ThreadLocal.class).get()).isNull();
6869
}
6970

7071
@Configuration
7172
@EnableIntegration
7273
public static class Config {
7374

74-
public static Session<String> testSessionDuringHandling = null;
75-
7675
@Bean
7776
MessageChannel in() {
7877
return MessageChannels.direct("in").get();
@@ -102,10 +101,7 @@ TestSessionFactory bar() {
102101
ContextHolderRequestHandlerAdvice advice() {
103102
final DelegatingSessionFactory<String> dsf = dsf();
104103
final Function<Message<?>, Object> keyProviderFn = m -> m.getHeaders().get("test-key");
105-
final Consumer<Object> contextSetHookFn = key -> {
106-
dsf.setThreadKey(key);
107-
testSessionDuringHandling = dsf.getSession();
108-
};
104+
final Consumer<Object> contextSetHookFn = dsf::setThreadKey;
109105
final Consumer<Object> contextClearHookFn = key -> dsf.clearThreadKey();
110106
return new ContextHolderRequestHandlerAdvice(keyProviderFn, contextSetHookFn, contextClearHookFn);
111107
}

0 commit comments

Comments
 (0)