博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc redis消息队列
阅读量:7142 次
发布时间:2019-06-29

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

通常情况下,为了提高系统开发的灵活性和可维护度,我们会采用消息队列队系统进行解耦。下面是一个采用spring redis实现的消息队列实例,但此实例会由于网络延迟和阻塞等情况导致消息处理的延时,因而不能确保消息处理的顺序,所以使用时需要注意。
pom.xml中添加如下配置
添加版本配置
<properties>
  <jedis.version>2.8.1</jedis.version>
  <spring-data-redis.version>1.7.2.RELEASE</spring-data-redis.version>
  <commons-pool2.version>2.2</commons-pool2.version>
</properties>
    <!-- jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>${jedis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>${commons-pool2.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>${spring-data-redis.version}</version>
    </dependency>
properties文件中添加如下配置
#redis配置
redis.host=192.168.1.150
redis.port=6379
redis.password=redis
redis.timeout=2000
redis.max_total=100
redis.max_idle=20
redis.min_idle=5
redis.test_on_borrow=true
redis.test_on_return=true
applicationContext.xml中添加如下配置:
<!-- Jedis 连接池配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="${redis.max_idle}" />
    <property name="maxTotal" value="${redis.max_total}"/>
    <property name="minIdle" value="${redis.min_idle}"/>
    <property name="testOnBorrow" value="${redis.test_on_borrow}" />
    <property name="testOnReturn" value="${redis.test_on_return}"/>
</bean>
<!-- Jedis ConnectionFactory 数据库连接配置,注意id名称必须为redisConnectionFactory-->
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="${redis.host}" />
    <property name="port" value="${redis.port}" />
    <property name="password" value="${redis.password}" />
    <property name="poolConfig" ref="jedisPoolConfig" />
</bean>
定义消息发送者(生产者):
@Component
public class SendMessage {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public void sendMessage(String channel, Serializable message) {
        redisTemplate.convertAndSend(channel, message);
    }
}
定义消息处理者(消费者):
public class ListenMessage {
    public void handleMessage(Serializable message){
        System.out.println(message);
    }
}
调用:/queue/redis
@Controller
@RequestMapping(value = "/queue")
public class QueueController {
    @Autowired
    SendMessage sendMessage;
    @RequestMapping(value="/redis")
    public void redis(){
        for (int i = 0; i <1000; i++) {
            sendMessage.sendMessage("java",i);
        }
    }
}
新建applicationContext-redis.xml,内容如下:
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd">
    <!--创建redis模板-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory"/>
    </bean>
    <!--序列化-->
    <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    <!--消息监听处理类-->
    <bean id="messageDelegateListener" class="com.quartz.task.core.queue.ListenMessage"/>
    <bean id="messageListener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
        <property name="delegate" ref="messageDelegateListener"/>
        <property name="serializer" ref="jdkSerializer" />
    </bean>
    <!--消息监听-->
    <redis:listener-container>
        <!--指定消息处理方法,序列化方式及主题名称-->
        <redis:listener ref="messageListener" method="handleMessage" serializer="jdkSerializer" topic="java"/>
    </redis:listener-container>
</beans>
目录结构
E:.
├─src
│  └─main
│      ├─java
│      │  └─com
│      │      └─quartz
│      │          └─task
│      │              │
│      │              ├─controller
│      │              │      QueueController.java
│      │              │
│      │              ├─core
│      │              │  ├─queue
│      │              │  │      ListenMessage.java
│      │              │  │      SendMessage.java
│      │
│      ├─resources
│      │      applicationContext-redis.xml
│      │      applicationContext.xml
│      │      springMVC-context.xml
│      │      system-config.properties
│      │
│      └─webapp
│          │  index.jsp
│          │
│          └─WEB-INF
│              │  web.xml
│              │
│              ├─resources
│              └─view

│                      index.jsp

本文转自秋楓博客园博客,原文链接:http://www.cnblogs.com/rwxwsblog/p/5889867.html,如需转载请自行联系原作者

你可能感兴趣的文章
html 三列布局(两列自适应,一列固定宽度)
查看>>
详解javascript立即执行函数表达式(IIFE)
查看>>
WPF画图の利用Path画扇形(仅图形)
查看>>
(二)spring cloud微服务分布式云架构 - 整合企业架构的技术点
查看>>
Windows开发环境搭建
查看>>
asp.net core mvc 管道之中间件
查看>>
Win10任务栏假死问题解决方案
查看>>
[UWP]为附加属性和依赖属性自定义代码段(兼容UWP和WPF)
查看>>
mysql到JSP之间数据格式转换
查看>>
Thrift原理分析(一) 基本概念
查看>>
老司机避坑指南:如何快速搞定微服务架构?
查看>>
杨老师课堂之JavaScript案例全选、全不选、及反选
查看>>
开源编辑器 Atom 简化代码审查过程
查看>>
等等!这两个mysql慢查询的坑我已经替你们踩了
查看>>
【Python标准库:fileinput】优雅的读取文件
查看>>
“NO GENDER.NO BORDER.”,无性别服饰品牌“Bosie”获近千万元Pre-A轮融资
查看>>
用Python统计你的简书数据
查看>>
全票通过,百度 Doris 项目进入 Apache 基金会孵化器
查看>>
Retrofit2源码解析——网络调用流程(下)
查看>>
5G时代下的移动边缘计算(MEC)探索系列之二
查看>>