1 package de.juplo.kafka.payment.transfer;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import de.juplo.kafka.payment.transfer.adapter.*;
6 import de.juplo.kafka.payment.transfer.domain.Transfer;
7 import de.juplo.kafka.payment.transfer.persistence.InMemoryTransferRepository;
8 import de.juplo.kafka.payment.transfer.ports.TransferRepository;
9 import de.juplo.kafka.payment.transfer.ports.TransferService;
10 import lombok.RequiredArgsConstructor;
11 import lombok.extern.slf4j.Slf4j;
12 import org.apache.kafka.clients.admin.AdminClient;
13 import org.apache.kafka.clients.admin.AdminClientConfig;
14 import org.apache.kafka.clients.consumer.ConsumerConfig;
15 import org.apache.kafka.clients.consumer.CooperativeStickyAssignor;
16 import org.apache.kafka.clients.consumer.KafkaConsumer;
17 import org.apache.kafka.clients.producer.KafkaProducer;
18 import org.apache.kafka.clients.producer.ProducerConfig;
19 import org.apache.kafka.common.serialization.StringDeserializer;
20 import org.apache.kafka.common.serialization.StringSerializer;
21 import org.springframework.boot.SpringApplication;
22 import org.springframework.boot.autoconfigure.SpringBootApplication;
23 import org.springframework.boot.context.properties.EnableConfigurationProperties;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.util.Assert;
26 import org.springframework.util.StringUtils;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.time.Clock;
33 import java.util.Optional;
34 import java.util.Properties;
37 @SpringBootApplication
38 @EnableConfigurationProperties(TransferServiceProperties.class)
40 public class TransferServiceApplication
42 @Bean(destroyMethod = "close")
43 AdminClient adminClient(TransferServiceProperties properties)
45 Assert.hasText(properties.getBootstrapServers(), "juplo.transfer.bootstrap-servers must be set");
47 Properties props = new Properties();
48 props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServers());
50 return AdminClient.create(props);
53 @Bean(destroyMethod = "close")
54 KafkaProducer<String, String> producer(TransferServiceProperties properties)
56 Assert.hasText(properties.getBootstrapServers(), "juplo.transfer.bootstrap-servers must be set");
57 Assert.hasText(properties.getTopic(), "juplo.transfer.topic must be set");
58 Assert.notNull(properties.getNumPartitions(), "juplo.transfer.num-partitions must be set");
60 Properties props = new Properties();
61 props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServers());
62 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
63 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
64 props.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, TransferPartitioner.class);
65 props.put(TransferPartitioner.TOPIC, properties.getTopic());
66 props.put(TransferPartitioner.NUM_PARTITIONS, properties.getNumPartitions());
68 return new KafkaProducer<>(props);
72 KafkaConsumer<String, String> consumer(TransferServiceProperties properties)
74 Assert.hasText(properties.getBootstrapServers(), "juplo.transfer.bootstrap-servers must be set");
75 Assert.hasText(properties.getGroupId(), "juplo.transfer.group-id must be set");
76 Assert.hasText(properties.getGroupInstanceId(), "juplo.transfer.group-instance-id must be set");
78 Properties props = new Properties();
79 props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServers());
80 props.put(ConsumerConfig.GROUP_ID_CONFIG, properties.getGroupId());
81 props.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, properties.getGroupInstanceId());
82 props.put(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, CooperativeStickyAssignor.class.getCanonicalName());
83 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
84 props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
85 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
86 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
88 return new KafkaConsumer<>(props);
91 @Bean(destroyMethod = "shutdown")
92 TransferConsumer transferConsumer(
93 TransferServiceProperties properties,
94 KafkaConsumer<String, String> consumer,
95 AdminClient adminClient,
96 TransferRepository repository,
97 LocalStateStoreSettings localStateStoreSettings,
99 TransferService productionTransferService,
100 TransferService restoreTransferService)
103 new TransferConsumer(
104 properties.getTopic(),
105 properties.getNumPartitions(),
106 properties.getInstanceIdUriMapping(),
110 Clock.systemDefaultZone(),
111 localStateStoreSettings.interval,
113 new TransferConsumer.ConsumerUseCases() {
115 public void create(Long id, Long payer, Long payee, Integer amount)
117 productionTransferService.create(id, payer, payee, amount);
121 public Optional<Transfer> get(Long id)
123 return productionTransferService.get(id);
127 public void handleStateChange(Long id, Transfer.State state)
129 productionTransferService.handleStateChange(id, state);
132 new TransferConsumer.ConsumerUseCases() {
134 public void create(Long id, Long payer, Long payee, Integer amount)
136 restoreTransferService.create(id, payer, payee, amount);
140 public Optional<Transfer> get(Long id)
142 return restoreTransferService.get(id);
146 public void handleStateChange(Long id, Transfer.State state)
148 restoreTransferService.handleStateChange(id, state);
154 KafkaMessagingService kafkaMessagingService(
155 KafkaProducer<String, String> producer,
157 TransferServiceProperties properties)
159 return new KafkaMessagingService(producer, mapper, properties.getTopic());
162 @RequiredArgsConstructor
163 static class LocalStateStoreSettings
165 final Optional<File> file;
170 LocalStateStoreSettings localStateStoreSettings(TransferServiceProperties properties)
172 if (properties.getStateStoreInterval() < 1)
174 log.info("juplo.transfer.state-store-interval is < 1: local storage of state is deactivated");
175 return new LocalStateStoreSettings(Optional.empty(), 0);
178 if (!StringUtils.hasText(properties.getLocalStateStorePath()))
180 log.info("juplo.transfer.local-state-store-path is not set: local storage of state is deactivated!");
181 return new LocalStateStoreSettings(Optional.empty(), 0);
184 Path path = Path.of(properties.getLocalStateStorePath());
185 log.info("using {} as local state store", path.toAbsolutePath());
187 if (Files.notExists(path))
191 Files.createFile(path);
193 catch (IOException e)
195 throw new IllegalArgumentException("Could not create local state store: " + path.toAbsolutePath());
199 if (!(Files.isReadable(path) && Files.isWritable(path)))
201 throw new IllegalArgumentException("No R/W-access on local state store: " + path.toAbsolutePath());
204 return new LocalStateStoreSettings(Optional.of(path.toFile()), properties.getStateStoreInterval());
208 InMemoryTransferRepository inMemoryTransferRepository(
209 LocalStateStoreSettings localStateStoreSettings,
210 TransferServiceProperties properties,
213 return new InMemoryTransferRepository(localStateStoreSettings.file, properties.getNumPartitions(), mapper);
217 TransferService productionTransferService(
218 TransferRepository repository,
219 KafkaMessagingService kafkaMessagingService)
221 return new TransferService(repository, kafkaMessagingService);
225 TransferService restoreTransferService(
226 TransferRepository repository,
227 NoOpMessageService noOpMessageService)
229 return new TransferService(repository, noOpMessageService);
233 TransferController transferController(
234 TransferService productionTransferService,
235 KafkaMessagingService kafkaMessagingService,
236 TransferConsumer transferConsumer)
238 return new TransferController(productionTransferService, kafkaMessagingService, transferConsumer);
242 public static void main(String[] args)
244 SpringApplication.run(TransferServiceApplication.class, args);