redis 发布订阅比较轻量,适合用来发布订阅简单场景的消息。
整个例子包括:
1、redis线程池配置:RedisJedisPoolConfig
2、消息接收器配置:MsgReceiverConfig
3、消息接收器:MsgReceiver、MsgReceiverImpl
4、消息发送例子:TestSandTopic1、TestSandTopic2
1、redis线程池配置:RedisJedisPoolConfig
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.lang.reflect.Field;
/**
* RedisUtils
*
* @Author XZ.Tan
* @Date: 2021/1/5 14:40
* @Version 1.0
*/
@Component
public class RedisJedisPoolConfig implements InitializingBean {
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
private static Integer MaxTotal = 100;
private static Integer MaxIdle = 100;
private static Integer MinIdle = 0;
private static Integer MaxWaitMillis = 3000;
private static boolean TestOnBorrow = false;
private JedisPool jedisPool;
public Jedis getJedis() throws Exception {// 记的关闭jedis
if (jedisPool != null) {
return jedisPool.getResource();
} else {
afterPropertiesSet();
return jedisPool.getResource();
}
}
@Override
public void afterPropertiesSet() throws Exception {
Field poolField = ReflectionUtils.findField(JedisConnectionFactory.class, "pool");
ReflectionUtils.makeAccessible(poolField);
jedisPool = (JedisPool) ReflectionUtils.getField(poolField, jedisConnectionFactory);
}
//redis连接池设置
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(MaxTotal);
jedisPoolConfig.setMaxIdle(MaxIdle);
jedisPoolConfig.setMaxWaitMillis(MaxWaitMillis);
jedisPoolConfig.setMinIdle(MinIdle);
jedisPoolConfig.setTestOnBorrow(TestOnBorrow);
return jedisPoolConfig;
}
}
2、消息接收器配置:MsgReceiverConfig
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* 接收消息配置
* container 和 wsListenerAdapter 都要配置
*
*
* @Author XZ.Tan
* @Date: 2021/1/5 10:10
* @Version 1.0
*/
@Configuration
@EnableCaching
public class MsgReceiverConfig {
/**
*
* @param connectionFactory
* @param wsListenerAdapter 对应监听适配器方法名称
* @return
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter wsListenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
/**
* 配置监听
* 这里配置监听wsListenerAdapter,监听topic是个列表
* 可以添加多个 messageListener,配置不同的Adapter(对应监听适配器方法名称)
* 这里配置的 Adapter与真正的方法名一样
* @see MsgReceiverConfig#wsListenerAdapter
*/
List<Topic> topics = new ArrayList<>();
topics.add(new PatternTopic("test_topic1"));
topics.add(new PatternTopic("test_topic2"));
container.addMessageListener(wsListenerAdapter, topics);
return container;
}
/**
* 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法
*
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter wsListenerAdapter(MsgReceiver receiver) {
System.out.println("消息适配器WsMsgReceiver");
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}
3、消息接收器:MsgReceiver、MsgReceiverImpl
import org.springframework.stereotype.Service;
/**
* 消息接收器
*
* @Author XZ.Tan
* @Date: 2021/1/5 10:10
* @Version 1.0
*/
@Service
public interface MsgReceiver {
/**
* 消息接收器
* @param redisMsg
*/
void receiveMessage(Object redisMsg);
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* 消息接收器
* 接收redis订阅的消息
* 可以根据不同业务写多个
* 到 MsgReceiverConfig 注册即可
*
* @Author XZ.Tan
* @Date: 2021/1/5 17:38
* @Version 1.0
*/
@Service
@Slf4j
public class MsgReceiverImpl implements MsgReceiver {
@Override
public void receiveMessage(Object redisMsg) {
log.info("收到数据: {}", redisMsg);
}
}
4、消息发送例子:TestSandTopic1、TestSandTopic2
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* TestSand
*
* @Author XZ.Tan
* @Date: 2021/4/10 15:01
* @Version 1.0
*/
@Component
public class TestSandTopic1 implements CommandLineRunner {
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public void run(String... args) throws Exception {
MyThread t = new MyThread();
t.start();
}
class MyThread extends Thread {
@Override
public void run() {
while (true) {
// 模拟发送
redisTemplate.convertAndSend("test_topic1", "test_topic1 发送消息....");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* TestSand
*
* @Author XZ.Tan
* @Date: 2021/4/10 15:01
* @Version 1.0
*/
@Component
public class TestSandTopic2 implements CommandLineRunner {
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public void run(String... args) throws Exception {
TestSandTopic2.MyThread t = new TestSandTopic2.MyThread();
t.start();
}
class MyThread extends Thread{
@Override
public void run(){
while (true) {
// 模拟发送
redisTemplate.convertAndSend("test_topic2", "test_topic2 发送消息....");
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}