实现房源15天后自动下架记录
生活随笔
收集整理的這篇文章主要介紹了
实现房源15天后自动下架记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先 我要先考慮的是15天自動下架的話 也就是我們要對數據庫進行掃描,發現有房源的添加時間與掃描此刻的時間相差大于15天時就對房源的狀態進行改變.
我們會用的優定時器 ,還有就是發送短信,由于我們公司時用的阿里大于發送短信 所以我就可以直接調用阿里大于的api.
定時器的話 由于我用的是spring boot框架所以可能直接用@Scheduled來做每天掃描,當然 我覺得最好把掃描時間定在凌晨或者是深夜,這樣用戶少,方便我們操作。
我覺得我們可以先定出定時器的執行方法。
1.0 HouseTimingTask.java
@Component public class HouseTimingTask { @Scheduled(cron = "0 30 6 ? * *")//我這邊設置的是每天早上六點半執行 public void Task(){ //這里面寫掃描數據庫的方法 我就不把自己的代碼寫出來了 畢竟每個項目的數據庫環境都不一樣, //重要的是要看房源的添加時間與掃描此刻的時間相差大于15天時就對房源的狀態進行改變.if(DateUtil.dateTointerval(添加的時間,現在的時間)>15){//1,改變房源狀態//2,發送短信給房東}}定時器寫好了 我們現在來配置阿里大于的接口,在到定時任務中進行調用。
2.導入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId></dependency><!-- 阿里大于相關配置 --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>1.0.0-SNAPSHOT</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>3.2.2</version></dependency>3.0 SmsUtils.java 短信工具類
import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import org.springframework.core.env.Environment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.text.SimpleDateFormat; import java.util.Date;@Component public class SmsUtils {static final String product = "Dysmsapi";static final String domain = "dysmsapi.aliyuncs.com";@Autowiredprivate Environment env;/*** 發送短信驗證碼* @param phoneNum 待發手機號* @param signName 短信簽名* @param templateCode 短信模板*/public SendSmsResponse sendSms(String phoneNum,String signName,String templateCode)throws ClientException {//可自助調整超時時間System.setProperty("sun.net.client.defaultConnectTimeout", "10000");System.setProperty("sun.net.client.defaultReadTimeout", "10000");//初始化acsClient,暫不支持region化IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", env.getProperty("AccessKeyID"), env.getProperty("accessKeySecret"));DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);IAcsClient acsClient = new DefaultAcsClient(profile);//組裝請求對象-具體描述見控制臺-文檔部分內容SendSmsRequest request = new SendSmsRequest();//必填:待發送手機號request.setPhoneNumbers(phoneNum);//必填:短信簽名-可在短信控制臺中找到request.setSignName(signName);//必填:短信模板-可在短信控制臺中找到request.setTemplateCode(templateCode);SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);return sendSmsResponse;}public QuerySendDetailsResponse querySendDetails(String phoneNum,String bizId) throws ClientException {String accessKeyId =env.getProperty("accessKeyId");String accessKeySecret = env.getProperty("accessKeySecret");//可自助調整超時時間System.setProperty("sun.net.client.defaultConnectTimeout", "10000");System.setProperty("sun.net.client.defaultReadTimeout", "10000");//初始化acsClient,暫不支持region化IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);IAcsClient acsClient = new DefaultAcsClient(profile);//組裝請求對象QuerySendDetailsRequest request = new QuerySendDetailsRequest();//必填-號碼request.setPhoneNumber(phoneNum);//可選-流水號request.setBizId(bizId);//必填-發送日期 支持30天內記錄查詢,格式yyyyMMddSimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");request.setSendDate(ft.format(new Date()));//必填-頁大小request.setPageSize(10L);//必填-當前頁碼從1開始計數request.setCurrentPage(1L);//hint 此處可能會拋出異常,注意catchQuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);return querySendDetailsResponse;} }由于我用到了activemq所以進行配置
4.application.properties
#整合mq spring.activemq.broker-url=tcp://192.168.12.66:61616 #發送短信秘鑰 AccessKeyID=自己在阿里大于上面看 accessKeySecret=自己在阿里大于上面看5.applicationContext-mq-producer.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--把activeMQ交給spring管理--><bean id="targetConnnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><constructor-arg name="brokerURL" value="tcp://139.199.***.***:61616"/></bean><!--創建spring jms組件提供工廠對象,無縫接管activeMQ--><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="targetConnnectionFactory"/></bean><!--spring jms提供消息發送模板對象--><bean class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"/></bean><!--發布訂閱消息空間--><bean class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="sms"/></bean></beans>6.SmsListener.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component;import java.util.Map;@Component public class SmsListener {//注入對象@Autowiredprivate SmsUtils smsUtils;@JmsListener(destination = "sms")public void sendSms(Map map){try {//從map中獲取消息String signName = (String) map.get("sign_name");//模板codeString templateCode = (String) map.get("template_code");//電話號碼String phone = (String) map.get("phone");// 發送短信smsUtils.sendSms(phone,signName,templateCode);} catch (Exception e) {e.printStackTrace();}} }7.完善 HouseTimingTask.java
import com.chuangting.common.util.DateUtil; import com.chuangting.organization.entity.EOrganization; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import java.util.*;@Component public class HouseTimingTask {@Scheduled(fixedDelayString = "0 30 6 ? * *")public void Task(){//這里面寫掃描數據庫的方法 我就不把自己的代碼寫出來了 畢竟每個項目的數據庫環境都不一樣, //重要的是要看房源的添加時間與掃描此刻的時間相差大于15天時就對房源的狀態進行改變.if(DateUtil.dateTointerval(添加的時間,現在的時間)>15){//1,改變房源狀態...............自己補充//2,發送短信給房東sendSms(查詢到的電話號碼,名稱,模板簽名);}@Autowiredprivate JmsTemplate jmsTemplate;public void sendSms(String phoneNum,String signName,String templateCode){try {HashMap<String, String> map = new HashMap<>();map.put("phoneNum", phoneNum);map.put("signName", signName);map.put("templateCode", templateCode);jmsTemplate.convertAndSend("sms", map);}catch (Exception e){e.printStackTrace();}}}以上是我寫的實現房源15天后自動下架的思路 ,肯定也有很多問題 ,希望各位大佬多指教一下
總結
以上是生活随笔為你收集整理的实现房源15天后自动下架记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MODBUS-RTU CRC校验算法及函
- 下一篇: 网易云课堂视频下载