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;
}
}