1. 程式人生 > >springboot結合redis實現redis訂閱釋出模式

springboot結合redis實現redis訂閱釋出模式

redis訂閱釋出模式:

Redis提供了釋出訂閱功能,可以用於訊息的傳輸,Redis的釋出訂閱機制包括三個部分,釋出者,訂閱者和Channel。 

釋出者和訂閱者都是Redis客戶端,Channel則為Redis伺服器端,釋出者將訊息傳送到某個的頻道,訂閱了這個頻道的訂閱者就能接收到這條訊息。Redis的這種釋出訂閱機制與基於主題的釋出訂閱類似,Channel相當於主題。

實現功能:

通過網頁訪問一個網址的時候實現redis在某個頻道的的釋出,同時訂閱了該頻道的redis訂閱者會做出相應的反應。

要想讓redis在web容器開啟時就一直處於訂閱狀態,考慮通過listener來實現:

Listener:

@Configuration
@WebListener
public class MyListenerOnServlet implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext容器初始化了。。。");
        Thread threadOnRedis = new TestSubscribe();
        threadOnRedis.start();

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("ServletContext容器銷燬了。。。");
    }

}
TestSubScribe執行緒類:
public class TestSubscribe extends Thread {


    @Override
    public void run() {
        JedisShardInfo jedisShardInfo = new JedisShardInfo("127.0.0.1", 6379);
        jedisShardInfo.setPassword("admin");
        Jedis jedis = new Jedis(jedisShardInfo);
        RedisMsgPubSubListener listener = new RedisMsgPubSubListener();
        jedis.subscribe(listener, "redisChatTest");
        System.out.println("不阻塞");
    }
}

RedisMsgPubSubListener類:

public class RedisMsgPubSubListener extends JedisPubSub {
    @Override
    public void unsubscribe() {
        super.unsubscribe();
    }

    @Override
    public void unsubscribe(String... channels) {
        super.unsubscribe(channels);
    }

    @Override
    public void subscribe(String... channels) {
        super.subscribe(channels);
    }

    @Override
    public void psubscribe(String... patterns) {
        super.psubscribe(patterns);
    }

    @Override
    public void punsubscribe() {
        super.punsubscribe();
    }

    @Override
    public void punsubscribe(String... patterns) {
        super.punsubscribe(patterns);
    }

    @Override
    public void onMessage(String channel, String message) {
        System.out.println("channel:" + channel + "receives message :" + message);
//        this.unsubscribe();
    }

    @Override
    public void onPMessage(String pattern, String channel, String message) {

    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);
    }

    @Override
    public void onPUnsubscribe(String pattern, int subscribedChannels) {

    }

    @Override
    public void onPSubscribe(String pattern, int subscribedChannels) {

    }

    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);
    }


}


通過controller的訪問來觸發redis的釋出:

Controller:

@RequestMapping("/t07")
    public String test07(@RequestParam("msg") String s){
        testService.testRedisForMsg(s);
        return "完成";
    }
Service:
@Service
public class TestService {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private UserMapper userMapper;

    

    public void testRedisForMsg(String msg){
        Jedis jedis = jedisPool.getResource();
        try{
            jedis.publish("redisChatTest", msg);
        }catch (Exception e){
            System.out.println("釋出出錯:"+e.toString());
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }



}
jedispool配置就不解釋了。差不多就是這樣,在瀏覽器訪問觸發redis釋出的時候,本例的響應是在控制檯輸出釋出資訊。

輸出資訊:

channel:redisChatTestreceives message :lvelvelve
相應可以自己決定,感覺又學到了好玩的東西。