Kotlin

Any language compiled into byte code which is run on JVM can be used to write microservices for JLupin. The example of such a language is Kotling. It also supports using Java libraries inside Kotlin's code, so it is easy to use JLupin Client library.

Native microservice

You can use libraries written in Java in your Kotlin code, so you won't see any plain Java class.

Project structure and dependencies

This is standard structure of Kotlin project with use of Maven. Create directories and files as shown below.

+--+ additional-files
|  |
|  +--- configuration.yml
|  |
|  +--- log4j2.xml
|
+--+ src
|  |
|  +--+ main
|  |  |
|  |  +--+ kotlin
|  |     |
|  |     +--+ com
|  |        |
|  |        +--+ example
|  |           |
|  |           +--+ configuration
|  |           |  |
|  |           |  +--+ ScalaHelloWorldJLupinConfiguration.kt
|  |           |  |
|  |           |  +--+ ScalaHelloWorldSpringConfiguration.kt
|  |           |
|  |           +--+ service
|  |              |
|  |              +--+ impl
|  |              |  |
|  |              |  +--+ ExampleServiceImpl.kt
|  |              |
|  |              +--+ interfaces
|  |                 |
|  |                 +--+ ExampleService.kt
|  |
|  +--+ test
|     |
|     +--+ kotlin
|
+--- pom.xml

Configure your pom (pom.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>jlupin-platform-parent</artifactId>
        <groupId>com.jlupin</groupId>
        <version>1.6.0.2</version>
    </parent>

    <name>kotlin-hello-world</name>
    <artifactId>kotlin-hello-world-implementation</artifactId>
    <groupId>com.example</groupId>
    <version>1.0</version>

    <repositories>
        <!-- Repository is also accessible using https connection: -->
        <!-- https://support.jlupin.com/maven2/ -->
        <repository>
            <id>jlupin-central</id>
            <name>jlupin</name>
            <url>http://support.jlupin.com/maven2/</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <!-- Repository is also accessible using https connection: -->
        <!-- https://support.jlupin.com/maven2/ -->
        <pluginRepository>
            <id>jlupin-central</id>
            <name>jlupin</name>
            <url>http://support.jlupin.com/maven2/</url>
        </pluginRepository>
    </pluginRepositories>

    <properties>
        <kotlin.version>1.3.21</kotlin.version>

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.jlupin</groupId>
            <artifactId>jlupin-platform-native</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}-app-${project.version}</finalName>

        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.jlupin</groupId>
                <artifactId>jlupin-platform-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jlupin-repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jlupin-zip</id>
                        <goals>
                            <goal>zip</goal>
                        </goals>
                        <configuration>
                            <additionalFilesDirectories>
                                <param>additional-files</param>
                            </additionalFilesDirectories>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jlupin-deploy</id>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

It is default pom.xml for single native module microservice with only added kotlin support.

Microservice code

Add two files with configuration: one for JLupin (KotlinHelloWorldJLupinConfiguration) and one for Spring container (KotlinHelloWorldSpringConfiguration). Create package com.example.configuration and put classed there.

package com.example.configuration

import com.jlupin.impl.container.application.spring.JLupinAbstractSpringApplicationContainer
import com.jlupin.interfaces.configuration.microservice.container.application.JLupinAbstractApplicationContainerProducer
import com.jlupin.interfaces.container.application.JLupinApplicationContainer
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.support.AbstractApplicationContext

class KotlinHelloWorldJLupinConfiguration : JLupinAbstractApplicationContainerProducer() {
    override fun produceJLupinApplicationContainer(): JLupinApplicationContainer {
        return object : JLupinAbstractSpringApplicationContainer() {
            override fun getAbstractApplicationContext(): AbstractApplicationContext {
                return AnnotationConfigApplicationContext(KotlinHelloWorldSpringConfiguration::class.java)
            }
        }
    }
}
package com.example.configuration

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import java.util.*

@Configuration
@ComponentScan("com.example")
open class KotlinHelloWorldSpringConfiguration() {
    @Bean("jLupinRegularExpressionToRemotelyEnabled")
    open fun getRemotelyBeanList(): List<String> {
        val list = ArrayList<String>()
        list.add("exampleService")
        return list
    }
}

Microservice is configured but does nothing. Create two packages com.example.service.interfaces and com.example.service.impl and put service definitions in them:

package com.example.service.interfaces

interface ExampleService {
    fun hello(name: String): String
}
package com.example.service.impl

import com.example.service.interfaces.ExampleService
import org.springframework.stereotype.Service

@Service(value = "exampleService")
class ExampleServiceImpl : ExampleService {
    override fun hello(name: String): String {
        return "Hello, $name!"
    }
}

Microservice is done. You only need to add configuration for it. Create special directory for it called additional-files. Put in there two files: configuration.yml and log4j2.xml.

Put default configuration (link) in configuration.yml and update it's application section with proper class:

[...]
APPLICATION:
  applicationContainerProducerClassName: 'com.example.configuration.KotlinHelloWorldJLupinConfiguration
[...]

Also put default configuration for Log4j2 (link).

Microservice deployment

You can deplpoy your microservice manually or automatically. Just follow instructions in documentation.

Checking microservice

You can check if your microservice is running using HTTP Elastic API and cURL for example:

curl -X POST http://localhost:8082/kotlin-hello-world/exampleService/hello -H 'Content-Type: application/json' -H 'X-JLNS-API-ID: ROA' -d '"Peter"'

As output you should see:

"Hello, Peter!"