Java, Kotlin, Spring Boot

Java, Kotlin, Spring Boot

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

53850955f15249a1a9dc49df6113e400?s=128

LINE Developers

May 28, 2019
Tweet

Transcript

  1. JAVA, KOTLIN, SPRING BOOT Parth Upadhyay LINE Fukuoka 2019/05/28 LINE

    Developer Meetup #54 in Fukuoka
  2. AGENDA ➤ Introduction [1 min] ➤ LINE Shop [2 min]

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

    LINE Shop and LINE Wallet ➤ Most recently worked on Custom Stickers (written in Kotlin)
  4. LINE STORE ➤ Buy Stickers, Themes, Manga Coin, etc.

  5. 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
  6. INTEGRATING KOTLIN

  7. 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
  8. 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/
  9. 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)
  10. 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)
  11. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 2: Configuration

    and Configuration Properties ➤ Bean Configuration works easily in Kotlin
  12. 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
  13. 3 STEPS FOR INTEGRATING KOTLIN CODE ➤ Step 3: Application

    Runner ➤ Also quite simple ➤ Just update classname in build.gradle
  14. ASYNCHRONOUS PROGRAMMING WITH ARMERIA

  15. 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
  16. 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.
  17. 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.
  18. CONVERSION TO COROUTINE CODE

  19. 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.
  20. PROBLEMS ➤ Kotlin and Java have good interoperability ➤ Just

    be careful about null values!
  21. NULL IN KOTLIN ➤ Null is part of the type

    system, so you have to declare whether an object is “nullable” or not.
  22. 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.
  23. 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!
  24. Questions?