Annotation Type MessageListener


@Retention(CLASS) @Target(METHOD) public @interface MessageListener

Receives the messages of a destination: the method runs once per message, on a consumer thread of the server, and the message is acknowledged when it returns.

@Component
public class Shipping {
    @MessageListener(value = "orders.placed", group = "shipping", concurrency = 4)
    public void placed(Order order) { ... }
}

The method takes one parameter: the payload, read as JSON by a codec the build generates for its type -- or a String, a byte[], or the whole Message. A method that throws has the message delivered again after a backoff, and after cn1.messaging.retry.max-attempts deliveries it goes to <destination>.dlq. Delivery is at least once, so a listener should be safe to run twice for one message.

The build finds every listener and writes the code that subscribes it when the server starts and stops it when the server stops; there is no reflection. A destination may name a setting, ${orders.topic}, which is read from the configuration when the server starts.

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    Consumer threads.
    The same as value().
    The consumer group; subscriptions of one group share the messages, and each group gets every one.
    The destinations: queues, topics or streams.
  • Element Details

    • value

      String[] value
      The destinations: queues, topics or streams. At least one.
      Default:
      {}
    • destinations

      String[] destinations
      The same as value().
      Default:
      {}
    • group

      String group
      The consumer group; subscriptions of one group share the messages, and each group gets every one. Empty means spring.application.name, or default without one.
      Default:
      ""
    • concurrency

      int concurrency
      Consumer threads.
      Default:
      1