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

Kotlin EE: Boost your Productivity @ JBCNConf 2018

Kotlin EE: Boost your Productivity @ JBCNConf 2018

Kotlin is a language for the JVM, invented by JetBrains a few years ago. But what the hell is Kotlin EE? If you never heard of Kotlin EE, don’t panic, it does not exist. But you can use it right away and boost your productivity!

I invented the term Kotlin EE as a synonym for using the Kotlin language together with the Java EE API to create services of any size (microservices, nanoservices etc.) with just a few lines of code and the ability to focus on the business logic.

Kotlin and Java EE are a perfect couple for writing micro- or nanoservices. Kotlin is a very pragmatic language, builds on many concepts and techniques from Java, designed for developer productivity. Kotlin works great with all existing Java libraries and frameworks and runs with the same level of performance as Java.

The Java EE API allows us to code against a proven and stable API. Provided libraries like JAX-RS for writing RESTful APIs and Jackson for JSON (de)serializing decrease the need for additional third-party libraries which results in a short build time and a small artifact size. Benefit from a very fast build and test feedback and stay focused on your code.

In this talk, I try to prove my statements from above. Live on stage I write a service in Kotlin with a RESTful JSON API with only a few lines of code and run the service using a local Docker cloud where you can see how these can be scaled up and down to manage fluctuating loads. Coding, building, testing, deploying, scaling: fast and efficient!

Marcus Fihlon

June 11, 2018
Tweet

More Decks by Marcus Fihlon

Other Decks in Technology

