├── .gitignore ├── LICENSE ├── README.adoc ├── from-maxwell ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── java │ │ ├── ChangeEvent.java │ │ └── FromMaxwellApplication.java │ ├── kotlin │ └── com │ │ └── example │ │ └── kotlin │ │ └── FromMaxwellApplication.kt │ └── resources │ └── application.yml ├── read-currencies ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── example │ │ └── java │ │ └── Cdc1Application.java │ ├── kotlin │ └── com │ │ └── example │ │ └── kotlin │ │ └── ReadCurrenciesApplication.kt │ └── resources │ └── application.yml └── update-currency ├── .gitignore ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ └── demo │ │ └── UpdateCurrencyApplication.java └── resources │ └── application.properties └── test └── java └── com └── example └── demo └── UpdateCurrencyApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | .mvn 26 | mvnw* 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | == s1p-2019 2 | Projects for Spring One Platform 2019 3 | 4 | === Event-Driven Architectures for Spring Developers 5 | 6 | ==== Overview 7 | 8 | This repo contains 3 Spring Boot projects 9 | 10 | - **update-currency** - A REST application for updating currency conversion rates: 11 | It uses Spring JPA and will auto-create the schema for the `currency` table; the `application.properties` are configured to use a database named `cdc`. 12 | 13 | - **from-maxwell** - Receives change events from MySQL via Zendesk Maxwell and a Kafka topic `maxwell` and forwards currency changes to compacted topic `currency`. 14 | Deletions are forwarded as tombstone records. 15 | 16 | - **read-currency** - Receives currency events and stores them in a map for instant use within the application. 17 | Java and Kotlin versions are provided. 18 | 19 | ==== Setup 20 | 21 | Install MySQL and Maxwell (I used homebrew on Mac OS). 22 | 23 | I had some [authentication issues with MySQL 8, documented here](https://github.com/zendesk/maxwell/issues/1232). 24 | 25 | .my.cnf 26 | ``` 27 | server_id=1 28 | log-bin=master 29 | binlog_format=row 30 | default-authentication-plugin=mysql_native_password 31 | ``` 32 | 33 | ``` 34 | CREATE USER 'maxwell'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS 'maxwell'; 35 | ``` 36 | 37 | ==== Running 38 | 39 | Run maxwell 40 | 41 | ``` 42 | $ maxwell --user maxwell --password maxwell --producer=kafka --kafka.bootstrap.servers=localhost:9092 --kafka_topic=maxwell 43 | ``` 44 | 45 | Run all three boot applications and post a few currency updates: 46 | 47 | ``` 48 | $ curl -X POST http://localhost:8080/update/USD/1000 49 | 50 | $ curl -X POST http://localhost:8080/update/GBP/782 51 | 52 | $ curl -X POST http://localhost:8080/update/EUR/800 53 | 54 | $ curl -X POST http://localhost:8080/delete/EUR 55 | ``` 56 | 57 | Observe the console of the **read-currency** application. 58 | -------------------------------------------------------------------------------- /from-maxwell/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | 27 | ### VS Code ### 28 | .vscode/ 29 | -------------------------------------------------------------------------------- /from-maxwell/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RC1 9 | 10 | 11 | net.gprussell 12 | from-maxwell 13 | 0.0.1-SNAPSHOT 14 | cdc 15 | cdc 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | org.springframework.kafka 28 | spring-kafka 29 | 30 | 31 | org.jetbrains.kotlin 32 | kotlin-stdlib 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | org.springframework.kafka 42 | spring-kafka-test 43 | test 44 | 45 | 46 | com.fasterxml.jackson.core 47 | jackson-databind 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /from-maxwell/src/main/java/com/example/java/ChangeEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.java; 18 | 19 | /** 20 | * @author Gary Russell 21 | * 22 | */ 23 | public class ChangeEvent { 24 | 25 | private String database; 26 | 27 | private String table; 28 | 29 | private Type type; 30 | 31 | private Currency data; 32 | 33 | public String getDatabase() { 34 | return this.database; 35 | } 36 | 37 | public void setDatabase(String database) { 38 | this.database = database; 39 | } 40 | 41 | public String getTable() { 42 | return this.table; 43 | } 44 | 45 | public void setTable(String table) { 46 | this.table = table; 47 | } 48 | 49 | public Type getType() { 50 | return this.type; 51 | } 52 | 53 | public void setType(Type type) { 54 | this.type = type; 55 | } 56 | 57 | public Currency getData() { 58 | return this.data; 59 | } 60 | 61 | public void setData(Currency data) { 62 | this.data = data; 63 | } 64 | 65 | public enum Type { 66 | insert, update, delete 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "ChangeEvent [database=" + this.database + ", table=" + this.table + ", type=" + this.type 72 | + ", currency=" + this.data + "]"; 73 | } 74 | 75 | public static class Currency { 76 | 77 | private String symbol; 78 | 79 | private int rate; 80 | 81 | public String getSymbol() { 82 | return this.symbol; 83 | } 84 | 85 | public void setSymbol(String abbrev) { 86 | this.symbol = abbrev; 87 | } 88 | 89 | public int getRate() { 90 | return this.rate; 91 | } 92 | 93 | public void setRate(int rate) { 94 | this.rate = rate; 95 | } 96 | 97 | @Override 98 | public String toString() { 99 | return "Currency [symbol=" + this.symbol + ", rate=" + this.rate + "]"; 100 | } 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /from-maxwell/src/main/java/com/example/java/FromMaxwellApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.java; 18 | 19 | import org.apache.kafka.clients.admin.NewTopic; 20 | import org.slf4j.Logger; 21 | import org.slf4j.LoggerFactory; 22 | 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.boot.SpringApplication; 25 | import org.springframework.boot.autoconfigure.SpringBootApplication; 26 | import org.springframework.context.annotation.Bean; 27 | import org.springframework.kafka.annotation.KafkaListener; 28 | import org.springframework.kafka.config.TopicBuilder; 29 | import org.springframework.kafka.core.KafkaTemplate; 30 | import org.springframework.kafka.support.converter.RecordMessageConverter; 31 | import org.springframework.kafka.support.converter.StringJsonMessageConverter; 32 | 33 | import com.example.java.ChangeEvent.Type; 34 | 35 | @SpringBootApplication 36 | public class FromMaxwellApplication { 37 | 38 | private static final Logger logger = LoggerFactory.getLogger(FromMaxwellApplication.class); 39 | 40 | @Autowired 41 | private KafkaTemplate template; 42 | 43 | public static void main(String[] args) { 44 | SpringApplication.run(FromMaxwellApplication.class, args); 45 | } 46 | 47 | @KafkaListener(id = "cdc", topics = "maxwell") 48 | public void listen(ChangeEvent change) { 49 | logger.info("Received: " + change); 50 | if (change.getTable().equals("currency")) { 51 | Type type = change.getType(); 52 | if (type.equals(Type.insert) || type.equals(Type.update)) { 53 | String symbol = change.getData().getSymbol(); 54 | double rate = ((change.getData().getRate())) / 1000.; 55 | this.template.send("currency", symbol, rate); 56 | } 57 | if (type.equals(Type.delete)) { 58 | String symbol = change.getData().getSymbol(); 59 | this.template.send("currency", symbol, null); 60 | } 61 | } 62 | } 63 | 64 | @Bean 65 | public NewTopic topic() { 66 | return TopicBuilder.name("currency").build(); 67 | } 68 | 69 | @Bean 70 | public NewTopic maxwell() { 71 | return TopicBuilder.name("maxwell").compact().build(); 72 | } 73 | 74 | @Bean 75 | public RecordMessageConverter converter() { 76 | return new StringJsonMessageConverter(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /from-maxwell/src/main/kotlin/com/example/kotlin/FromMaxwellApplication.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.kotlin 18 | 19 | import org.apache.kafka.clients.admin.NewTopic 20 | import org.slf4j.LoggerFactory 21 | 22 | import org.springframework.beans.factory.annotation.Autowired 23 | import org.springframework.boot.SpringApplication 24 | import org.springframework.boot.autoconfigure.SpringBootApplication 25 | import org.springframework.context.annotation.Bean 26 | import org.springframework.kafka.annotation.KafkaListener 27 | import org.springframework.kafka.config.TopicBuilder 28 | import org.springframework.kafka.core.KafkaTemplate 29 | import org.springframework.kafka.support.converter.RecordMessageConverter 30 | import org.springframework.kafka.support.converter.StringJsonMessageConverter 31 | 32 | import com.example.java.ChangeEvent 33 | import com.example.java.ChangeEvent.Type 34 | 35 | @SpringBootApplication 36 | open class FromMaxwellApplication { 37 | 38 | @Autowired 39 | private val template: KafkaTemplate? = null 40 | 41 | @KafkaListener(id = "cdc", topics = ["maxwell"]) 42 | fun listen(change: ChangeEvent) { 43 | logger.info("Received: $change") 44 | if (change.table == "currency") { 45 | val type = change.type 46 | if (type == Type.insert || type == Type.update) { 47 | val symbol = change.data.symbol 48 | val rate = change.data.rate / 1000.0 49 | this.template!!.send("currency", symbol, rate) 50 | } 51 | if (type == Type.delete) { 52 | val symbol = change.data.symbol 53 | this.template!!.send("currency", symbol, null) 54 | } 55 | } 56 | } 57 | 58 | @Bean 59 | open fun topic(): NewTopic { 60 | return TopicBuilder.name("currency").compact().build() 61 | } 62 | 63 | @Bean 64 | open fun maxwell(): NewTopic { 65 | return TopicBuilder.name("maxwell").build() 66 | } 67 | 68 | @Bean 69 | open fun converter(): RecordMessageConverter { 70 | return StringJsonMessageConverter() 71 | } 72 | 73 | companion object { 74 | 75 | private val logger = LoggerFactory.getLogger(FromMaxwellApplication::class.java) 76 | 77 | @JvmStatic 78 | fun main(args: Array) { 79 | SpringApplication.run(FromMaxwellApplication::class.java, *args) 80 | } 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /from-maxwell/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | kafka: 3 | consumer: 4 | auto-offset-reset: earliest 5 | enable-auto-commit: false 6 | producer: 7 | value-serializer: org.apache.kafka.common.serialization.DoubleSerializer 8 | -------------------------------------------------------------------------------- /read-currencies/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /nbbuild/ 22 | /dist/ 23 | /nbdist/ 24 | /.nb-gradle/ 25 | /build/ 26 | 27 | ### VS Code ### 28 | .vscode/ 29 | -------------------------------------------------------------------------------- /read-currencies/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.0.RC1 9 | 10 | 11 | net.gprussell 12 | read-currencies 13 | 0.0.1-SNAPSHOT 14 | cdc-1 15 | cdc 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | org.springframework.kafka 28 | spring-kafka 29 | 30 | 31 | org.jetbrains.kotlin 32 | kotlin-stdlib 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | org.springframework.kafka 42 | spring-kafka-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /read-currencies/src/main/java/com/example/java/Cdc1Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.java; 18 | 19 | import java.util.Collection; 20 | import java.util.Map; 21 | import java.util.concurrent.ConcurrentHashMap; 22 | 23 | import org.apache.kafka.clients.admin.NewTopic; 24 | import org.apache.kafka.clients.consumer.Consumer; 25 | import org.apache.kafka.common.TopicPartition; 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | import org.springframework.boot.SpringApplication; 30 | import org.springframework.boot.autoconfigure.SpringBootApplication; 31 | import org.springframework.context.annotation.Bean; 32 | import org.springframework.kafka.annotation.KafkaListener; 33 | import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; 34 | import org.springframework.kafka.config.TopicBuilder; 35 | import org.springframework.kafka.listener.ConsumerAwareRebalanceListener; 36 | import org.springframework.kafka.support.KafkaHeaders; 37 | import org.springframework.messaging.handler.annotation.Header; 38 | import org.springframework.messaging.handler.annotation.Payload; 39 | import org.springframework.stereotype.Component; 40 | 41 | @SpringBootApplication 42 | public class Cdc1Application { 43 | 44 | private static final Logger logger = LoggerFactory.getLogger(Cdc1Application.class); 45 | 46 | private final Map currencies = new ConcurrentHashMap<>(); 47 | 48 | public static void main(String[] args) { 49 | SpringApplication.run(Cdc1Application.class, args); 50 | } 51 | 52 | @KafkaListener(id = "currency1", topics = "currency") 53 | public void listen(@Payload(required = false) Double rate, 54 | @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key) { 55 | 56 | if (rate == null) { 57 | this.currencies.remove(key); 58 | } 59 | else { 60 | this.currencies.put(key, rate); 61 | } 62 | logger.info("Currencies now: " + this.currencies); 63 | } 64 | 65 | @Bean 66 | public NewTopic topic() { 67 | return TopicBuilder.name("currency") 68 | .compact() 69 | .partitions(1) 70 | .replicas(1) 71 | .build(); 72 | } 73 | 74 | } 75 | 76 | @Component 77 | class FactoryConfigurer { 78 | 79 | FactoryConfigurer(ConcurrentKafkaListenerContainerFactory factory) { 80 | factory.getContainerProperties().setConsumerRebalanceListener(new ConsumerAwareRebalanceListener() { 81 | 82 | @Override 83 | public void onPartitionsAssigned(Consumer consumer, Collection partitions) { 84 | consumer.seekToBeginning(partitions); 85 | } 86 | 87 | }); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /read-currencies/src/main/kotlin/com/example/kotlin/ReadCurrenciesApplication.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.kotlin 18 | 19 | import java.util.concurrent.ConcurrentHashMap 20 | 21 | import org.apache.kafka.clients.admin.NewTopic 22 | import org.apache.kafka.clients.consumer.Consumer 23 | import org.apache.kafka.common.TopicPartition 24 | import org.slf4j.LoggerFactory 25 | 26 | import org.springframework.boot.SpringApplication 27 | import org.springframework.boot.autoconfigure.SpringBootApplication 28 | import org.springframework.context.annotation.Bean 29 | import org.springframework.kafka.annotation.KafkaListener 30 | import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory 31 | import org.springframework.kafka.config.TopicBuilder 32 | import org.springframework.kafka.listener.ConsumerAwareRebalanceListener 33 | import org.springframework.kafka.support.KafkaHeaders 34 | import org.springframework.messaging.handler.annotation.Header 35 | import org.springframework.messaging.handler.annotation.Payload 36 | import org.springframework.stereotype.Component 37 | 38 | @SpringBootApplication 39 | open class ReadCurrenciesApplication { 40 | 41 | private val currencies = ConcurrentHashMap() 42 | 43 | @KafkaListener(id = "currency1", topics = ["currency"]) 44 | fun listen(@Payload(required = false) rate: Double?, 45 | @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) key: String) { 46 | 47 | if (rate == null) { 48 | this.currencies.remove(key) 49 | } else { 50 | this.currencies[key] = rate 51 | } 52 | logger.info("Currencies now: " + this.currencies) 53 | } 54 | 55 | @Bean 56 | open fun topic(): NewTopic { 57 | return TopicBuilder.name("currency") 58 | .compact() 59 | .partitions(1) 60 | .replicas(1) 61 | .build() 62 | } 63 | 64 | companion object { 65 | 66 | private val logger = LoggerFactory.getLogger(ReadCurrenciesApplication::class.java) 67 | 68 | @JvmStatic 69 | fun main(args: Array) { 70 | SpringApplication.run(ReadCurrenciesApplication::class.java, *args) 71 | } 72 | } 73 | 74 | } 75 | 76 | @Component 77 | internal class FactoryConfigurer(factory: ConcurrentKafkaListenerContainerFactory<*, *>) { 78 | 79 | init { 80 | factory.containerProperties.consumerRebalanceListener = object : ConsumerAwareRebalanceListener { 81 | 82 | override fun onPartitionsAssigned(consumer: Consumer<*, *>?, partitions: Collection?) { 83 | consumer!!.seekToBeginning(partitions) 84 | } 85 | 86 | } 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /read-currencies/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | kafka: 3 | consumer: 4 | auto-offset-reset: earliest 5 | enable-auto-commit: false 6 | value-deserializer: org.apache.kafka.common.serialization.DoubleDeserializer 7 | producer: 8 | value-serializer: org.apache.kafka.common.serialization.DoubleSerializer 9 | properties: 10 | delivery.timeout.ms: 123456 11 | -------------------------------------------------------------------------------- /update-currency/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | 30 | ### VS Code ### 31 | .vscode/ 32 | -------------------------------------------------------------------------------- /update-currency/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.9.RELEASE 9 | 10 | 11 | com.example 12 | update-currency 13 | 0.0.1-SNAPSHOT 14 | update-currency 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jdbc 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-jdbc 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | runtime 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-data-jpa 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /update-currency/src/main/java/com/example/demo/UpdateCurrencyApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 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 com.example.demo; 18 | 19 | import javax.persistence.Entity; 20 | import javax.persistence.EntityManager; 21 | import javax.persistence.Id; 22 | 23 | import org.springframework.beans.factory.annotation.Autowired; 24 | import org.springframework.boot.SpringApplication; 25 | import org.springframework.boot.autoconfigure.SpringBootApplication; 26 | import org.springframework.transaction.annotation.Transactional; 27 | import org.springframework.web.bind.annotation.PathVariable; 28 | import org.springframework.web.bind.annotation.PostMapping; 29 | import org.springframework.web.bind.annotation.RestController; 30 | 31 | @SpringBootApplication 32 | public class UpdateCurrencyApplication { 33 | 34 | public static void main(String[] args) { 35 | SpringApplication.run(UpdateCurrencyApplication.class, args); 36 | } 37 | 38 | } 39 | 40 | @RestController 41 | @Transactional 42 | class Rest { 43 | 44 | @Autowired 45 | EntityManager em; 46 | 47 | @PostMapping(path = "update/{symbol}/{rate}") 48 | public void post(@PathVariable String symbol, @PathVariable int rate) { 49 | em.merge(new Currency(symbol, rate)); 50 | } 51 | 52 | @PostMapping(path = "delete/{symbol}") 53 | public void delete(@PathVariable String symbol) { 54 | Currency found = em.find(Currency.class, symbol); 55 | em.remove(found); 56 | } 57 | 58 | } 59 | 60 | @Entity 61 | class Currency { 62 | 63 | @Id 64 | String symbol; 65 | 66 | int rate; 67 | 68 | public Currency() { 69 | super(); 70 | } 71 | 72 | public Currency(String symbol, int rate) { 73 | this.symbol = symbol; 74 | this.rate = rate; 75 | } 76 | 77 | protected String getSymbol() { 78 | return this.symbol; 79 | } 80 | 81 | protected void setSymbol(String symbol) { 82 | this.symbol = symbol; 83 | } 84 | 85 | protected int getRate() { 86 | return this.rate; 87 | } 88 | 89 | protected void setRate(int rate) { 90 | this.rate = rate; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /update-currency/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost/cdc?serverTimezone=UTC 2 | spring.datasource.username=root 3 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 4 | 5 | spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 6 | spring.jpa.hibernate.ddl-auto=update 7 | -------------------------------------------------------------------------------- /update-currency/src/test/java/com/example/demo/UpdateCurrencyApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class UpdateCurrencyApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------