java-runtime


License

License

MIT
Categories

Categories

Java Languages Ant Build Tools gRPC Net Networking
GroupId

GroupId

org.lyranthe.fs2-grpc
ArtifactId

ArtifactId

java-runtime_2.11
Last Version

Last Version

0.6.0
Release Date

Release Date

Type

Type

jar
Description

Description

java-runtime
java-runtime
Project URL

Project URL

https://github.com/fiadliel/fs2-grpc
Project Organization

Project Organization

org.lyranthe.fs2-grpc
Source Code Management

Source Code Management

https://github.com/fiadliel/fs2-grpc

Download java-runtime_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/org.lyranthe.fs2-grpc/java-runtime_2.11/ -->
<dependency>
    <groupId>org.lyranthe.fs2-grpc</groupId>
    <artifactId>java-runtime_2.11</artifactId>
    <version>0.6.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.lyranthe.fs2-grpc/java-runtime_2.11/
implementation 'org.lyranthe.fs2-grpc:java-runtime_2.11:0.6.0'
// https://jarcasting.com/artifacts/org.lyranthe.fs2-grpc/java-runtime_2.11/
implementation ("org.lyranthe.fs2-grpc:java-runtime_2.11:0.6.0")
'org.lyranthe.fs2-grpc:java-runtime_2.11:jar:0.6.0'
<dependency org="org.lyranthe.fs2-grpc" name="java-runtime_2.11" rev="0.6.0">
  <artifact name="java-runtime_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.lyranthe.fs2-grpc', module='java-runtime_2.11', version='0.6.0')
)
libraryDependencies += "org.lyranthe.fs2-grpc" % "java-runtime_2.11" % "0.6.0"
[org.lyranthe.fs2-grpc/java-runtime_2.11 "0.6.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12
co.fs2 : fs2-core_2.11 jar 2.1.0
org.typelevel : cats-effect_2.11 jar 2.0.0
io.grpc : grpc-core jar 1.24.0

test (3)

Group / Artifact Type Version
io.grpc : grpc-netty-shaded jar 1.24.0
org.typelevel : cats-effect-laws_2.11 jar 2.0.0
io.monix : minitest_2.11 jar 2.7.0

Project Modules

There are no modules declared in this project.

fs2-grpc - gRPC implementation for FS2/cats-effect

Join the chat at https://gitter.im/fs2-grpc/Lobby Build Status Latest version

SBT configuration

project/plugins.sbt:

addSbtPlugin("org.lyranthe.fs2-grpc" % "sbt-java-gen" % "<latest-version>")

build.sbt:

enablePlugins(Fs2Grpc)

Protocol buffer files

The protobuf files should be stored in the directory <project_root>/src/main/protobuf.

Multiple projects

If the generated code is used by multiple projects, you may build the client/server code in a common project which other projects depend on. For example:

lazy val protobuf =
  project
    .in(file("protobuf"))
    .enablePlugins(Fs2Grpc)

lazy val client =
  project
    .in(file("client"))
    .dependsOn(protobuf)

lazy val server =
  project
    .in(file("server"))
    .dependsOn(protobuf)

Creating a client

A ManagedChannel is the type used by grpc-java to manage a connection to a particular server. This library provides syntax for ManagedChannelBuilder which creates an FS2 Stream which can manage the shutdown of the channel, by calling .stream[F] where F has an instance of the Sync typeclass. This implementation will do a drain of the server, and attempt to shut down the server, forcefully closing after 30 seconds. An example of the syntax is:

val managedChannelStream: Stream[IO, ManagedChannel] =
  ManagedChannelBuilder
    .forAddress("127.0.0.1", 9999)
    .stream[IO]

The syntax also offers the method streamWithShutdown which takes a function ManagedChannel => F[Unit] which is used to manage the shutdown. This may be used where requirements before shutdown do not match the default behaviour.

The generated code provides a method stub[F] (for any F which has a Sync instance), and it takes a parameter of type ManagedChannel. It returns an implementation of the service (in a trait), which can be used to make calls.

def runProgram(stub: MyFs2Grpc[IO]): IO[Unit] = ???

for {
  managedChannel <- managedChannelStream
  client = MyFs2Grpc.stub[IO](managedChannel)
  _ <- Stream.eval(runProgram(client))
} yield ()

If a ManagedChannelProvider isn't found on your classpath you may receive an error similar to

io.grpc.ManagedChannelProvider$ProviderNotFoundException: No functional channel service provider found. Try adding a dependency on the grpc-okhttp or grpc-netty artifact

This can be fixed by adding a dependency to the netty provider e.g.

libraryDependencies += "io.grpc" % "grpc-netty-shaded" % scalapb.compiler.Version.grpcJavaVersion

Creating a server

The generated code provides a method bindService[F] (for any F which has a Sync instance), and it takes an implementation of the service (in a trait), which is used to serve responses to RPC calls. It returns a ServerServiceDefinition which is given to the server builder when setting up the service.

A Server is the type used by grpc-java to manage the server connections and lifecycle. This library provides syntax for ServerBuilder, which mirrors the pattern for the client. An example is:

import org.lyranthe.fs2_grpc.java_runtime.implicits._

val helloService: ServerServiceDefinition = MyFs2Grpc.bindService(new MyImpl())

ServerBuilder
  .forPort(9999)
  .addService(helloService)
  .addService(ProtoReflectionService.newInstance()) // reflection makes lots of tooling happy
  .stream[IO] // or for any F: Sync
  .evalMap(server => IO(server.start())) // start server
  .evalMap(_ => IO.never) // server now running

Code generation options

To alter code generation, you can set some flags with scalapbCodeGeneratorOptions, e.g.:

scalapbCodeGeneratorOptions += CodeGeneratorOption.FlatPackage

The full set of options available are:

  • CodeGeneratorOption.FlatPackage - If true, the compiler does not append the proto base file name
  • CodeGeneratorOption.JavaConversions - Enable Java conversions for protobuf
  • CodeGeneratorOption.Grpc (included by default) - generate grpc bindings based on Observables
  • CodeGeneratorOption.Fs2Grpc (included by default) - generate grpc bindings for FS2/cats
  • CodeGeneratorOption.SingleLineToProtoString - toProtoString generates single line
  • CodeGeneratorOption.AsciiFormatToString - toString uses toProtoString functionality

Pass additional protoc options

PB.protocOptions in Compile := Seq("-xyz")

Versions

Version
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.5.0-RC1
0.5.0-M1
0.4.1
0.4.0
0.4.0-RC1
0.4.0-M6
0.4.0-M5
0.4.0-M4
0.4.0-M3
0.4.0-M2
0.4.0-M1
0.3.1
0.3.0
0.2.0