Interface Codec

All Known Implementing Classes:
AvroCodec, BytesCodec, ClaimCheckCodec, CompositeCodec, EncryptedCodec, JsonCodec, ProtobufCodec, StringCodec, TomlCodec, XmlCodec, YamlCodec

public interface Codec
Turns payloads into bytes and back.

Selected by content type, so a queue can carry more than one representation and a consumer can be migrated to a new format without a flag day.

  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canDecode(@Nullable String contentType)
     
     
    <T> T
    decode(byte[] body, Class<T> target)
     
    default <T> T
    decode(byte[] body, Class<T> target, @Nullable String contentType)
    Decodes a received message, told what the sender said it was.
    byte[]
    encode(Object payload)
     
  • Method Details

    • contentType

      String contentType()
      Returns:
      the content type this codec produces, for example application/json
    • encode

      byte[] encode(Object payload)
      Parameters:
      payload - object to encode; never null
      Returns:
      encoded bytes
      Throws:
      AceMqException - if the payload cannot be encoded
    • decode

      <T> T decode(byte[] body, Class<T> target)
      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      Returns:
      the decoded payload
      Throws:
      AceMqException - if the bytes cannot be decoded, which the engine treats as a poison message rather than a retryable failure
    • decode

      default <T> T decode(byte[] body, Class<T> target, @Nullable String contentType)
      Decodes a received message, told what the sender said it was.

      The engine calls this one, because a codec that dispatches between several formats cannot choose without knowing the content type. A codec that handles a single format has no use for it and need not override this.

      Type Parameters:
      T - target type
      Parameters:
      body - encoded bytes
      target - type to produce
      contentType - content type the message arrived with, or null when the sender did not set one
      Returns:
      the decoded payload
      Throws:
      AceMqException - if the bytes cannot be decoded
    • canDecode

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