基于redis实现简单的消息订阅发布

demo地址

装载配置

@Slf4j
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisServiceImpl redisService() {
        return new RedisServiceImpl();
    }

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListener messageListener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        List topicList = new ArrayList<>();
        topicList.add(new ChannelTopic("a"));
        topicList.add(new ChannelTopic("b"));
        container.addMessageListener(messageListener, topicList);
        return container;
    }
  }

编写工具类

@Slf4j
public class RedisServiceImpl implements MessageListener {

    /**
     * 默认过期时长,单位:秒
     */
    public static final long DEFAULT_EXPIRE = 60 * 60 * 24;

    /**
     * 不设置过期时长
     */
    public static final long NOT_EXPIRE = -1;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ValueOperations valueOperations;

    @Autowired
    private List redisMessageConsumers;

    private  T run(RedisCallBack redisCallback) {
        try {
            return redisCallback.doInRedis(redisTemplate);
        } catch (Exception e) {
            log.error("redis error", e);
            return null;
        }
    }

    /**
     * pub消息
     */
    @Override
    public void convertAndSend(String channel, Object message) {
        run(redisTemplate -> {
            redisTemplate.convertAndSend(channel, message);
            return true;
        });
    }

    /**
     * 订阅redis消息
     */
    @Override
    public void onMessage(Message message, @Nullable byte[] bytes) {
        System.out.println("=================");
        byte[] body = message.getBody();
        byte[] messageChannel = message.getChannel();

        String bodySting = new String(body);
        String channel = new String(messageChannel);
        String byteStr = new String(bytes);

        System.out.println(bodySting);
        System.out.println(channel);
        System.out.println(byteStr);

        System.out.println("=================");

        if (CollectionUtils.isNotEmpty(redisMessageConsumers)) {
            for (RedisConsumer consumer : redisMessageConsumers) {
                if (consumer.support(channel)) {//策略模式
                    consumer.handle(message, bytes);
                }
            }
        }
    }
}

消费消息

@Slf4j
@Service
public class RedisMessageConsumer1 implements RedisConsumer {

    @Override
    public Boolean support(String messageType) {
        return Objects.equals("a", messageType);
    }

    @Override
    public void handle(Message message, byte[] bytes) {
        System.out.println("我是消费者1");
    }
}
@Slf4j
@Service
public class RedisMessageConsumer2 implements RedisConsumer {

    @Override
    public Boolean support(String messageType) {
        return Objects.equals("b", messageType);
    }

    @Override
    public void handle(Message message, byte[] bytes) {
        System.out.println("我是消费者2");
    }
}

发布消息

@Slf4j
@RestController
@AllArgsConstructor
public class PubMessageController {

    private RedisService redisService;

    @GetMapping("/a")
    public R a() {
        redisService.convertAndSend("a", "ae67");
        return R.SUCCESS();
    }

    @GetMapping("/b")
    public R b() {
        redisService.convertAndSend("b", "b");
        return R.SUCCESS();
    }
}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(基于redis实现简单的消息订阅发布)