package jsyf.com.redis.cluster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPubSub;
import javax.annotation.PostConstruct;
@Component
public class Subscription {
@Autowired
private JedisCluster jedisCluster;
@PostConstruct
public void startSub() {
new Thread(() -> {
jedisCluster.subscribe(new JedisPubSub() {
/**
* 完全匹配-接受消息
* @param channel 管道
* @param message 消息
*/
@Override
public void onMessage(String channel, String message) {
System.out.println("onMessagex:" + message);
}
}, "WEIXIN");
}).start();
}
}
@Autowired
private JedisCluster jedisCluster;
public JSONObject sendMsgToUser(String jsonData) throws Exception {
Long count = jedisCluster.publish("WEIXIN", "您有一条微信技术文章.");
System.out.println("count = " + count);
return new JSONObject();
}