博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-boot + redis 消息订阅/发布
阅读量:5913 次
发布时间:2019-06-19

本文共 4292 字,大约阅读时间需要 14 分钟。

hot3.png

创建spring-boot 项目

pom.xml引入依赖:
 

4.0.0
com.cherrish
demo-redis
0.0.1-SNAPSHOT
jar
demo-redis
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

application.properties:

 

spring.redis.database=4spring.redis.host=192.168.1.17spring.redis.port=6379spring.redis.password=redis

Java代码:

 

/*********************************DemoRedisApplication.java*********************************/package com.cherrish.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoRedisApplication {	public static void main(String[] args) {		SpringApplication.run(DemoRedisApplication.class, args);	}}/*********************************RedisConfig.java*********************************/package com.cherrish.demo;import com.cherrish.demo.redis.MessageReceiver;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.adapter.MessageListenerAdapter;/** * @author cherrish * @time 2018-08-30 14:51 * @name RedisConfig * @desc: */@Configurationpublic class RedisConfig {    @Bean    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,                                            MessageListenerAdapter listenerAdapter){        RedisMessageListenerContainer container = new RedisMessageListenerContainer();        container.setConnectionFactory(connectionFactory);        container.addMessageListener(listenerAdapter, new PatternTopic("chat"));        return container;    }    @Bean    MessageListenerAdapter listenerAdapter(MessageReceiver receiver){        return new MessageListenerAdapter(receiver, "receiveMessage");    }    @Bean    StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory){        return new StringRedisTemplate(connectionFactory);    }}/*********************************MessageReceiver.java*********************************/package com.cherrish.demo.redis;import org.springframework.stereotype.Component;/** * @author cherrish * @time 2018-08-30 15:05 * @name MessageReceiver * @desc: */@Componentpublic class MessageReceiver {    public void receiveMessage(String message){        System.out.println("receive a message: " + message);    }}/*********************************MessageSender.java*********************************/package com.cherrish.demo.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;/** * @author cherrish * @time 2018-08-30 15:01 * @name MessageSender * @desc: */@EnableScheduling@Componentpublic class MessageSender {    @Autowired    private StringRedisTemplate stringRedisTemplate;    @Scheduled(fixedRate = 2000)    public void sendMessage(){        stringRedisTemplate.convertAndSend("chat", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));    }}

 

转载于:https://my.oschina.net/u/574036/blog/1942213

你可能感兴趣的文章
linux系统配置之单一网卡配置多个不同网段IP(centos)
查看>>
.erb 中不能显示从mysql检索出的中文 incompatible character encodings: UTF-8 and ASCII-8BIT...
查看>>
51nod 1831: 小C的游戏(Bash博弈 找规律)
查看>>
使用filezilla连接树莓派失败
查看>>
[数分提高]2014-2015-2第5教学周第2次课讲义 3.2 微分中值定理
查看>>
Clr静态数据Table-Valued函数
查看>>
转:一个基于互联网医疗的创业公司,三年是一个收获
查看>>
How to effectively work with multiple files in Vim?
查看>>
Android 中文API (70) —— BluetoothDevice[蓝牙]
查看>>
不定宽高垂直居中分析
查看>>
ibatis中使用like模糊查询
查看>>
Scrum三头猪
查看>>
mysql之视图
查看>>
项目管理学习笔记之二.工作分解
查看>>
奇异值分解(We Recommend a Singular Value Decomposition)
查看>>
一个单元测试 学习 aysnc await
查看>>
Linux驱动总结3- unlocked_ioctl和堵塞(waitqueue)读写函数的实现 【转】
查看>>
iOS开发网络篇—HTTP协议
查看>>
jboss7 添加虚拟目录 上传文件路径
查看>>
在eclipse中建立lua开发环境
查看>>