Skip to content

Commit 445fbcb

Browse files
authored
GH-8108: Add DSL for Redis channel adapters
Related to: #8108 * Doc: Add inbound/outbound changes in whats-new.adoc and respective parts in redis.adoc Add topicExpression(String) and topicFunction(Function) in RedisOutboundChannelAdapterSpec. * rewrite redis dsl changes in whats-new.adoc Signed-off-by: Jiandong Ma <jiandong.ma.cn@gmail.com>
1 parent e5b8994 commit 445fbcb

7 files changed

Lines changed: 517 additions & 2 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.redis.dsl;
18+
19+
import org.springframework.data.redis.connection.RedisConnectionFactory;
20+
21+
/**
22+
* Factory class for Redis components.
23+
*
24+
* @author Jiandong Ma
25+
*
26+
* @since 7.1
27+
*/
28+
public final class Redis {
29+
30+
/**
31+
* The factory to produce a {@link RedisInboundChannelAdapterSpec}.
32+
* @param connectionFactory the {@link RedisConnectionFactory} to build on
33+
* @return the {@link RedisInboundChannelAdapterSpec} instance
34+
*/
35+
public static RedisInboundChannelAdapterSpec inboundChannelAdapter(RedisConnectionFactory connectionFactory) {
36+
return new RedisInboundChannelAdapterSpec(connectionFactory);
37+
}
38+
39+
/**
40+
* The factory to produce a {@link RedisOutboundChannelAdapterSpec}.
41+
* @param connectionFactory the {@link RedisConnectionFactory} to build on
42+
* @return the {@link RedisOutboundChannelAdapterSpec} instance
43+
*/
44+
public static RedisOutboundChannelAdapterSpec outboundChannelAdapter(RedisConnectionFactory connectionFactory) {
45+
return new RedisOutboundChannelAdapterSpec(connectionFactory);
46+
}
47+
48+
private Redis() {
49+
}
50+
51+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.redis.dsl;
18+
19+
import java.util.concurrent.Executor;
20+
21+
import org.springframework.data.redis.connection.RedisConnectionFactory;
22+
import org.springframework.data.redis.serializer.RedisSerializer;
23+
import org.springframework.integration.dsl.MessageProducerSpec;
24+
import org.springframework.integration.redis.inbound.RedisInboundChannelAdapter;
25+
import org.springframework.messaging.converter.MessageConverter;
26+
27+
/**
28+
* A {@link MessageProducerSpec} for a {@link RedisInboundChannelAdapterSpec}.
29+
*
30+
* @author Jiandong Ma
31+
*
32+
* @since 7.1
33+
*/
34+
public class RedisInboundChannelAdapterSpec extends MessageProducerSpec<RedisInboundChannelAdapterSpec, RedisInboundChannelAdapter> {
35+
36+
protected RedisInboundChannelAdapterSpec(RedisConnectionFactory connectionFactory) {
37+
this.target = new RedisInboundChannelAdapter(connectionFactory);
38+
}
39+
40+
/**
41+
* @param serializer the serializer
42+
* @return the spec
43+
* @see RedisInboundChannelAdapter#setSerializer(RedisSerializer)
44+
*/
45+
public RedisInboundChannelAdapterSpec serializer(RedisSerializer<?> serializer) {
46+
this.target.setSerializer(serializer);
47+
return this;
48+
}
49+
50+
/**
51+
* @param topics the topics
52+
* @return the spec
53+
* @see RedisInboundChannelAdapter#setTopics(String...)
54+
*/
55+
public RedisInboundChannelAdapterSpec topics(String... topics) {
56+
this.target.setTopics(topics);
57+
return this;
58+
}
59+
60+
/**
61+
* @param topicPatterns the topicPatterns
62+
* @return the spec
63+
* @see RedisInboundChannelAdapter#setTopicPatterns(String...)
64+
*/
65+
public RedisInboundChannelAdapterSpec topicPatterns(String... topicPatterns) {
66+
this.target.setTopicPatterns(topicPatterns);
67+
return this;
68+
}
69+
70+
/**
71+
* @param messageConverter the messageConverter
72+
* @return the spec
73+
* @see RedisInboundChannelAdapter#setMessageConverter(MessageConverter)
74+
*/
75+
public RedisInboundChannelAdapterSpec messageConverter(MessageConverter messageConverter) {
76+
this.target.setMessageConverter(messageConverter);
77+
return this;
78+
}
79+
80+
/**
81+
* @param taskExecutor the taskExecutor
82+
* @return the spec
83+
* @see RedisInboundChannelAdapter#setTaskExecutor(Executor)
84+
*/
85+
public RedisInboundChannelAdapterSpec taskExecutor(Executor taskExecutor) {
86+
this.target.setTaskExecutor(taskExecutor);
87+
return this;
88+
}
89+
90+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2026-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.redis.dsl;
18+
19+
import java.util.function.Function;
20+
21+
import org.springframework.data.redis.connection.RedisConnectionFactory;
22+
import org.springframework.data.redis.serializer.RedisSerializer;
23+
import org.springframework.expression.Expression;
24+
import org.springframework.integration.dsl.MessageHandlerSpec;
25+
import org.springframework.integration.expression.FunctionExpression;
26+
import org.springframework.integration.redis.outbound.RedisPublishingMessageHandler;
27+
import org.springframework.messaging.Message;
28+
import org.springframework.messaging.converter.MessageConverter;
29+
30+
/**
31+
* A {@link MessageHandlerSpec} for a {@link RedisOutboundChannelAdapterSpec}.
32+
*
33+
* @author Jiandong Ma
34+
*
35+
* @since 7.1
36+
*/
37+
public class RedisOutboundChannelAdapterSpec extends MessageHandlerSpec<RedisOutboundChannelAdapterSpec, RedisPublishingMessageHandler> {
38+
39+
protected RedisOutboundChannelAdapterSpec(RedisConnectionFactory connectionFactory) {
40+
this.target = new RedisPublishingMessageHandler(connectionFactory);
41+
}
42+
43+
/**
44+
* @param serializer the serializer
45+
* @return the spec
46+
* @see RedisPublishingMessageHandler#setSerializer(RedisSerializer)
47+
*/
48+
public RedisOutboundChannelAdapterSpec serializer(RedisSerializer<?> serializer) {
49+
this.target.setSerializer(serializer);
50+
return this;
51+
}
52+
53+
/**
54+
* @param messageConverter the messageConverter
55+
* @return the spec
56+
* @see RedisPublishingMessageHandler#setMessageConverter(MessageConverter)
57+
*/
58+
public RedisOutboundChannelAdapterSpec messageConverter(MessageConverter messageConverter) {
59+
this.target.setMessageConverter(messageConverter);
60+
return this;
61+
}
62+
63+
/**
64+
* @param topic the topic
65+
* @return the spec
66+
* @see RedisPublishingMessageHandler#setTopic(String)
67+
*/
68+
public RedisOutboundChannelAdapterSpec topic(String topic) {
69+
this.target.setTopic(topic);
70+
return this;
71+
}
72+
73+
/**
74+
* @param topicExpression the topicExpression
75+
* @return the spec
76+
* @see RedisPublishingMessageHandler#setTopicExpression(Expression)
77+
*/
78+
public RedisOutboundChannelAdapterSpec topicExpression(Expression topicExpression) {
79+
this.target.setTopicExpression(topicExpression);
80+
return this;
81+
}
82+
83+
/**
84+
* Configure a SpEL expression to determine the topic.
85+
* @param topicExpression the topicExpression
86+
* @return the spec
87+
* @see RedisPublishingMessageHandler#setTopicExpression(Expression)
88+
*/
89+
public RedisOutboundChannelAdapterSpec topicExpression(String topicExpression) {
90+
this.target.setTopicExpression(PARSER.parseExpression(topicExpression));
91+
return this;
92+
}
93+
94+
/**
95+
* Configure a {@link Function} to determine the topic.
96+
* @param topicFunction the topicFunction
97+
* @param <P> the payload type.
98+
* @return the spec
99+
*/
100+
public <P> RedisOutboundChannelAdapterSpec topicFunction(Function<Message<P>, String> topicFunction) {
101+
this.target.setTopicExpression(new FunctionExpression<>(topicFunction));
102+
return this;
103+
}
104+
105+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Java DSL classes for Redis module.
3+
*/
4+
@org.jspecify.annotations.NullMarked
5+
package org.springframework.integration.redis.dsl;

0 commit comments

Comments
 (0)