Transcript

  1. Kotlin EE
    Boost Your Productivity with Kotlin, Jakarta EE and Docker
    Marcus Fihlon
    @McPringle #live
    June 11, 2018
    Barcelona

    View full-size slide

  2. @McPringle #live
    Disclaimer
    The following presentation has been approved for open audiences only. Hypersensitivity to occasional
    profanity requires covering ears.
    All logos, photos etc. used in this presentation are the property of their respective copyright owners and
    are used here for educational purposes only. Any and all marks used throughout this presentation are
    trademarks of their respective owners.
    The presenter is not acting on behalf of CSS Insurance, neither as an official agent nor representative. The
    views expressed are those solely of the presenter.
    Marcus Fihlon disclaims all responsibility for any loss or damage which any person may suffer from reliance
    on this information or any opinion, conclusion or recommendation in this presentation whether the loss or
    damage is caused by any fault or negligence on the part of presenter or otherwise.

    View full-size slide

  3. @McPringle #live
    Agenda
    ● Introduction
    ● Why Kotlin
    ● Why Jakarta EE
    ● Kotlin EE
    ● Live Coding
    ● Wrap-Up

    View full-size slide

  4. Introduction

    View full-size slide

  5. @McPringle #live
    About me
    ● Agile Coach
    CSS Insurance
    ● Software Engineer
    CSS Insurance, Open Source Software
    ● Lecturer
    TEKO Swiss Technical College
    ● Speaker
    Conferences, User Groups, Meetups
    ● Author
    Articles, Books
    ● Community Leader
    Hackergarten, Java User Group Switzerland, Kotlin Swiss User Group,
    Voxxed Days Zürich, BaselOne

    View full-size slide

  6. @McPringle #live
    Coming soon*
    Kotlin Web Development
    Develop full stack web applications with Kotlin and React.js
    Packt Publishing
    ISBN 978-1-78862-031-4
    * around Q3/2018

    View full-size slide

  7. @McPringle #live
    Buzzword Bingo
    ● Monolith
    Bad by default!
    ● Microservices
    Solve every problem!
    ● Nanoservices
    Solve all other Problems!
    ● Docker
    Just because it's cool!
    ● Jakarta EE
    Uncool and heavy framework for bloated monoliths!

    View full-size slide

  8. @McPringle #live
    About Kotlin
    ● Statically typed
    ● Object oriented
    ● Java compatible
    ● Easy to learn
    ● Compiles to Java 6 Bytecode
    ● Compiles to Java 8 Bytecode
    ● Transpiles to JavaScript
    ● Compiles to Native

    View full-size slide

  9. @McPringle #live
    Java Syntax
    public class App {
    public static void main(String[] args) {
    final String event = "JBCNConf";
    System.out.println("Hi " + event + "!"); // Hi JBCNConf!
    }
    }

    View full-size slide

  10. @McPringle #live
    Boilerplate
    public class App {
    public static void main(String[] args) {
    final String event = "JBCNConf";
    System.out.println("Hi " + event + "!");
    }
    }

    View full-size slide

  11. @McPringle #live
    Kotlin Syntax
    fun main(args: Array) {
    val event = "JBCNConf"
    println("Hi $event!") // Hi JBCNConf!
    }

    View full-size slide

  12. @McPringle #live
    Classes and Properties
    class Person(val name: String,
    val age: Int,
    val company: String?) {

    }

    View full-size slide

  13. @McPringle #live
    Classes final by default
    open class Person(val name: String,
    val age: Int,
    val company: String?) {

    }

    View full-size slide

  14. @McPringle #live
    Data classes
    data class Person(val name: String,
    val age: Int,
    val company: String?)
    val marcus = Person("Marcus", 24, null)
    println(marcus) // Person(name=Marcus, age=24, company=null)
    val marcusCSS = marcus.copy(company = "CSS Insurance")
    println(marcusCSS) // Person(name=Marcus, age=24, company=CSS Insurance)

    View full-size slide

  15. @McPringle #live
    Default Values
    data class Person(val name: String,
    val age: Int,
    val company: String? = null)
    val marcus = Person("Marcus", 24)
    println(marcus) // Person(name=Marcus, age=24, company=null)
    val marcusCSS = Person("Marcus", 24, "CSS Insurance")
    println(marcusCSS) // Person(name=Marcus, age=24, company=CSS Insurance)

    View full-size slide

  16. @McPringle #live
    Named Parameters
    data class Person(val name: String,
    val age: Int,
    val company: String? = null)
    val marcus = Person(name = "Marcus", age = 24)
    println(marcus) // Person(name=Marcus, age=24, company=null)
    val marcusCSS = marcus.copy(company = "CSS Insurance")
    println(marcusCSS) // Person(name=Marcus, age=24, company=CSS Insurance)

    View full-size slide

  17. @McPringle #live
    String Templates
    data class Person(val name: String,
    val age: Int,
    val company: String? = null)
    val marcus = Person(name = "Marcus", age = 24)
    println("$marcus.name is $marcus.age years old")
    // Marcus is 24 years old
    println("${marcus.name} is ${marcus.age} years old")
    // Marcus is 24 years old

    View full-size slide

  18. @McPringle #live
    Multiline Strings
    data class Person(val name: String,
    val age: Int,
    val company: String? = null)
    val marcus = Person(name = "Marcus", age = 24)
    println("""Name: $marcus.name
    Age: $marcus.age
    Company: $marcus.company""")

    View full-size slide

  19. @McPringle #live
    Singletons
    object PersonController {
    val persons = mutableListOf()
    fun save(person: Person) {
    persons.add(person)
    }
    }
    PersonController.save(marcus)

    View full-size slide

  20. @McPringle #live
    Coroutines
    fun main(args: Array) {
    println("Before")
    launch(CommonPool) {
    delay(2000)
    println("Hello from coroutine")
    }
    println("After") // Before
    Thread.sleep(5000) // After
    } // Hello from coroutine

    View full-size slide

  21. @McPringle #live
    Typesafe Builder
    val page = html {
    head {
    title { "My Website" }
    }
    body {
    p { "Hello JUG.CH!" }
    }
    }

    View full-size slide

  22. Why Jakarta EE

    View full-size slide

  23. @McPringle #live
    Library Management as a Service
    ● Servlets, JSTL, EL and JSPs
    ● WebSockets
    ● JSF
    ● JAX-RS
    ● EJB lite
    ● JTA
    ● JPA
    ● Bean Validation
    ● CDI
    ● Interceptors
    ● JBatch
    ● Concurrency
    ● JCache
    ● …
    dependencies {
    providedCompile "javax:javaee-api:$javaee_version"
    }

    View full-size slide

  24. @McPringle #live
    Ready to use containers
    FROM payara/micro
    COPY myapplication.war /opt/payara/deployments
    My Application
    Application Server
    Java Runtime
    Operating System changes very infrequently
    changes sometimes, if not delayed
    changes frequently
    changes on every build

    View full-size slide

  25. @McPringle #live
    It's a Standard!

    View full-size slide

  26. @McPringle #live
    Classes final by default
    buildscript {
    dependencies {
    classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
    }
    }
    allOpen {
    annotation('javax.ejb.Stateless')
    annotation('javax.ws.rs.Path')
    }

    View full-size slide

  27. @McPringle #live
    Zero-Argument Constructor
    buildscript {
    dependencies {
    classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
    }
    }
    noArg {
    annotation('javax.ws.rs.Path')
    }

    View full-size slide

  28. @McPringle #live
    Architecture

    View full-size slide

  29. @McPringle #live
    Prepare the Swarm
    ● List all available machines
    docker-machine ls
    ● Create a new machine to be used as a manager node
    docker-machine create -d virtualbox mgr
    ● Create a new machine to be used as a worker node
    docker-machine create -d virtualbox node01
    ● Login to the manager
    docker-machine ssh mgr
    ● Initialize the swarm
    docker swarm init --advertise-addr 192.168.99.100

    View full-size slide

  30. @McPringle #live
    ● Start the visualizer service
    docker run -it -d -p 8080:8080 -e HOST=192.168.99.100 \
    -v /var/run/docker.sock:/var/run/docker.sock \
    --name visualizer dockersamples/visualizer
    ● Open the visualizer service in a web browser
    http://192.168.99.100:8080/
    Prepare the Visualizer

    View full-size slide

  31. @McPringle #live
    Join the Swarm
    ● Ask the manager node for the token to join the swarm
    docker swarm join-token worker
    ● Login to the worker node
    docker-machine ssh node01
    ● Join the worker to the swarm
    docker swarm join --token 192.168.99.100:2377

    View full-size slide

  32. @McPringle #live
    Verify the Swarm
    ● Login to the manager
    docker-machine ssh mgr
    ● List all nodes
    docker node ls
    ● Show more detailed information
    docker info

    View full-size slide

  33. @McPringle #live
    ● Login to the manager
    docker-machine ssh mgr
    ● Create a new overlay network
    docker network create -d overlay jbcnconf
    ● List all networks
    docker network ls
    Create a Network

    View full-size slide

  34. @McPringle #live
    Deploy Services
    ● Login to the manager
    docker-machine ssh mgr
    ● Deploy the time service
    docker service create --network jbcnconf \
    --name timeservice mcpringle/timeservice
    ● Deploy the hello service
    docker service create --network jbcnconf -p 8181:8080 \
    --name helloservice mcpringle/helloservice
    ● List all services
    docker service ls

    View full-size slide

  35. @McPringle #live
    Testing, Scaling and Maintenance
    ● Testing our services
    http http://192.168.99.100:8181/api/hello/JBCNConf
    ● Scaling services up and down
    docker service update --replicas 3 helloservice
    ● Take a node out of service
    docker node update --availability drain mgr
    ● Take a node back into service
    docker node update --availability active mgr

    View full-size slide

  36. @McPringle #live
    Conclusion
    With Kotlin, Jakarta EE and Docker you get:
    ● Easy to understand code
    ● Fast build times
    ● Reproducible deployments
    ● Easy scaling of your services
    You are fast and efficient because you have your focus on your business code.
    You create business value!

    View full-size slide

  37. One more thing…

    View full-size slide

  38. @McPringle #live
    You ever dreamed of living in Lucerne?

    View full-size slide

  39. @McPringle #live
    Code Warriors wanted!
    About you
    ● You are frighteningly awesome at what you do.
    ● You can perform quick and deadly tactical
    strikes, as well as feats of epic badassery.
    Sometimes both at the same time.
    ● You would rather refactor existing, mostly
    working, ugly code instead of rewriting it from
    scratch. OK nobody would really rather do that,
    but you know it's the right and honorable thing
    to do most of the time.
    ● You know where your towel is.
    About us
    ● Biggest Basic Health Insurance Company
    ● 2700 Employees
    ● 300 IT Professionals
    ● 24 Scrum Teams
    ● 130 Software Developers
    ● Flexible working times
    ● Personal development support
    ● 6 weeks paid vacation
    ● Great benefits
    ● Located in Lucerne, Switzerland
    Contact me: [email protected]

    View full-size slide

  40. @McPringle #live
    https://fihlon.ch/jbcnconf18

    View full-size slide