重构客户注册-基于ActiveMQ实现短信验证码生产者
来源:互联网 发布:办公室椅子知乎 编辑:程序博客网 时间:2024/05/16 15:34
重构目标:将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>
阅读全文
0 0
- 重构客户注册-基于ActiveMQ实现短信验证码生产者
- java实现注册的短信验证码
- 短信验证码注册
- Android 短信注册验证码
- 用户注册填写手机号接收短信验证码怎么实现?
- SSH2框架实现注册发短信验证码实例
- 用短信验证码SDK实现手机号注册功能
- Android请求服务器获取短信验证码实现注册功能
- Java用户注册服务器发送短信验证码功能实现
- 使用activeMQ发送短信验证码
- 注册juery短信,验证码 验证
- 利用某网站注册短信验证码漏洞,抓包改包实现短信轰炸骚扰
- 利用某网站注册短信验证码漏洞,抓包改包实现短信轰炸骚扰
- android 短信sms 接入,实现短信验证注册功能
- 实现短信验证码
- 基于mob短信验证的账号注册demo
- app注册短信验证和邮箱验证的技术实现
- Android注册短信验证码功能
- 《Java 编程技巧1001条》 第404条 了解实用方法
- bzoj 2199: [Usaco2011 Jan]奶牛议会
- C#基础知识简单梳理
- Cloudera Manager和CDH5.11.0升级流程
- bzoj4213 贪吃蛇
- 重构客户注册-基于ActiveMQ实现短信验证码生产者
- MongoDB的安装与配置
- binder原理
- 应用部署的六种策略
- JAVA实现约瑟夫环
- 【基础】 EFI基本概念之HOB
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"问题
- C语言printf()、sprintf()、vsprintf() 的区别与联系
- 解决input、textarea里使用disable属性文字变灰问题