Using Infinispan with Kotlin
What You Will Learn
How to use the Infinispan Hot Rod Java client from Kotlin, including basic cache operations and Protobuf serialization with @Proto annotations on Kotlin data classes.
Prerequisites
-
Java 17+
-
An Infinispan Server running on
localhost:11222(or Docker/Podman available for Testcontainers)
Step 1: Add Kotlin and Infinispan Dependencies
Add the Kotlin standard library, Infinispan Hot Rod client, and Protostream dependencies to your pom.xml:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<version>${version.protostream}</version>
</dependency>
Configure the Kotlin Maven plugin with kapt for annotation processing. See the pom.xml for the full build configuration.
Step 2: Define a Protobuf Entity in Kotlin
Use @Proto on a Kotlin data class. Fields must use @JvmField for Protostream compatibility:
@Proto
data class GreetingKotlin(@JvmField var name: String?=null,
@JvmField var greeting: String?=null)
Register the schema:
@ProtoSchema(includeClasses = [GreetingKotlin::class])
interface GreetingSchemaKotlin : GeneratedSchema
Step 3: Connect and Perform Cache Operations
Use the same TutorialsConnectorHelper from Kotlin:
object InfinispanKotlinExample {
lateinit var cacheManager: RemoteCacheManager
lateinit var cache: RemoteCache<String, String>
@JvmStatic
fun main(args: Array<String>) {
connectToInfinispan()
manipulateCache()
disconnect()
}
fun manipulateCache() {
// Store a value
cache.put("key", "value")
// Retrieve the value and print it out
println("key = ${cache["key"]}")
}
fun connectToInfinispan() {
// Connect to the server
cacheManager = TutorialsConnectorHelper.connect()
// Obtain the remote cache
cache = cacheManager.getCache(TUTORIAL_CACHE_NAME)
}
fun disconnect() {
// Stop the cache manager and release all resources
TutorialsConnectorHelper.stop(cacheManager)
}
}
Step 4: Run the Tutorial
mvn package exec:java
You should see output like:
key = value
What’s Next
-
Get started with basic Java cache operations
-
Query your data with Ickle queries


