1、redis序列化配置
package com.inesa.health.rc.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@SuppressWarnings({"all"})
@Configuration
@EnableCaching
public class PublishRedisConfig {
/**
* redis 读取内容的template
*/
// @Bean
// StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
// return new StringRedisTemplate(connectionFactory);
// }
@Bean
public RedisTemplate<String, String> publishRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
//定义value的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
2、由于在监听器中无法注入bean对象,因此需要以下配置
package com.inesa.health.rc.redis;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringJobBeanFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringJobBeanFactory.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
if (applicationContext == null){
return null;
}
return (T)applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> name) throws BeansException {
if (applicationContext == null){
return null;
}
return applicationContext.getBean(name);
}
}
3、注入redis
@Autowired
private RedisTemplate publishRedisTemplate;
4、完成推送消息
publishRedisTemplate.convertAndSend(RedisChannelEnum.CHANNEL_DEPARTMENT.getCode(),departmentUpdate);
5、redis通道的枚举配置
package com.inesa.health.rc.hospitalmanage.enums;
import java.util.ArrayList;
import java.util.List;
public enum RedisChannelEnum {
CHANNEL_HOSPITALMANAGE("channel:hospitalmanage","机构channel"),
CHANNEL_DEPARTMENT("channel:department","单位channel");
private String code;
private String desc;
RedisChannelEnum(String code, String desc){
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
/**
* 校验枚举中是否存在
* @param code
* @return
*/
public static boolean isValidEnum(String code){
RedisChannelEnum[] applyWayEnums = values();
for (RedisChannelEnum applyWayEnum : applyWayEnums) {
if (applyWayEnum.code.equals(code)){
return true;
}
}
return false;
}
public static List<String> getCodes(){
List<String> list = new ArrayList<>();
RedisChannelEnum[] applyStatusEnums = values();
for (RedisChannelEnum applyStatusEnum : applyStatusEnums) {
list.add(applyStatusEnum.getCode());
}
return list;
}
}
6、redis消息监听器容器
package com.inesa.health.rc.redis;
import com.inesa.health.rc.hospitalmanage.enums.RedisChannelEnum;
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.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
@SuppressWarnings({"all"})
@Configuration
@EnableCaching
public class RedisListenerConfig {
/**
* redis消息监听器容器
* 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器
* 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
* @param connectionFactory
* @param listenerAdapter
* @return
*/
@Bean
//相当于xml中的bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter HospitalmanageAdapter,MessageListenerAdapter DepartmentAdapter
) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//订阅了一个叫chat 的通道
container.addMessageListener(HospitalmanageAdapter, new PatternTopic(RedisChannelEnum.CHANNEL_HOSPITALMANAGE.getCode()));
container.addMessageListener(DepartmentAdapter, new PatternTopic(RedisChannelEnum.CHANNEL_DEPARTMENT.getCode()));
//这个container 可以添加多个 messageListener
return container;
}
/**
* 消息监听器适配器,绑定消息处理器
*
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter HospitalmanageAdapter() {
return new MessageListenerAdapter(new HospitalmanageListener());
}
/**
* 消息监听器适配器,绑定消息处理器
*
* @param receiver
* @return
*/
@Bean
MessageListenerAdapter DepartmentAdapter() {
return new MessageListenerAdapter(new DepartmentListener());
}
// @Bean
// MessageListenerAdapter DepartmentAdapter1() {
// return new MessageListenerAdapter(new DepartmentListener1());
// }
}
7、配置接听消息
package com.inesa.health.rc.redis;
import com.alibaba.fastjson.JSONObject;
import com.inesa.health.rc.hospitalmanage.po.Department;
import com.inesa.health.rc.hospitalmanage.po.Hospital;
import com.inesa.health.rc.service.DepartmentService;
import com.inesa.health.rc.service.HospitalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class DepartmentListener implements MessageListener {
DepartmentService departmentService = SpringJobBeanFactory.getBean(DepartmentService.class);
@Override
public void onMessage(Message message, byte[] bytes) {
String string = message.toString();
//log.info("收到信息为:"+string);
//Department department = JSONObject.parseObject(string, Department.class);
}
}
package com.inesa.health.rc.redis;
import com.alibaba.fastjson.JSONObject;
import com.inesa.health.rc.hospitalmanage.po.Hospital;
import com.inesa.health.rc.service.HospitalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class HospitalmanageListener implements MessageListener {
HospitalService hospitalService = SpringJobBeanFactory.getBean(HospitalService.class);
@Override
public void onMessage(Message message, byte[] bytes) {
String string = message.toString();
//log.info("收到信息为:"+string);
//Hospital hospital = JSONObject.parseObject(string, Hospital.class);
}
}