Redis订阅模式示例

1、Maven引入

        <!-- Redis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

2、订阅频道方法

package com.seata.storage.redis.sub;
import com.seata.storage.redis.service.RedisService;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
/**
 * 订阅频道
 */
public class SubThread extends Thread{
    private final RedisService redisService;
    private final String redisPoolKey;
    private final String channel;
    private final Subscriber subscriber;

    public SubThread(RedisService redisService,Subscriber subscriber,String channel,String redisPoolKey) {
        super("SubThread");
        this.redisService = redisService;
        this.subscriber = subscriber;
        this.channel = channel;
        this.redisPoolKey = redisPoolKey;
    }

    @Override
    public void run() {
        System.out.println(String.format("subscribe redis, channel %s, thread will be blocked", this.channel));
        Jedis jedis = null;
        try {
            //取出一个连接
            if(StringUtils.isNotBlank(this.redisPoolKey)) jedis = this.redisService.getRedisClient(this.redisPoolKey);
            else jedis = this.redisService.getRedisClient();
            //通过subscribe 的api去订阅,入参是订阅者和频道名
            jedis.subscribe(this.subscriber, this.channel);
        } catch (Exception e) {
            System.out.println(String.format("subsrcibe channel error, %s", e));
        } finally {
            this.redisService.returnResource(jedis);
        }
    }
}

3、订阅者
具体业务实现在此编写

package com.seata.storage.redis.sub;
import redis.clients.jedis.JedisPubSub;
/**
 * 订阅者
 * 订阅者需要继承JedisPubSub,来重写它的三个方法
 */
public class Subscriber extends JedisPubSub {
    public Subscriber(){}
    /**
     * 收到消息会调用
     * @param channel
     * @param message
     */
    @Override
    public void onMessage(String channel, String message) {
        System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message));
    }
    /**
     * 订阅了频道会调用
     * @param channel
     * @param subscribedChannels
     */
    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",channel, subscribedChannels));
    }
    /**
     * 取消订阅会调用
     * @param channel
     * @param subscribedChannels
     */
    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",channel, subscribedChannels));
    }
}

4、测试

package com.seata.storage.controller;
import com.seata.storage.redis.service.RedisService;
import com.seata.storage.redis.sub.SubThread;
import com.seata.storage.redis.sub.Subscriber;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

@RestController
@RequestMapping("/redis")
public class PubSubController {

    @Autowired
    private RedisService redisService;

    /**
     * 订阅
     * @param channel
     * @param redisPoolKey
     * @return
     */
    @RequestMapping("/sub")
    public Boolean sub(String channel,String redisPoolKey){
        Subscriber subscriber = new Subscriber();
        SubThread subThread = new SubThread(redisService,subscriber,channel,redisPoolKey);
        subThread.start();
        return true;
    }

    /**
     * 发布
     * 向channel的频道上推送消息
     * @param channel
     * @param redisPoolKey
     * @return
     */
    @RequestMapping("/pub")
    public Boolean pub(String channel,String redisPoolKey,String msg){
        Jedis jedis = null;
        try {
            if(StringUtils.isNotBlank(redisPoolKey)) jedis = this.redisService.getRedisClient(redisPoolKey);
            else jedis = this.redisService.getRedisClient();
            // 向channel的频道上推送消息
            jedis.publish(channel, msg);
        } catch (Exception e) {
            System.out.println(String.format("publish channel error, %s", e));
        } finally {
            this.redisService.returnResource(jedis);
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值