Groovy

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 Groovy. It also supports using Java libraries inside Groovy's code, so it is easy to use JLupin Client library.

Native microservice

You can use libraries written in Java in your Groovy 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
|  |  |
|  |  +--+ groovy
|  |     |
|  |     +--+ com
|  |        |
|  |        +--+ example
|  |           |
|  |           +--+ configuration
|  |           |  |
|  |           |  +--+ GroovyHelloWorldJLupinConfiguration.groovy
|  |           |  |
|  |           |  +--+ GroovyHelloWorldSpringConfiguration.groovy
|  |           |
|  |           +--+ service
|  |              |
|  |              +--+ impl
|  |              |  |
|  |              |  +--+ ExampleServiceImpl.groovy
|  |              |
|  |              +--+ interfaces
|  |                 |
|  |                 +--+ ExampleService.groovy
|  |
|  +--+ test
|     |
|     +--+ groovy
|
+--- 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>groovy-hello-world</name>
    <artifactId>groovy-hello-world-implementation</artifactId>
    <groupId>com.example</groupId>
    <version>1.0-SNAPSHOT</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>
        <gmavenplus-plugin.version>1.6.2</gmavenplus-plugin.version>
        <groovy.version>2.5.6</groovy.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.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
            <type>pom</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>${gmavenplus-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addSources</goal>
                            <goal>addTestSources</goal>
                            <goal>compile</goal>
                            <goal>compileTests</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 groovy support.

Microservice code

Add two files with configuration: one for JLupin (GroovyHelloWorldJLupinConfiguration) and one for Spring container (GroovyHelloWorldSpringConfiguration). 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 GroovyHelloWorldJLupinConfiguration extends JLupinAbstractApplicationContainerProducer {
    @Override
    JLupinApplicationContainer produceJLupinApplicationContainer() {
        return new JLupinAbstractSpringApplicationContainer() {
            @Override
            AbstractApplicationContext getAbstractApplicationContext() {
                return new AnnotationConfigApplicationContext(GroovyHelloWorldSpringConfiguration.class)
            }
        }
    }
}
package com.example.configuration

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration

@Configuration
@ComponentScan("com.example")
class GroovyHelloWorldSpringConfiguration {
    @Bean("jLupinRegularExpressionToRemotelyEnabled")
    def getRemotelyBeanList() {
        def list = new 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 {
    String hello(String name)
}
package com.example.service.impl

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

@Service(value = "exampleService")
class ExampleServiceImpl implements ExampleService {
    @Override
    String hello(final String name) {
        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.GroovyHelloWorldJLupinConfiguration
[...]

Also put default configuration for Log4j2 (link).

Microservice deployment

You can deploy 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/groovy-hello-world/exampleService/hello -H 'Content-Type: application/json' -H 'X-JLNS-API-ID: ROA' -d '"Peter"'

As output you should see:

"Hello, Peter!"