-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathZIOgRPCClient.scala
83 lines (67 loc) · 3.22 KB
/
ZIOgRPCClient.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.features.grpc
import com.features.zio.connectorManager.ConnectorManagerGrpc.ConnectorManagerStub
import com.features.zio.connectorManager.{ConnectorInfoDTO, ConnectorManagerGrpc}
import io.grpc.{ManagedChannel, ManagedChannelBuilder}
import zio.Runtime.{default => Main}
import zio.{Has, ULayer, ZIO, ZLayer}
object ZIOgRPCClient extends App {
val process = runServerFromAnotherJVM()
/**
* Experiment of how we can run gRPC between client and server from different JVM and still obtain a very
* good performance, just like if both were running under the same JVM.
*/
private def runServerFromAnotherJVM(): Process = {
val path = "/Users/nb38tv/Developer/projects/reactiveScala/zio/target"
val JavaCommand = s"java -cp $path/zio-1.0-SNAPSHOT-jar-with-dependencies.jar com.features.grpc.ZIOgRPCServer"
val process: Process = Runtime.getRuntime.exec(JavaCommand)
println(s"Service process alive:${process.isAlive}")
process
}
Thread.sleep(2000)
/**
* Implementation/Behavior of ZLayer as dependency of Channel to be injected in the program
*/
val channelDependency: ULayer[Has[ManagedChannel]] = ZLayer.succeed {
ManagedChannelBuilder.forAddress("localhost", 9999)
.usePlaintext()
.asInstanceOf[ManagedChannelBuilder[_]]
.build()
}
/**
* Implementation/Behavior of ZLayer as dependency of [ManagedChannel => ConnectorManagerStub] function to be injected in the program
* and obtain the [ConnectorManagerStub] receiving the [ManagedChannel]
*/
val connectorManagerStubDependency: ULayer[Has[ManagedChannel => ConnectorManagerStub]] = ZLayer.succeed {
channel: ManagedChannel => ConnectorManagerGrpc.stub(channel)
}
/**
* DSL/Structure of how to obtain channel from the dependency
*/
def getChannel: ZIO[Has[ManagedChannel], Nothing, ManagedChannel] = ZIO.access(has => has.get)
/**
* DSL/Structure of how to obtain ConnectorManagerStub from the dependency passing ManagedChannel
*/
def getConnectorManagerStub(channel: ManagedChannel): ZIO[Has[ManagedChannel => ConnectorManagerStub], Nothing, ConnectorManagerStub] =
ZIO.access(_.get.apply(channel))
/**
* Client gRPC program that receive as dependency the channel and ConnectorManagerStub to make the request against the server.
*/
private val clientProgram: ZIO[Has[ManagedChannel] with Has[ManagedChannel => ConnectorManagerStub], Throwable, Unit] = (for {
channel <- getChannel
connectorManagerStub <- getConnectorManagerStub(channel)
request <- ZIO.effect(ConnectorInfoDTO(connectorName = "Rest", requestInfo = "READ"))
response <- ZIO.fromFuture(_ => connectorManagerStub.connectorRequest(request))
_ <- ZIO.succeed(println(s"Response: ${response.message}"))
} yield ()).catchAll(t => {
println(s"Error running ZIO gRPC client. Caused by $t")
ZIO.fail(t)
})
/**
* ZLayer with all dependencies together to be passed to the program.
*/
val dependencies: ZLayer[Any, Nothing, Has[ManagedChannel] with Has[ManagedChannel => ConnectorManagerStub]] =
channelDependency ++ connectorManagerStubDependency
Main.unsafeRun(clientProgram.provideCustomLayer(dependencies))
//Kill the other JVM process, and the server running with it.
process.destroy()
}