-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-cloud stream config removed and used simple spring-kafka for d…
…ata-generator
- Loading branch information
Showing
14 changed files
with
164 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Assemble" type="GradleRunConfiguration" factoryName="Gradle" folderName="00-build"> | ||
<ExternalSystemSettings> | ||
<option name="executionName" /> | ||
<option name="externalProjectPath" value="$PROJECT_DIR$" /> | ||
<option name="externalSystemIdString" value="GRADLE" /> | ||
<option name="scriptParameters" value="" /> | ||
<option name="taskDescriptions"> | ||
<list /> | ||
</option> | ||
<option name="taskNames"> | ||
<list> | ||
<option value="assemble" /> | ||
</list> | ||
</option> | ||
<option name="vmOptions" /> | ||
</ExternalSystemSettings> | ||
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess> | ||
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess> | ||
<DebugAllEnabled>false</DebugAllEnabled> | ||
<RunAsTest>false</RunAsTest> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
This file contains 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
28 changes: 28 additions & 0 deletions
28
...c/integrationTest/java/com/maemresen/k8s/workshop/data/generator/DataGeneratorTaskIT.java
This file contains 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,28 @@ | ||
package com.maemresen.k8s.workshop.data.generator; | ||
|
||
import com.maemresen.k8s.workshop.data.generator.task.DataGeneratorTask; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.testcontainers.containers.KafkaContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@Testcontainers | ||
@SpringBootTest | ||
class DataGeneratorTaskIT { | ||
|
||
@Autowired | ||
private DataGeneratorTask dataGeneratorTask; | ||
|
||
@Container | ||
private KafkaContainer kafkaContainer = | ||
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.6.1")); | ||
|
||
|
||
@Test | ||
void test1() { | ||
dataGeneratorTask.produceSensorData(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...r/src/integrationTest/java/com/maemresen/k8s/workshop/data/generator/KafkaTestConfig.java
This file contains 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,7 @@ | ||
package com.maemresen.k8s.workshop.data.generator; | ||
|
||
import org.springframework.boot.test.context.TestConfiguration; | ||
|
||
@TestConfiguration | ||
public class KafkaTestConfig { | ||
} |
26 changes: 8 additions & 18 deletions
26
...src/main/java/com/maemresen/k8s/workshop/data/generator/messaging/SensorDataProducer.java
This file contains 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 |
---|---|---|
@@ -1,26 +1,16 @@ | ||
package com.maemresen.k8s.workshop.data.generator.messaging; | ||
|
||
import com.maemresen.k8s.workshop.messaging.starter.BaseMessageProducer; | ||
import com.maemresen.k8s.workshop.messaging.starter.BaseProducer; | ||
import com.maemresen.k8s.workshop.messaging.starter.MessagingProps; | ||
import com.maemresen.k8s.workshop.messaging.starter.Topic; | ||
import com.maemresen.lib.message.dto.SensorData; | ||
import org.springframework.cloud.function.cloudevent.CloudEventMessageBuilder; | ||
import org.springframework.cloud.stream.function.StreamBridge; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Component | ||
public class SensorDataProducer extends BaseMessageProducer<SensorData> { | ||
@Service | ||
public class SensorDataProducer extends BaseProducer<String, SensorData> { | ||
|
||
protected SensorDataProducer( | ||
final MessagingProps messagingProps, | ||
final StreamBridge streamBridge) { | ||
super(messagingProps, streamBridge, Topic.SENSOR_DATA); | ||
} | ||
|
||
@Override | ||
protected CloudEventMessageBuilder<SensorData> messageCustomizer( | ||
final SensorData payload, | ||
final CloudEventMessageBuilder<SensorData> builder) { | ||
return builder.setHeader(topicProps.partitionKey(), payload.getDevice().getLocation()); | ||
} | ||
public SensorDataProducer(final MessagingProps messagingProps, final KafkaTemplate<String, SensorData> kafkaTemplate) { | ||
super(Topic.SENSOR_DATA, messagingProps, kafkaTemplate); | ||
} | ||
} |
This file contains 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 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 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
20 changes: 20 additions & 0 deletions
20
...boot-starter/src/main/java/com/maemresen/k8s/workshop/messaging/starter/BaseConsumer.java
This file contains 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,20 @@ | ||
package com.maemresen.k8s.workshop.messaging.starter; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.kafka.core.ConsumerFactory; | ||
|
||
@RequiredArgsConstructor | ||
public abstract class BaseConsumer<K, V> { | ||
|
||
private final String topic; | ||
private final ConsumerFactory<K, V> consumerFactory; | ||
|
||
@KafkaListener(topics = "#{@baseConsumer.topic}", groupId = "${spring.kafka.consumer.group-id}") | ||
public void consume(final ConsumerRecord<K, V> consumerRecord) { | ||
processMessage(consumerRecord); | ||
} | ||
|
||
protected abstract void processMessage(final ConsumerRecord<K, V> consumerRecord); | ||
} |
35 changes: 0 additions & 35 deletions
35
...arter/src/main/java/com/maemresen/k8s/workshop/messaging/starter/BaseMessageProducer.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
...boot-starter/src/main/java/com/maemresen/k8s/workshop/messaging/starter/BaseProducer.java
This file contains 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,25 @@ | ||
package com.maemresen.k8s.workshop.messaging.starter; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BaseProducer<K, V> { | ||
private final Topic topic; | ||
private final MessagingProps messagingProps; | ||
private final KafkaTemplate<K, V> kafkaTemplate; | ||
|
||
public void sendMessage(final K key, final V value) { | ||
try { | ||
final var topicProps = messagingProps.getTopic(topic); | ||
final var topicName = topicProps.name(); | ||
kafkaTemplate.send(topicName, key, value); | ||
log.info("Producing message to topic {} with key {} and value {}", topicName, key, value); | ||
} catch (Exception e) { | ||
log.error("Failed to send message to topic {}", topic, e); | ||
throw new MessagingException("Failed to send message to topic " + topic, e); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...-boot-starter/src/main/java/com/maemresen/k8s/workshop/messaging/starter/KafkaConfig.java
This file contains 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,10 @@ | ||
package com.maemresen.k8s.workshop.messaging.starter; | ||
|
||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.kafka.annotation.EnableKafka; | ||
|
||
@EnableKafka | ||
@Configuration | ||
public class KafkaConfig { | ||
} |
8 changes: 8 additions & 0 deletions
8
...tarter/src/main/java/com/maemresen/k8s/workshop/messaging/starter/MessagingException.java
This file contains 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,8 @@ | ||
package com.maemresen.k8s.workshop.messaging.starter; | ||
|
||
public class MessagingException extends RuntimeException { | ||
|
||
public MessagingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains 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