When I add Hystrix support to a kotlin spring boot project, some fields of an abstract class are suddenly null. It seems the Hystrix proxy does not handle fields properly:
I have an abstract kotlin class ClientHandler
having two fields logger
and test
directly initialized. I have a kotlin subclass SomeHandler
of ClientHandler
which I use via autowiring in a class. As expected when using the subclass the two fields are initialized.
But when I add Hystrix support (uncomment @EnableHystrix
and @HystrixCommand
) the two fields logger
and test
in the abstract class are suddenly null.
Do you have any ideas why this happens? Spring adds a proxy class to the SomeHandler
class, Hystrix does the same. So I assume something goes wrong in the double proxying in combination with Kotlin!? I did the same with a java class and everything went fine.
package com.test
import mu.KotlinLogging
import org.springframework.stereotype.Component
import java.lang.RuntimeException
abstract class ClientHandler {
protected val logger = KotlinLogging.logger {}
private val test = "test"
//@HystrixCommand(commandKey = "Some Service")
fun send(communication: Communication): ResponseObject {
try {
return callApi(communication)
} catch (e: Exception) {
logger.warn("Could not send information", e)
return ResponseObject()
}
}
protected abstract fun callApi(communication: Communication): ResponseObject
}
@Component
class SomeHandler : ClientHandler() {
override fun callApi(communication: Communication): ResponseObject {
throw RuntimeException()
}
}
class Communication {
}
class ResponseObject {
}
The application
package com.test
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
//@EnableHystrix
@SpringBootApplication
class SomeApplication {
fun main(args: Array<String>) {
runApplication<SomeApplication>(*args)
}
}
The test
package com.test
import org.hibernate.validator.internal.util.Contracts.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
@SpringBootTest
class CustomerApiImplTest {
@Autowired
private lateinit var someHandler: SomeHandler
@Test
fun testIt() {
val communication = Communication()
val responseEntity = someHandler.send(communication)
assertNotNull(responseEntity)
}
}
The 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>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<properties>
<kotlin.version>1.3.21</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging</artifactId>
<version>1.6.25</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<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>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/swagger/src/gen/java/main</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<args>
<arg>-Xjsr305=strict</arg> <!-- Enable strict mode for JSR-305 annotations -->
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I was able to figure it out by myself: The function send
has to be open, so Hystrix and Spring will create a proper Proxy around the abstract class including the two fields.