1 package de.juplo.kafka.payment.transfer;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import de.juplo.kafka.payment.transfer.adapter.KafkaMessagingService;
6 import de.juplo.kafka.payment.transfer.adapter.NoOpMessageService;
7 import de.juplo.kafka.payment.transfer.adapter.TransferConsumer;
8 import de.juplo.kafka.payment.transfer.adapter.TransferController;
9 import de.juplo.kafka.payment.transfer.domain.Transfer;
10 import de.juplo.kafka.payment.transfer.ports.TransferRepository;
11 import de.juplo.kafka.payment.transfer.ports.TransferService;
12 import lombok.extern.slf4j.Slf4j;
13 import org.apache.kafka.clients.consumer.ConsumerConfig;
14 import org.apache.kafka.clients.consumer.KafkaConsumer;
15 import org.apache.kafka.clients.producer.KafkaProducer;
16 import org.apache.kafka.clients.producer.ProducerConfig;
17 import org.apache.kafka.common.serialization.StringDeserializer;
18 import org.apache.kafka.common.serialization.StringSerializer;
19 import org.springframework.boot.SpringApplication;
20 import org.springframework.boot.autoconfigure.SpringBootApplication;
21 import org.springframework.boot.context.properties.EnableConfigurationProperties;
22 import org.springframework.context.annotation.Bean;
24 import java.util.Optional;
25 import java.util.Properties;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
30 @SpringBootApplication
31 @EnableConfigurationProperties(TransferServiceProperties.class)
33 public class TransferServiceApplication
35 @Bean(destroyMethod = "close")
36 KafkaProducer<String, String> producer(TransferServiceProperties properties)
38 Properties props = new Properties();
39 props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
40 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
41 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
43 return new KafkaProducer<>(props);
47 KafkaConsumer<String, String> consumer(TransferServiceProperties properties)
49 Properties props = new Properties();
50 props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
51 props.put(ConsumerConfig.GROUP_ID_CONFIG, properties.groupId);
52 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
53 props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
54 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
55 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
57 return new KafkaConsumer<>(props);
60 @Bean(destroyMethod = "shutdown")
61 ExecutorService executorService()
63 return Executors.newFixedThreadPool(1);
66 @Bean(destroyMethod = "shutdown")
67 TransferConsumer transferConsumer(
68 TransferServiceProperties properties,
69 KafkaConsumer<String, String> consumer,
70 ExecutorService executorService,
72 TransferService productionTransferService,
73 TransferService restoreTransferService)
81 new TransferConsumer.ConsumerUseCases() {
83 public void create(Long id, Long payer, Long payee, Integer amount)
85 productionTransferService.create(id, payer, payee, amount);
89 public Optional<Transfer> get(Long id)
91 return productionTransferService.get(id);
95 public void handleStateChange(Long id, Transfer.State state)
97 productionTransferService.handleStateChange(id, state);
100 new TransferConsumer.ConsumerUseCases() {
102 public void create(Long id, Long payer, Long payee, Integer amount)
104 restoreTransferService.create(id, payer, payee, amount);
108 public Optional<Transfer> get(Long id)
110 return restoreTransferService.get(id);
114 public void handleStateChange(Long id, Transfer.State state)
116 restoreTransferService.handleStateChange(id, state);
122 KafkaMessagingService kafkaMessagingService(
123 KafkaProducer<String, String> producer,
125 TransferServiceProperties properties)
127 return new KafkaMessagingService(producer, mapper, properties.topic);
131 TransferService productionTransferService(
132 TransferRepository repository,
133 KafkaMessagingService kafkaMessagingService)
135 return new TransferService(repository, kafkaMessagingService);
139 TransferService restoreTransferService(
140 TransferRepository repository,
141 NoOpMessageService noOpMessageService)
143 return new TransferService(repository, noOpMessageService);
147 TransferController transferController(
148 TransferService productionTransferService,
149 KafkaMessagingService kafkaMessagingService)
151 return new TransferController(productionTransferService, kafkaMessagingService);
155 public static void main(String[] args)
157 SpringApplication.run(TransferServiceApplication.class, args);