Created
March 9, 2020 22:59
-
-
Save latant/dc41c1e31f337a0116404efb4d1a1315 to your computer and use it in GitHub Desktop.
An example using Java RMI with Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable | |
import java.rmi.Remote | |
import java.rmi.RemoteException | |
import java.rmi.registry.LocateRegistry | |
import java.rmi.server.UnicastRemoteObject | |
class Person(val name: String, val age: Int): Serializable | |
interface HelloService : Remote { | |
@Throws(RemoteException::class) | |
fun hello(person: Person): String | |
} | |
class HelloServiceImpl : HelloService { | |
override fun hello(person: Person) = "Hello, ${person.name}, you are ${person.age} years old.".also(::println) | |
} | |
inline fun <reified T: Remote> publish(port: Int, obj: T) { | |
LocateRegistry.createRegistry(port).bind(T::class.simpleName, UnicastRemoteObject.exportObject(obj, 0)) | |
} | |
inline fun <reified T: Remote> consume(host: String, port: Int): T { | |
return LocateRegistry.getRegistry(host, port).lookup(T::class.simpleName) as T | |
} | |
object Server { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
publish<HelloService>(4000, HelloServiceImpl()) | |
println("Server started") | |
} | |
} | |
object Client { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
val helloService = consume<HelloService>("localhost", 4000) | |
println(helloService.hello(Person("Bob", 20))) | |
} | |
} |
can you please intodunce good sources to learn more in kotlin?
In kotlinlang.org I could not find good lessons for topics such as RMI
Love it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks dear friend.
I am familiar with java but new to kotlin
This code taught me a lot.
Very usefull
Again thanks for your time and sharing such a clean code