通常情况下,为了提高系统开发的灵活性和可维护度,我们会采用消息队列队系统进行解耦。下面是一个采用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