Spring-redis消息的订阅与发布

本文介绍了一个基于Spring和Redis实现的消息订阅发布系统。消息生产者通过Redis发布消息到指定通道,消息消费者订阅该通道并处理接收到的消息。文章详细展示了系统的配置文件、消息监听接口与实现类、以及消息发布的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

消息发布与订阅概述

消息订阅发布模型如图所示

消息生产者负责消息的发布,通过约定的通信方式,让消费者消费相应的消息。下面使用redis简单了实现了消息的订阅与发布。

1.消息的订阅方

1.1配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:redis="http://www.springframework.org/schema/redis"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/redis
       http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">

    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="localhost" p:port="6379" p:usePool="true">
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="redisConnectionFactory"/>
    <bean id="redisDAO" class="redis.springredis.daoimpl.RedisDAOImpl">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
    <bean id="listener" class="redis.springredis.listern.MessageDelegateListenerImpl"/>
    <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    <redis:listener-container>
        <redis:listener ref="listener" serializer="jdkSerializer" method="handleMessage" topic="java" />
    </redis:listener-container>
</beans>

1.2 消息监听接口和实现类

1.2.1监听接口

 

public interface MessageDelegateListener {

    void handleMessage(Serializable message);
}

1.2.2实现类

 

public class MessageDelegateListenerImpl implements MessageDelegateListener  {

    public void handleMessage(Serializable message) {
       if(message == null){
           System.out.println("null");
       } else if(message.getClass().isArray()){
           System.out.println(Arrays.toString((Object[])message));
       } else if(message instanceof List<?>) {
           System.out.println(message);
       } else if(message instanceof Map<? , ?>) {
            System.out.println(message);
        } else {
           System.out.println(message.toString());
       }

    }
}

1.3 测试类

 

public class Main {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("classpath:spring.redis.xml");
        while (true) {
            try {
                System.out.println("current time: " + new Date());

                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.消息发布

2.1配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="localhost" p:port="6379" p:usePool="true">
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
          p:connectionFactory-ref="redisConnectionFactory"/>
    <bean id="redisDAO" class="redistest.RedisDAOImpl">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
</beans>

2.2 消息的发布测试

2.2.1 测试接口

 

public interface RedisDAO {
    void sendMessage(String channel, Serializable message);
}

2.2.2测试接口实现类

 

@Service
public class RedisDAOImpl implements RedisDAO {

    private RedisTemplate<String, Object> redisTemplate = null;

    public RedisDAOImpl() {

    }

    public void sendMessage(String channel, Serializable message) {
        redisTemplate.convertAndSend(channel, message);
    }


    public RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
}

2.2.3 测试发布消息程序

 

public class RedisProducer {
        @Autowired
        RedisDAO redisDAO;
        @Test
        public void testPublishMessage() throws Exception {
        String msg = "Hello, Redis!";
        redisDAO.sendMessage("java", msg); 
        Integer[] values = new Integer[]{21341,123123,12323};
        redisDAO.sendMessage("java", values); 
}

 

至此redis与spring整合的消息订阅与发布程序已经结束,后续补充理论知识。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值