Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions coroutine-service-call/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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">
<parent>
<artifactId>lagomkotlin</artifactId>
<groupId>org.cakesolutions</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>coroutine-service-call</artifactId>

<dependencies>
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-server_2.11</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-jdk8</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.cakesolutions.coroutine

import com.lightbend.lagom.javadsl.api.ServiceCall
import com.lightbend.lagom.javadsl.api.transport.RequestHeader
import com.lightbend.lagom.javadsl.api.transport.ResponseHeader
import com.lightbend.lagom.javadsl.server.HeaderServiceCall
import com.lightbend.lagom.javadsl.server.ServerServiceCall
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.future

/**
* @author Alex Mihailov {@literal <[email protected]>}.
*/

/**
* Starts new coroutine and returns its result as an implementation of [ServiceCall].
*
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
* @param block the coroutine code.
*/
fun <Request, Response> serviceCall(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.(request: Request) -> Response
) = ServiceCall<Request, Response> {
CoroutineScope(Dispatchers.Unconfined).future(context, start) {
block(it)
}
}

/**
* Starts new coroutine and returns its result as an implementation of [ServerServiceCall].
*
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
* @param block the coroutine code.
*/
fun <Request, Response> serverServiceCall(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.(request: Request) -> Response
) = ServerServiceCall<Request, Response> {
CoroutineScope(Dispatchers.Unconfined).future(context, start) {
block(it)
}
}

/**
* Starts new coroutine and returns its result as an implementation of [HeaderServiceCall].
*
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine.
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
* @param block the coroutine code.
*/
fun <Request, Response> headerServiceCall(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.(requestHeader: RequestHeader, request: Request) -> akka.japi.Pair<ResponseHeader, Response>
) = HeaderServiceCall<Request, Response> { requestHeader, request ->
CoroutineScope(Dispatchers.Unconfined).future(context, start) {
block(requestHeader, request)
}
}
9 changes: 7 additions & 2 deletions hello-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@

<dependencies>
<dependency>
<groupId>org.cakesolutions</groupId>
<groupId>${project.groupId}</groupId>
<artifactId>hello-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>coroutine-service-call</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.lightbend.lagom</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import akka.Done
import akka.NotUsed
import com.lightbend.lagom.javadsl.api.ServiceCall
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry
import kotlinx.coroutines.future.await
import org.cakesolutions.coroutine.serviceCall
import org.cakesolutions.hello.api.KGreetingMessage
import org.cakesolutions.hello.api.KHelloService
import java.util.Optional
Expand All @@ -16,17 +18,13 @@ class KHelloServiceImpl @Inject constructor(private val persistentEntityRegistry
persistentEntityRegistry.register(KHelloEntity::class.java)
}

override fun hello(id: String): ServiceCall<NotUsed, String> {
return ServiceCall {
val ref = persistentEntityRegistry.refFor(KHelloEntity::class.java, id)
ref.ask(KHelloCommand.Hello(id, Optional.empty()))
}
override fun hello(id: String): ServiceCall<NotUsed, String> = serviceCall {
val ref = persistentEntityRegistry.refFor(KHelloEntity::class.java, id)
ref.ask<String, KHelloCommand.Hello>(KHelloCommand.Hello(id, Optional.empty())).await()
}

override fun useGreeting(id: String): ServiceCall<KGreetingMessage, Done> {
return ServiceCall { request ->
val ref = persistentEntityRegistry.refFor(KHelloEntity::class.java, id)
ref.ask(KHelloCommand.UserGreetingMessage(request.message))
}
override fun useGreeting(id: String): ServiceCall<KGreetingMessage, Done> = serviceCall { request ->
val ref = persistentEntityRegistry.refFor(KHelloEntity::class.java, id)
ref.ask<Done, KHelloCommand.UserGreetingMessage>(KHelloCommand.UserGreetingMessage(request.message)).await()
}
}
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>stream-impl</module>
<module>integration-tests</module>
<module>cassandra-config</module>
<module>coroutine-service-call</module>
</modules>

<build>
Expand Down Expand Up @@ -205,6 +206,16 @@
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>${kotlin.coroutines.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-jdk8</artifactId>
<version>${kotlin.coroutines.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -215,9 +226,10 @@
<play.version>2.5.10</play.version>
<akka.version>2.4.16</akka.version>
<conductr.lib.name>lagom10-conductr-bundle-lib_2.11</conductr.lib.name>
<kotlin.version>1.2.71</kotlin.version>
<kotlin.version>1.3.50</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<kotlin.compiler.languageVersion>1.2</kotlin.compiler.languageVersion>
<kotlin.compiler.languageVersion>1.3</kotlin.compiler.languageVersion>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<kotlin.coroutines.version>1.3.2</kotlin.coroutines.version>
</properties>
</project>
5 changes: 5 additions & 0 deletions stream-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>hello-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>coroutine-service-call</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.lightbend.lagom</groupId>
<artifactId>lagom-javadsl-server_2.11</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ package org.cakesolutions.stream.impl
import akka.NotUsed
import akka.stream.javadsl.Source
import com.lightbend.lagom.javadsl.api.ServiceCall
import org.cakesolutions.coroutine.serviceCall
import org.cakesolutions.hello.api.KHelloService
import org.cakesolutions.stream.api.KStreamService
import java.util.concurrent.CompletableFuture.completedFuture
import javax.inject.Inject


class KStreamServiceImpl @Inject
constructor(private val helloService: KHelloService) : KStreamService {
override fun stream(): ServiceCall<Source<String, NotUsed>, Source<String, NotUsed>> {
return ServiceCall { hellos ->
completedFuture(hellos.mapAsync(8){ name ->
helloService.hello(name)()
})
override fun stream(): ServiceCall<Source<String, NotUsed>, Source<String, NotUsed>> = serviceCall { hellos ->
hellos.mapAsync(8) { name ->
helloService.hello(name)()
}
}
}