Home > AI > Backend > SpringBoot > Redis >

SpringBoot implements Pub and Sub for Redis

Type 1: SpringBoot Application

Step 1: install dependencies

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>

Step 2: codes

RedisConfig.java

@Configuration
@ComponentScan("com.example.demo.redis")
@EnableRedisRepositories(basePackages = "com.example.demo.redis")
@PropertySource("classpath:application.properties")
public class RedisConfig {


    /**
     * Use the Jedis Redis Client to connect with Redis Server
     * @return
     */
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }


    /**
     * For querying data with a custom repository.
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
        return template;
    }

    @Bean
    ChannelTopic topic() {
        return new ChannelTopic("chat");
    }

    @Bean
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter(new RedisMessageSubscriber());
    }

    @Bean
    RedisMessageListenerContainer redisContainer() {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(jedisConnectionFactory());
        container.addMessageListener(messageListener(), topic());
        return container;
    }

    @Bean
    MessagePublisher redisPublisher() {
        return new RedisMessagePublisher(redisTemplate(), topic());
    }
}

RedisMessagePublisher.java

@Service
@NoArgsConstructor
@AllArgsConstructor
public class RedisMessagePublisher {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ChannelTopic topic;

    
    public void publish(final String message) {
        redisTemplate.convertAndSend(topic.getTopic(), message);
    }
}

RedisMessageSubscriber.java

@Service
public class RedisMessageSubscriber implements MessageListener {

    public static List<String> messageList = new ArrayList<String>();

    public void onMessage(final Message message, final byte[] pattern) {
        messageList.add(message.toString());
        System.out.println("Message received: " + new String(message.getBody()));
    }
}

DemoApplication.java

@RestController
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {

	    SpringApplication.run(DemoApplication.class, args);

	}

	@Autowired
    RedisMessagePublisher redisMessagePublisher;

	@GetMapping("/send")
    public void sendPub() {
        redisMessagePublisher.publish("hello, Shark send you a message");
    }
}

Type 2: redis-cli

You can open three terminals.

terminal numusagecommand
subscriber 1receive messagesubscribe {channel}
subscriber 2receive messagesubscribe {channel}
publisherpublish {channel} {message}

Leave a Reply