Spring Boot使用Redis進行消息的發佈訂閱

1.application.properties配置redis以及連接池

spring.redis.host=123.207.115.129
spring.redis.password=198568huiqing
spring.redis.database=0
spring.redis.port=6379
2.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.skycomm</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-redis-message-pubsub-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3.redis消息監聽器容器配置

/**
 * <p>
 * Title:RedisConfig
 * </p>
 * <p>
 * Description:Redis配置類
 * </p>
 * <p>
 * Copyright:Copyright(c)2005
 * </p>
 * <p>
 * Company:skycomm.com.cn
 * </p>
 * <p>
 * Author:lishuangjiang
 * </p>
 * <p>
 * Date:2018/4/18 13:47
 * </p>
 */

@Configuration
@EnableCaching
public class RedisConfig{

	/**
	 * Redis消息監聽器容器
	 * @param connectionFactory
	 * @param listenerAdapter
	 * @return
	 */
	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {

		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		//訂閱了一個叫pmp 的通道
		container.addMessageListener(listenerAdapter,new PatternTopic("pmp"));
		//這個container 可以添加多個 messageListener
		return container;
	}

	/**
	 * 配置消息接收處理類
	 * @param redisMsgSub  自定義消息接收類
	 * @return
	 */
	@Bean
	MessageListenerAdapter listenerAdapter(RedisMsgSub redisMsgSub) {
		//這個地方 是給messageListenerAdapter 傳入一個消息接受的處理器,利用反射的方法調用“receiveMessage”
		//也有好幾個重載方法,這邊默認調用處理器的方法 叫handleMessage 可以自己到源碼裏面看
		return new MessageListenerAdapter(redisMsgSub, "receiveMessage");
	}

}

4.redis消息接收

/**
 * <p>
 * Title:RedisMsgSub
 * </p>
 * <p>
 * Description:自定義消息接收類
 * </p>
 * <p>
 * Copyright:Copyright(c)2005
 * </p>
 * <p>
 * Company:skycomm.com.cn
 * </p>
 * <p>
 * Author:lishuangjiang
 * </p>
 * <p>
 * Date:2018/4/18 13:47
 * </p>
 */
@Component
public class RedisMsgSub{

    /**
     * 接收消息的方法
     * @param message 訂閱消息
     */
    public void receiveMessage(String message){
        System.out.println(message);
    }
}
5.redis消息多個訂閱不同的實現  代碼改造
    
    5.1添加redis訂閱消息接口

/**
 * <p>
 * Title:RedisMsg
 * </p>
 * <p>
 * Description:TODO
 * </p>
 * <p>
 * Copyright:Copyright(c)2005
 * </p>
 * <p>
 * Company:skycomm.com.cn
 * </p>
 * <p>
 * Author:lishuangjiang
 * </p>
 * <p>
 * Date:2018/4/18 17:18
 * </p>
 */
@Component
public interface RedisMsg {

    public void receiveMessage(String message);
}

    5.2實現消息接口

/**
 * <p>
 * Title:RedisPmpSub
 * </p>
 * <p>
 * Description:自定義消息接收類
 * </p>
 * <p>
 * Copyright:Copyright(c)2005
 * </p>
 * <p>
 * Company:skycomm.com.cn
 * </p>
 * <p>
 * Author:lishuangjiang
 * </p>
 * <p>
 * Date:2018/4/18 13:47
 * </p>
 */
public class RedisPmpSub implements RedisMsg{

    /**
     * 接收消息的方法
     * @param message 訂閱消息
     */
    public void receiveMessage(String message){
        System.out.println(message);
    }
}

5.2配置redis監聽容器

/**
 * <p>
 * Title:RedisConfig
 * </p>
 * <p>
 * Description:消息監聽器容器
 * </p>
 * <p>
 * Copyright:Copyright(c)2005
 * </p>
 * <p>
 * Company:skycomm.com.cn
 * </p>
 * <p>
 * Author:lishuangjiang
 * </p>
 * <p>
 * Date:2018/4/18 13:47
 * </p>
 */
@Configuration
@EnableCaching
public class RedisConfig{
	/**
	 * Redis消息監聽器容器
	 * @param connectionFactory
	 * @return
	 */
	@Bean
	RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		//訂閱了一個叫pmp 的通道
		container.addMessageListener(listenerAdapter(new RedisPmpSub()),new PatternTopic("pmp"));
		container.addMessageListener(listenerAdapter(new RedisChannelSub()),new PatternTopic("channel"));
		//這個container 可以添加多個 messageListener
		return container;
	}

	/**
	 * 配置消息接收處理類
	 * @param redisMsg  自定義消息接收類
	 * @return
	 */
	@Bean()
	@Scope("prototype")
	MessageListenerAdapter listenerAdapter(RedisMsg redisMsg) {
		//這個地方 是給messageListenerAdapter 傳入一個消息接受的處理器,利用反射的方法調用“receiveMessage”
		//也有好幾個重載方法,這邊默認調用處理器的方法 叫handleMessage 可以自己到源碼裏面看
		return new MessageListenerAdapter(redisMsg, "receiveMessage");
	}

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章