ScalaPB integration

ScalaPB is a protocol buffer compiler (protoc) plugin for Scala. It will generate Scala case classes, parsers and serializers for your protocol buffers. ScalaPB also supports a thin wrapper around grpc-java, and provides you with an interface that is based on Scala's standard library Future, while streaming is based on the Observer pattern. If you integrate ScalaPB with Armeria, you can leverage the followings:

  • Using both gRPC server and gRPC client features such as gRPC-over-HTTP/1 and gRPC-Web protocol powered by Armeria
  • Browsing the list of available RPC operations and invoking a service operation via a web console
  • Converting either a Protocol Buffers or JSON to and from an scalapb.GeneratedMessage in annotated service

First, you need the armeria-scalapb_2.12 or armeria-scalapb_2.13 dependency to use the above features:

libraryDependencies += "com.linecorp.armeria" %% "armeria-scalapb" % "1.3.0"

Running a gRPC service

You need to register a ScalaPB gRPC stub to a GrpcService using a GrpcServiceBuilder and add it to the ServerBuilder:

import com.linecorp.armeria.common.scalapb.ScalaPBJsonMarshaller
import com.linecorp.armeria.server.Server
import com.linecorp.armeria.server.grpc.GrpcService

// Creates GrpcService with your gRPC stub generated by ScalaPB.
val grpcService =
        // Add your ScalaPB gRPC stub using `bindService()`
          new YourServiceImpl,
        // Register `ScalaPBJsonMarshaller` for supporting gRPC JSON format.
        .jsonMarshallerFactory(_ => ScalaPBJsonMarshaller())

// Creates Armeria Server for ScalaPB gRPC stub.
      // Add DocService for browsing the list of gRPC services and
      // invoking a service operation from a web form.
      // See for more information.
      .serviceUnder("/docs", new DocService())

Please see gRPC service for more information.

Calling a gRPC service

You can also call a gRPC service using a ScalaPB gRPC client. ScalaPbJsonMarshaller should be registered with GrpcClientOptions.GRPC_JSON_MARSHALLER_FACTORY to support gRPC JSON serialization format.

import com.linecorp.armeria.client.Clients
import com.linecorp.armeria.client.grpc.GrpcClientOptions
import com.linecorp.armeria.common.scalapb.ScalaPbJsonMarshaller

val client =
         // Register 'ScalaPBJsonMarshaller' for enabling gRPC JSON serialization format
                                  .newValue(_ => ScalaPbJsonMarshaller()))

val request = HelloRequest("Armerian World")
val reply = helloService.hello(request)
assert(reply.message == "Hello, Armerian World!")

Please see gRPC client for more information.

Supporting ScalaPB in annotated services.

Converting an HTTP request to a ScalaPB's GeneratedMesage

  • A Protocol Buffers is automatically converted to a GeneratedMessage only when the content type is one of followings:
    • application/probuf
    • application/octet-stream
    • application/x-protobuf
    • no media type negotiated
  • A JSON is automatically converted to a GeneratedMessage only when the content type is either application/json or ends with +json.

If you have the following proto file,

syntax = "proto3";
package com.example.testing;
option java_package = "com.example.testing";

message SimpleRequest {
  string payload = 1;
  int32 size = 2;

message SimpleResponse {
  string message = 1;
  int32 status = 2;

the SimpleRequest generated by ScalaPB could be used for a parameter of your service method.

import com.example.testing.SimpleRequest
import com.linecorp.armeria.server.annotation.{ConsumesJson, ConsumesProtobuf, Post}

class GreetingService {
  // If a content type is not set, Protoco Buffers' parser is used by default.
  def noContentType(request: SimpleRequest): String = s"Hello, ${request.payload}!"

  // Convert the Protocol Buffers in an HTTP payload into the 'SimpleRequest'
  def consumeProtobuf(request: SimpleRequest): String = s"Hello, ${request.payload}!"

  // Convert the JSON in an HTTP payload into a 'SimpleRequest'
  def consumeJson(request: SimpleRequest): String = "Hello, Armeria!"

Returning a ScalaPB's GeneratedMesage

A GeneratedMesage can be converted to either Protocol Buffers or JSON.

import com.example.testing.SimpleResponse
import scala.concurrent.Future

class GreetingService {

  // Convert 'SimpleResponse' into Protocol Buffers wire format
  def produceProtobuf: SimpleResponse = SimpleResponse("Hello, Armeria!")

  // Convert 'SimpleResponse' into JSON format
  def produceJson: SimpleResponse = SimpleResponse("Hello, Armeria!")

  // Convert 'SimpleResponse' into Protocol Buffers wire format with Scala Future
  def produceProtobufFuture: Future[SimpleResponse] =
    Future { SimpleResponse("Hello, Armeria!") }

However, a sequence of GeneratedMesages can be only converted to JSON array because Protocol Buffers wire format is not self-delimiting. The following collection types can be converted to JSON array:

  • scala.List
  • scala.Vector
  • scala.Set
  • Reactive Stream Publisher
  • java.util.List
  • java.util.Set

Injecting an ExecutionContext to an annotated service method

An ExecutionContext could be automatically injected as a method parameter to execute an asynchronous service on top of an Armeria's event loop or blocking task executor.

import com.linecorp.armeria.server.ServiceRequestContext
import com.linecorp.armeria.server.annoation.Blocking
import scala.concurrent.Future

class MyAsyncService {
  def asyncService(req: SimpleRequest)(implicit ec: ExecutionContext): Future[String] =
    Future {
      // This callback will be executed in an Armeria's event loop
      assert(ServiceRequestContext.current() != null)
      "Hello, Armeria!"

  def blockingAsyncService(req: SimpleRequest)(implicit ec: ExecutionContext): Future[String] =
    Future {
      // This callback will be executed in an Armeria's blocking task executor
      assert(ServiceRequestContext.current() != null)
      // Perform a long running task
      "Hello, Armeria!"