重构目标:将bos_fore项目中的CustomerAction作为短信消息生产者,将消息发给ActiveMQ,创建一个单独的SMS项目,作为短信息的消费者,从ActiveMQ获取短信消息,调用第三方接口完成短信发送。 
CustomerAction完整代码:

 @ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {
@Autowired
@Qualifier("jmsQueueTemplate")
private JmsTemplate jmsTemplate; @Action(value = "customer_sendSms")
public String sendSms() throws IOException {
// 手机号保存在Customer对象
// 生成短信验证码
String randomCode = RandomStringUtils.randomNumeric(4);
// 将短信验证码 保存到session
ServletActionContext.getRequest().getSession()
.setAttribute(model.getTelephone(), randomCode); System.out.println("生成手机验证码为:" + randomCode);
// 编辑短信内容
final String msg = "尊敬的用户您好,本次获取的验证码为:" + randomCode
+ ",服务电话:4007654321"; // 调用MQ服务,发送一条消息
jmsTemplate.send("bos_sms", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("telephone", model.getTelephone());
mapMessage.setString("msg", msg);
return mapMessage;
}
});
return NONE; } // 属性驱动
private String checkcode; public void setCheckcode(String checkcode) {
this.checkcode = checkcode;
} @Autowired
private RedisTemplate<String, String> redisTemplate; @Action(value = "customer_regist", results = {
@Result(name = "success", type = "redirect", location = "signup-success.html"),
@Result(name = "input", type = "redirect", location = "signup.html") })
public String regist() {
// 先校验短信验证码,如果不通过,调回注册页面
// 从session获取 之前生成验证码
String checkcodeSession = (String) ServletActionContext.getRequest()
.getSession().getAttribute(model.getTelephone());
if (checkcodeSession == null || !checkcodeSession.equals(checkcode)) {
System.out.println("短信验证码错误...");
// 短信验证码错误
return INPUT;
}
// 调用webService 连接CRM 保存客户信息
WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer")
.type(MediaType.APPLICATION_JSON).post(model);
System.out.println("客户注册成功..."); // 发送一封激活邮件
// 生成激活码
String activecode = RandomStringUtils.randomNumeric(32); // 将激活码保存到redis,设置24小时失效
redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24,
TimeUnit.HOURS); // 调用MailUtils发送激活邮件
String content = "尊敬的客户您好,请于24小时内,进行邮箱账户的绑定,点击下面地址完成绑定:<br/><a href='"
+ MailUtils.activeUrl + "?telephone=" + model.getTelephone()
+ "&activecode=" + activecode + "'>速运快递邮箱绑定地址</a>";
MailUtils.sendMail("速运快递激活邮件", content, model.getEmail()); return SUCCESS;
} // 属性驱动
private String activecode; public void setActivecode(String activecode) {
this.activecode = activecode;
} @Action("customer_activeMail")
public String activeMail() throws IOException {
ServletActionContext.getResponse().setContentType(
"text/html;charset=utf-8");
// 判断激活码是否有效
String activecodeRedis = redisTemplate.opsForValue().get(
model.getTelephone());
if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {
// 激活码无效
ServletActionContext.getResponse().getWriter()
.println("激活码无效,请登录系统,重新绑定邮箱!");
} else {
// 激活码有效
// 防止重复绑定
// 调用CRM webService 查询客户信息,判断是否已经绑定
Customer customer = WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer/telephone/"
+ model.getTelephone())
.accept(MediaType.APPLICATION_JSON).get(Customer.class);
if (customer.getType() == null || customer.getType() != 1) {
// 没有绑定,进行绑定
WebClient.create(
"http://localhost:9002/crm_management/services"
+ "/customerService/customer/updatetype/"
+ model.getTelephone()).get();
ServletActionContext.getResponse().getWriter()
.println("邮箱绑定成功!");
} else {
// 已经绑定过
ServletActionContext.getResponse().getWriter()
.println("邮箱已经绑定过,无需重复绑定!");
} // 删除redis的激活码
redisTemplate.delete(model.getTelephone());
}
return NONE;
} }

spring的配置文件applicationContext-mq.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd "> <!-- ActiveMQ 连接工厂 -->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码-->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- Spring Caching连接工厂 -->
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="mqConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
<!-- 同上,同理 -->
<!-- <constructor-arg ref="amqConnectionFactory" /> -->
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="100" />
</bean> <!-- Spring JmsTemplate 的消息生产者 start--> <!-- 定义JmsTemplate的Queue类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="mqConnectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
</bean> <!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<constructor-arg ref="mqConnectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
</bean> <!--Spring JmsTemplate 的消息生产者 end-->
</beans>

maven的pom文件完整代码:

 <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> <parent>
<groupId>cn.niwotaxuexiba.maven</groupId>
<artifactId>common_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <artifactId>bos_fore</artifactId>
<packaging>war</packaging>
<name>bos_fore</name>
<description>物流前端系统</description> <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9003</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>cn.niwotaxuexiba.maven</groupId>
<artifactId>crm_domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

重构客户注册-基于ActiveMQ实现短信验证码生产者的更多相关文章

  1. 项目一:在线下单(补充) activeMQ使用(重点) 重构客户注册功能,发短信功能分离

    1 课程计划 1.在线下单(补充) 2.activeMQ使用(重点) n 简介和安装 n activeMQ入门案例 n spring整合activeMQ应用 3.重构客户注册功能,发短信功能分离 n  ...

  2. 客户注册功能,发短信功能分离 通过ActiveMQ实现

    客户注册功能,发短信功能分离 通过ActiveMQ 配置链接工厂, 配置session缓存工厂(引入链接工厂) 2.配置模板对象JmsTemplate 引入缓存工厂    指定消息模式(队列,发布和订 ...

  3. 基于PHP实现短信验证码接口的方法

    步骤: 1.登录荣联运通讯注册获取ACCOUNT SID.AUTH TOKEN.Rest URL(生产).AppID(默认): 2.注册测试用手机号码(先注册测试号码方可使用): 3.下载demo示例 ...

  4. PHP 手机短信验证码 laravel 实现流程

    https://blog.csdn.net/uknow0904/article/details/80336941 本人在自己博客(Laravel)的注册部分 使用手机号注册,需要发送短信验证码. 使用 ...

  5. java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况

    java springboot activemq 邮件短信微服务,解决国际化服务的国内外兼容性问题,含各服务商调研情况 邮件短信微服务 spring boot 微服务 接收json格式参数 验证参数合 ...

  6. day101:MoFang:模型构造器ModelSchema&注册功能之手机号唯一验证/保存用户注册信息/发送短信验证码

    目录 1.模型构造器:ModelSchema 1.SQLAlchemySchema 2.SQLAlchemyAutoSchema 2.注册功能基本实现 1.关于手机号码的唯一性验证 2.保存用户注册信 ...

  7. day102:MoFang:后端完成对短信验证码的校验&基于celery完成异步短信发送&flask_jwt_extended&用户登录的API接口

    目录 1.用户注册 1.后端完成对短信验证码的校验 2.基于celery实现短信异步发送 2.用户登录 1.jwt登录验证:flask_jwt_extended 2.服务端提供用户登录的API接口 1 ...

  8. python基于LeanCloud的短信验证

    python基于LeanCloud的短信验证 1. 获取LeanCloud的Id.Key 2. 安装Flask框架和Requests库 pip install flask pip install re ...

  9. java实现注册的短信验证码

    最近在做只能净化器的后台用户管理系统,需要使用手机号进行注册,找了许久才大致了解了手机验证码实现流程,今天在此和大家分享一下. 我们使用的是榛子云短信平台, 官网地址:http://smsow.zhe ...

随机推荐

  1. Python selenium ActionChains 基本操作

    ActionChains类继承自object类,但是需要为其传入一个driver对象,即ActionChains(driver) 此类常用于模拟鼠标键盘操作,其API列表如下: perform()   ...

  2. Python *args 和 **kwargs用法

    *args的参数形式把剩下的没有关键字的参数收起来形成一个tuple,*kwargs把有关键字的收起来做成一个字典 def this_fun(a,b,*args,**kwargs): print a ...

  3. 深度学习之Attention Model(注意力模型)

    1.Attention Model 概述 深度学习里的Attention model其实模拟的是人脑的注意力模型,举个例子来说,当我们观赏一幅画时,虽然我们可以看到整幅画的全貌,但是在我们深入仔细地观 ...

  4. BZOJ1095:[ZJOI2007]Hide 捉迷藏(动态点分治)

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...

  5. day14(2)---列表推导式、生成器推导式、字典推导式、三元表达式

    一.列表推导式: ls = [元素表达式 for i in 可迭代对象 if 筛选条件] 案例: # -*- coding: utf-8 -*- '''列表生成式练习''' # 练习一(三元表达式): ...

  6. 环境部署(一):Linux下安装JDK

    自动化测试的主要目的是为了执行回归测试.当然,为了模拟真实的用户操作,一般都是在UAT或者生产环境进行回归测试. 为了尽量避免内网和外网解析对测试结果的影响,将自动化测试服务部署在外网的服务器是比较好 ...

  7. Git分支管理规范

    关于Git的一些分支管理规范... 一.分支与角色说明 Git 分支类型 master 分支(主分支) 稳定版本 develop 分支(开发分支) 最新版本 release 分支(发布分支) 发布新版 ...

  8. Postman的Tests标签测试

    接口测试最重要的就是返回数据的检查,一个简单的接口,我们可以肉眼检查返回数据,但接口一旦多起来且复杂,每次的检查都会很费劲,此时我们就需要postman 的tests模块来代替 postman面板: ...

  9. LOJ2527 HAOI2018 染色 容斥、生成函数、多项式求逆

    传送门 调了1h竟然是因为1004535809写成了998244353 "恰好有\(K\)种颜色出现了\(S\)次"的限制似乎并不容易达到,考虑容斥计算. 令\(c_j\)表示强制 ...

  10. Ajax跨越问题原因分析与解决思路

    1.什么是AJAX跨域问题 简单来说,就是前端调用后端服务接口时 如果服务接口不是同一个域,就会产生跨域问题 2.AJAX跨域场景 前后端分离.服务化的开发模式 前后端开发独立,前端需要大量调用后端接 ...