Class ProtobufCodec

java.lang.Object
org.acemq.amqp.codec.protobuf.ProtobufCodec
All Implemented Interfaces:
Codec

public final class ProtobufCodec extends Object implements Codec
Reads and writes Protocol Buffers.

Like Avro, and for the same reason, there is no asProtobuf(). Protobuf bytes say nothing about which message they are: field numbers and wire types, and no names or type. A reader must be told what to parse them as, so this is reached through publisher.as(ProtobufCodec.of(OrderPlaced.parser())).

One codec per message type, which is not a limitation so much as a description of how protobuf is used: a destination carries one kind of message, and that is what makes the bytes interpretable at all. A destination that needs to carry several has to say which is which, and the protobuf answer to that is a wrapper message with a oneof — a decision about the schema rather than about the transport, so it belongs in the .proto file.

Evolution is the format's own affair and works without anything here: readers ignore fields they do not know and use defaults for fields that are absent, provided nobody reuses a field number. Nothing in this codec can protect against a reused number, and nothing can detect it either — the bytes are valid and mean something else. Reserve retired numbers in the schema.

  • Method Details

    • of

      public static <T extends com.google.protobuf.Message> ProtobufCodec of(com.google.protobuf.Parser<T> parser)
      Type Parameters:
      T - message type
      Parameters:
      parser - the generated type's parser, as in OrderPlaced.parser()
      Returns:
      a codec for that one message type
    • of

      public static <T extends com.google.protobuf.Message> ProtobufCodec of(Class<T> type)
      The same thing, given the class instead of its parser.
      Type Parameters:
      T - message type
      Parameters:
      type - a class generated by protoc
      Returns:
      a codec for that one message type
    • contentType

      public String contentType()
      Specified by:
      contentType in interface Codec
      Returns:
      the content type this codec produces, for example application/json
    • encode

      public byte[] encode(Object payload)
      Specified by:
      encode in interface Codec
      Parameters:
      payload - object to encode; never null
      Returns:
      encoded bytes
    • decode

      public <T> T decode(byte[] body, Class<T> target)
      Specified by:
      decode in interface Codec
      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      Returns:
      the decoded payload
    • canDecode

      public boolean canDecode(@Nullable String contentType)
      Specified by:
      canDecode in interface Codec
      Parameters:
      contentType - content type of a received message, or null when the sender did not set one
      Returns:
      whether this codec can decode it
    • toString

      public String toString()
      Overrides:
      toString in class Object