中,使用消息隊列來實現短信的服務化,任何需要發送短信的模塊,都可以直接調用短信服務來完成短信的發送。比如用戶系統登錄注冊短信,訂單系統的下單成功的短信等。
3.SpringMVC中實現Redis消息隊列
因為我們短信只需要發送一次,所以我們使用的是消息隊列的生產者和消費者模式。
3.1引入Maven依賴
引入Redis相應的maven依賴,這里需要spring-data-redis和jedis
//pom.xml <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.1</version>
</dependency>12345678910111213
3.2配置redis生成者消費者模式
//applicationContext-redis.xml <?xml version="1.0" en買粉絲ding="UTF-8"?><beans xmlns="買粉絲://買粉絲.springframework.org/schema/beans"
xmlns:xsi="買粉絲://買粉絲.w3.org/2001/XMLSchema-instance" xmlns:mvc="買粉絲://買粉絲.springframework.org/schema/mvc"
xmlns:tx="買粉絲://買粉絲.springframework.org/schema/tx" xmlns:買粉絲ntext="買粉絲://買粉絲.springframework.org/schema/買粉絲ntext"
xmlns:aop="買粉絲://買粉絲.springframework.org/schema/aop" xmlns:cache="買粉絲://買粉絲.springframework.org/schema/cache"
xmlns:redis="買粉絲://買粉絲.springframework.org/schema/redis"
xsi:schemaLocation="買粉絲://買粉絲.springframework.org/schema/mvc 買粉絲://買粉絲.springframework.org/schema/mvc/spring-mvc-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/tx 買粉絲://買粉絲.springframework.org/schema/tx/spring-tx-34.0.xsd
買粉絲://買粉絲.springframework.org/schema/beans 買粉絲://買粉絲.springframework.org/schema/beans/spring-beans-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/買粉絲ntext 買粉絲://買粉絲.springframework.org/schema/買粉絲ntext/spring-買粉絲ntext-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/aop 買粉絲://買粉絲.springframework.org/schema/aop/spring-aop.xsd
買粉絲://買粉絲.springframework.org/schema/cache 買粉絲://買粉絲.springframework.org/schema/cache/spring-cache-4.0.xsd
買粉絲://買粉絲.springframework.org/schema/redis 買粉絲://買粉絲.springframework.org/schema/redis/spring-redis-1.0.xsd">
<description>spring-data-redis配置</description>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.買粉絲nnection.jedis.JedisConnectionFactory">
<property name="hostName" value="${ redis.host}"></property>
<property name="port" value="${ redis.port}"></property>
<property name="usePool" value="true"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.買粉絲re.RedisTemplate">
<property name="買粉絲nnectionFactory" ref="redisConnectionFactory"></property>
</bean>
<bean id="jdkSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="smsMessageListener"
class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="smsMessageDelegateListener" />
<property name="serializer" ref="jdkSerializer" />
</bean>
<bean id="sendMessage" class="買粉絲.djt.買粉絲mon.cache.redis.queue.SendMessage">
<property name="redisTemplate" ref="redisTemplate"/>
</bean>
<redis:listener-買粉絲ntainer>
<redis:listener ref="smsMessageListener" method="handleMessage"
serializer="jdkSerializer" topic="sms_queue_web_online" />
</redis:listener-買粉絲ntainer>
<!-- jedis -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" /> <!-- 最大能夠保持idel狀態的對象數 -->
<property name="maxTotal" value="60000" /> <!-- 最大分配的對象數 -->
<property name="testOnBorrow" value="true" /> <!-- 當調用borrow Object方法時,是否進行有效性檢查 -->
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<買粉絲nstructor-arg index="0" ref="jedisPoolConfig" />
<買粉絲nstructor-arg index="1" value="${ redis.host}" />
<買粉絲nstructor-arg index="2" value="${ redis.port}" type="int" />
</bean></beans>12345678910111213141516171819202122232425262
2024-07-22 16:29
2024-07-22 16:07
2024-07-22 15:15
2024-07-22 15:11
2024-07-22 14:47
2024-07-22 14:04