5 changed files with 97 additions and 1 deletions
			
			
		@ -0,0 +1,27 @@
					 | 
				
			||||
package com.hnac.hzims.alarm.monitor.listener; | 
				
			||||
 | 
				
			||||
import org.springframework.beans.factory.annotation.Autowired; | 
				
			||||
import org.springframework.data.redis.connection.Message; | 
				
			||||
import org.springframework.data.redis.connection.MessageListener; | 
				
			||||
import org.springframework.data.redis.core.RedisTemplate; | 
				
			||||
import org.springframework.data.redis.serializer.RedisSerializer; | 
				
			||||
import org.springframework.stereotype.Component; | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * @author ysj | 
				
			||||
 * @version 4.0.0 | 
				
			||||
 * @create 2023-11-09-14:04 | 
				
			||||
 */ | 
				
			||||
@Component | 
				
			||||
public class AlarmListener implements MessageListener { | 
				
			||||
 | 
				
			||||
    @Autowired | 
				
			||||
    private RedisTemplate<Object, Object> redisTemplate; | 
				
			||||
 | 
				
			||||
    @Override | 
				
			||||
    public void onMessage(Message message, byte[] pattern) { | 
				
			||||
        RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); | 
				
			||||
        String msg = serializer.deserialize(message.getBody()); | 
				
			||||
        System.out.println("接收到的消息是:" + msg); | 
				
			||||
    } | 
				
			||||
} | 
				
			||||
@ -0,0 +1,45 @@
					 | 
				
			||||
package com.hnac.hzims.alarm.monitor.listener; | 
				
			||||
 | 
				
			||||
import com.hnac.hzims.common.config.RedisMessageListener; | 
				
			||||
import org.springframework.context.annotation.Bean; | 
				
			||||
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; | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * @author ysj | 
				
			||||
 * @version 4.0.0 | 
				
			||||
 * @create 2023-11-09 14:02 | 
				
			||||
 */ | 
				
			||||
public class QuqueConfig { | 
				
			||||
 | 
				
			||||
 | 
				
			||||
        /** | 
				
			||||
         * 创建连接工厂 | 
				
			||||
         * | 
				
			||||
         * @param connectionFactory | 
				
			||||
         * @param adapter | 
				
			||||
         * @return | 
				
			||||
         */ | 
				
			||||
        @Bean | 
				
			||||
        public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, | 
				
			||||
                                                       MessageListenerAdapter adapter) { | 
				
			||||
            RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | 
				
			||||
            container.setConnectionFactory(connectionFactory); | 
				
			||||
            //监听对应的channel
 | 
				
			||||
            container.addMessageListener(adapter, new PatternTopic("alarmchannel")); | 
				
			||||
            return container; | 
				
			||||
        } | 
				
			||||
 | 
				
			||||
        /** | 
				
			||||
         * 	绑定消息监听者和接收监听的方法 | 
				
			||||
         * @param message | 
				
			||||
         * @return | 
				
			||||
         */ | 
				
			||||
        @Bean | 
				
			||||
        public MessageListenerAdapter adapter(RedisMessageListener message) { | 
				
			||||
            // onMessage 如果RedisMessage 中 没有实现接口,这个参数必须跟RedisMessage中的读取信息的方法名称一样
 | 
				
			||||
            return new MessageListenerAdapter(message, "onMessage"); | 
				
			||||
        } | 
				
			||||
} | 
				
			||||
@ -0,0 +1,8 @@
					 | 
				
			||||
package com.hnac.hzims.alarm.monitor.service; | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * @author ysj | 
				
			||||
 */ | 
				
			||||
public interface AlarmSaveService { | 
				
			||||
 | 
				
			||||
} | 
				
			||||
@ -0,0 +1,16 @@
					 | 
				
			||||
package com.hnac.hzims.alarm.monitor.service.impl; | 
				
			||||
 | 
				
			||||
import com.hnac.hzims.alarm.monitor.service.AlarmSaveService; | 
				
			||||
import lombok.AllArgsConstructor; | 
				
			||||
import lombok.extern.slf4j.Slf4j; | 
				
			||||
import org.springframework.stereotype.Service; | 
				
			||||
 | 
				
			||||
/** | 
				
			||||
 * @author ysj | 
				
			||||
 */ | 
				
			||||
@AllArgsConstructor | 
				
			||||
@Service | 
				
			||||
@Slf4j | 
				
			||||
public class AlarmSaveServiceImpl  implements AlarmSaveService { | 
				
			||||
 | 
				
			||||
} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue