Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java, Kotlin, Spring Boot

Java, Kotlin, Spring Boot

2019/5/28に開催されたLINE Developer Meetup #54 in Fukuokaでの登壇資料です

LINE Developers

May 28, 2019
Tweet

More Decks by LINE Developers

Other Decks in Technology

Transcript

  1. AGENDA ➤ Introduction [1 min] ➤ LINE Shop [2 min]

    ➤ Integrating Kotlin into a Spring Boot Application [8 min] ➤ Problems [3 min] ➤ Questions?
  2. ABOUT ME ➤ Parth Upadhyay ➤ Backend Engineer working on

    LINE Shop and LINE Wallet ➤ Most recently worked on Custom Stickers (written in Kotlin)
  3. LINE SHOP STACK ➤ Backend Databases: ➤ MySQL, mongoDB ➤

    Redis for caching ➤ Frameworks ➤ Spring Boot (dependency injection, configuration management, etc.) ➤ Armeria (async request handling framework) ➤ Languages ➤ Java ➤ Kotlin
  4. INTEGRATING KOTLIN ➤ Why? ➤ Just want to try! ➤

    Coroutines looked interesting (as a simple alternative to RxJava) ➤ Null Safety, Immutable Data Classes, Type Inference ➤ (some features coming to Java soon) ➤ Overall Impressions ➤ “Minimally painful” ➤ Kotlin and Java interop is almost perfect ➤ Spring Boot / Kotlin interop is now relatively well supported
  5. KOTLIN AND SPRING BOOT ➤ Since Spring 5.0, there has

    been official support for Kotlin ➤ Before that, there were problems with open classes and configuration properties. ➤ References ➤ https://spring.io/blog/2017/01/04/introducing-kotlin- support-in-spring-framework-5-0 ➤ https://spring.io/guides/tutorials/spring-boot-kotlin/
  6. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 1: Normal

    Classes ➤ All Kotlin classes are final by default ➤ Spring needs to extend them for AOP ➤ Declare open manually, or ➤ Import kotlin-spring plugin (recommended)
  7. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 1: Normal

    Classes ➤ After plugin, just write / use the code. ➤ Some Caveats ➤ Be careful of libraries that may require special treatment for Kotlin ➤ ex: Jackson ObjectMapper ➤ Need to configure using KotlinModule() ➤ Null Safety (talk about it later)
  8. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 2: Configuration

    and Configuration Properties ➤ Bean Configuration works easily in Kotlin
  9. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 2: Configuration

    and Configuration Properties ➤ Configuration Properties are more difficult. ➤ Need to use “lateinit var” for now. ➤ This is solved in Spring Boot 2.2
  10. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 3: Application

    Runner ➤ Also quite simple ➤ Just update classname in build.gradle
  11. ASYNCHRONOUS PROGRAMMING IN LINE SHOP ➤ Armeria is an asynchronous

    RPC/Rest client/server library built on top of Netty. ➤ Allows easy interface to return CompletableFutures from Controllers. ➤ In our codebase, we tend to use RxJava and Java Completable Futures in our code. ➤ What are coroutines? ➤ Kotlin’s asynchronous programming model ➤ Gives the appearance of writing imperative code, but is actually asynchronous
  12. COROUTINES ➤ Consider this code. ➤ Each method might take

    a really long time (call some database) ➤ We don’t want to block all resources during this time.
  13. COROUTINES ➤ Using RxJava, we can easily make this code

    asynchronous. ➤ But it brings in complexity. ➤ Which thread pool to observe on? How to handle failures? etc.
  14. MIXING COROUTINES, JAVA FUTURES, AND RX JAVA ➤ Easy conversion

    methods available everywhere ➤ Coroutine -> Future ➤ GlobalScope.future(<executor service>) { … } ➤ Future -> Coroutine ➤ future.await() ➤ RxJava -> Coroutine ➤ rxJava.await() ➤ Coroutine -> RxJava ➤ GlobalScope.rxSingle/rxCompletable/…. ➤ Extremely easy to switch back and forth.
  15. NULL IN KOTLIN ➤ Null is part of the type

    system, so you have to declare whether an object is “nullable” or not.
  16. NULL IN JAVA ➤ Every object in java could be

    null. So what happens when you interact? ➤ Kotlin “assumes it is not null” but you can specify explicitly if you think it will be null. ➤ EXCEPTION: Kotlin will respect Nullable / NonNull annotations.
  17. CONCLUSIONS ➤ Low cost to try out using Kotlin in

    your own codebase if you’re interested. ➤ Well supported by frameworks and recently becoming supported by libraries. ➤ Please give it a shot!