日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

重构客户注册-基于ActiveMQ实现短信验证码生产者

發布時間:2025/1/21 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 重构客户注册-基于ActiveMQ实现短信验证码生产者 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

重構目標:將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);// 將短信驗證碼 保存到sessionServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(), randomCode);System.out.println("生成手機驗證碼為:" + randomCode);// 編輯短信內容final String msg = "尊敬的用戶您好,本次獲取的驗證碼為:" + randomCode+ ",服務電話:4007654321";// 調用MQ服務,發送一條消息jmsTemplate.send("bos_sms", new MessageCreator() {@Overridepublic 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;}@Autowiredprivate 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsdhttp://www.springframework.org/schema/jmshttp://www.springframework.org/schema/jms/spring-jms.xsdhttp://activemq.apache.org/schema/corehttp://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实现短信验证码生产者的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。