This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Redis consumer #31
Open
sobychacko
wants to merge
2
commits into
master
Choose a base branch
from
redis-consumer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Redis consumer #31
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>redis-consumer</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
<name>redis-consumer</name> | ||
<description>redis consumer</description> | ||
|
||
<parent> | ||
<groupId>io.pivotal.java.function</groupId> | ||
<artifactId>spring-functions-parent</artifactId> | ||
<version>1.0.0.BUILD-SNAPSHOT</version> | ||
<relativePath>../../spring-functions-parent</relativePath> | ||
</parent> | ||
|
||
<properties> | ||
<spring-cloud-starters.version>2.2.0.RELEASE</spring-cloud-starters.version> | ||
<embedded-redis.version>1.48</embedded-redis.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.integration</groupId> | ||
<artifactId>spring-integration-redis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-validation</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.validation</groupId> | ||
<artifactId>validation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.playtika.testcontainers</groupId> | ||
<artifactId>embedded-redis</artifactId> | ||
<version>${embedded-redis.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter</artifactId> | ||
<version>${spring-cloud-starters.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
73 changes: 73 additions & 0 deletions
73
...mer/src/main/java/io/pivotal/java/function/redis/consumer/RedisConsumerConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2015-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @author Eric Bottard | ||
* @author Mark Pollack | ||
* @author Gary Russell | ||
* @author Soby Chacko | ||
*/ | ||
package io.pivotal.java.function.redis.consumer; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.expression.ExpressionParser; | ||
import org.springframework.expression.common.LiteralExpression; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.integration.redis.outbound.RedisPublishingMessageHandler; | ||
import org.springframework.integration.redis.outbound.RedisQueueOutboundChannelAdapter; | ||
import org.springframework.integration.redis.outbound.RedisStoreWritingMessageHandler; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageHandler; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(RedisConsumerProperties.class) | ||
public class RedisConsumerConfiguration { | ||
|
||
private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(); | ||
|
||
@Bean | ||
public Consumer<Message<?>> redisConsumer(MessageHandler redisConsumerMessageHandler) { | ||
return redisConsumerMessageHandler::handleMessage; | ||
} | ||
|
||
@Bean | ||
public MessageHandler redisConsumerMessageHandler(RedisConnectionFactory redisConnectionFactory, | ||
RedisConsumerProperties redisConsumerProperties) { | ||
if (redisConsumerProperties.isKeyPresent()) { | ||
RedisStoreWritingMessageHandler redisStoreWritingMessageHandler = new RedisStoreWritingMessageHandler( | ||
redisConnectionFactory); | ||
redisStoreWritingMessageHandler.setKeyExpression( | ||
new LiteralExpression(redisConsumerProperties.keyExpression())); | ||
return redisStoreWritingMessageHandler; | ||
} | ||
else if (redisConsumerProperties.isQueuePresent()) { | ||
return new RedisQueueOutboundChannelAdapter(redisConsumerProperties.queueExpression(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have missed to parse an expression here. |
||
redisConnectionFactory); | ||
} | ||
else { // must be topic | ||
RedisPublishingMessageHandler redisPublishingMessageHandler = new RedisPublishingMessageHandler( | ||
redisConnectionFactory); | ||
redisPublishingMessageHandler.setTopicExpression( | ||
EXPRESSION_PARSER.parseExpression(redisConsumerProperties.topicExpression())); | ||
return redisPublishingMessageHandler; | ||
} | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
...nsumer/src/main/java/io/pivotal/java/function/redis/consumer/RedisConsumerProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2015-2020 the original author or authors. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.pivotal.java.function.redis.consumer; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import javax.validation.constraints.AssertTrue; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.expression.common.LiteralExpression; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
/** | ||
* Used to configure those Redis Sink module options that are not related to connecting to Redis. | ||
* | ||
* @author Eric Bottard | ||
* @author Mark Pollack | ||
* @author Artem Bilan | ||
* @author Soby Chacko | ||
*/ | ||
@ConfigurationProperties("redis.consumer") | ||
@Validated | ||
public class RedisConsumerProperties { | ||
|
||
/** | ||
* A SpEL expression to use for topic. | ||
*/ | ||
private String topicExpression; | ||
|
||
/** | ||
* A SpEL expression to use for queue. | ||
*/ | ||
private String queueExpression; | ||
|
||
/** | ||
* A SpEL expression to use for storing to a key. | ||
*/ | ||
private String keyExpression; | ||
|
||
/** | ||
* A literal key name to use when storing to a key. | ||
*/ | ||
private String key; | ||
|
||
/** | ||
* A literal queue name to use when storing in a queue. | ||
*/ | ||
private String queue; | ||
|
||
/** | ||
* A literal topic name to use when publishing to a topic. | ||
*/ | ||
private String topic; | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public void setQueue(String queue) { | ||
this.queue = queue; | ||
} | ||
|
||
public void setTopic(String topic) { | ||
this.topic = topic; | ||
} | ||
|
||
public String keyExpression() { | ||
return key != null ? new LiteralExpression(key).getExpressionString() : keyExpression; | ||
} | ||
|
||
public void setKeyExpression(String keyExpression) { | ||
this.keyExpression = keyExpression; | ||
} | ||
|
||
public String queueExpression() { | ||
return queue != null ? new LiteralExpression(queue).getExpressionString() : queueExpression; | ||
} | ||
|
||
public void setQueueExpression(String queueExpression) { | ||
this.queueExpression = queueExpression; | ||
} | ||
|
||
public String topicExpression() { | ||
return topic != null ? new LiteralExpression(topic).getExpressionString() : topicExpression; | ||
} | ||
|
||
public void setTopicExpression(String topicExpression) { | ||
this.topicExpression = topicExpression; | ||
} | ||
|
||
boolean isKeyPresent() { | ||
return StringUtils.hasText(key) || keyExpression != null; | ||
} | ||
|
||
boolean isQueuePresent() { | ||
return StringUtils.hasText(queue) || queueExpression != null; | ||
} | ||
|
||
boolean isTopicPresent() { | ||
return StringUtils.hasText(topic) || topicExpression != null; | ||
} | ||
|
||
public String getTopicExpression() { | ||
return topicExpression; | ||
} | ||
|
||
public String getQueueExpression() { | ||
return queueExpression; | ||
} | ||
|
||
public String getKeyExpression() { | ||
return keyExpression; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getQueue() { | ||
return queue; | ||
} | ||
|
||
public String getTopic() { | ||
return topic; | ||
} | ||
|
||
|
||
// The javabean property name is what will be reported in case of violation. Make it meaningful | ||
@AssertTrue(message = "Exactly one of 'queue', 'queueExpression', 'key', 'keyExpression', " | ||
+ "'topic' and 'topicExpression' must be set") | ||
public boolean isMutuallyExclusive() { | ||
Object[] props = new Object[] { queue, queueExpression, key, keyExpression, topic, topicExpression }; | ||
return (props.length - 1) == Collections.frequency(Arrays.asList(props), null); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. It has to be parsed - it is not a plain literal.
Anyway consider a convenient
setKeyExpressionString()
instead.