{
  "schema_version": 2,
  "id": "integrate/spring-framework-cache/pub-sub",
  "title": "Publish and subscribe to messages with Spring",
  "url": "https://redis.io/docs/latest/integrate/spring-framework-cache/pub-sub/",
  "summary": "Send and receive Redis pub/sub messages in a Spring application.",
  "tags": [
    "docs",
    "integrate",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "client"
  ],
  "last_updated": "2026-11-17T15:08:03Z",
  "page_type": "content",
  "content_hash": "9106bf352207417e1dd46b3988163736f171477b504b2fef5850143edc649003",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Spring Data Redis supports Redis [pub/sub](https://redis.io/docs/latest/develop/pubsub) with\ntwo parts: the template publishes messages, and a message listener container\nreceives them. See the\n[pub/sub use case](https://redis.io/docs/latest/develop/use-cases/pub-sub) for when to\nchoose pub/sub."
    },
    {
      "id": "subscribe-to-channels",
      "title": "Subscribe to channels",
      "role": "content",
      "text": "Define a `RedisMessageListenerContainer` bean. Add one listener for each channel or\npattern you want to receive. This example subscribes to the `news` channel and to every\nchannel that matches `alerts:*`:\n\n[code example]\n\nThe container subscribes when the application starts and delivers each message to your\nlistener on a separate thread.\n\n> [!NOTE]\n> If you use a `MessageListenerAdapter` to call a method on your own class, the\n> second `String` argument of the handler method is the pattern that matched, not the\n> channel. For a pattern subscription such as `alerts:*`, the adapter passes\n> `alerts:*` rather than `alerts:disk`. Read the channel from `message.getChannel()`\n> in a `MessageListener`, as in the example."
    },
    {
      "id": "publish-messages",
      "title": "Publish messages",
      "role": "content",
      "text": "Call `convertAndSend` on the template:\n\n[code example]\n\nWith the listener container above, the listener receives `news: hello` and\n`alerts:disk: full`."
    },
    {
      "id": "delivery-guarantees",
      "title": "Delivery guarantees",
      "role": "content",
      "text": "Redis pub/sub is fire-and-forget. A message published while your application isn't\nsubscribed is lost. If you need to keep messages until a consumer processes them, use a\n[stream](https://redis.io/docs/latest/integrate/spring-framework-cache/streams) instead."
    }
  ],
  "examples": [
    {
      "id": "subscribe-to-channels-ex0",
      "language": "java",
      "code": "@Configuration\npublic class PubSubConfig {\n    @Bean\n    RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory) {\n        MessageListener listener = (message, pattern) -> {\n            String channel = new String(message.getChannel(), StandardCharsets.UTF_8);\n            String body = new String(message.getBody(), StandardCharsets.UTF_8);\n            System.out.println(channel + \": \" + body);\n        };\n\n        RedisMessageListenerContainer container = new RedisMessageListenerContainer();\n        container.setConnectionFactory(connectionFactory);\n        container.addMessageListener(listener, new ChannelTopic(\"news\"));\n        container.addMessageListener(listener, new PatternTopic(\"alerts:*\"));\n        return container;\n    }\n}",
      "section_id": "subscribe-to-channels"
    },
    {
      "id": "publish-messages-ex0",
      "language": "java",
      "code": "template.convertAndSend(\"news\", \"hello\");\ntemplate.convertAndSend(\"alerts:disk\", \"full\");",
      "section_id": "publish-messages"
    }
  ]
}
