首先 我要先考虑的是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";
@Autowired
private 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天内记录查询,格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页大小
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
//hint 此处可能会抛出异常,注意catch
QuerySendDetailsResponse 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/beans
http://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 {
//注入对象
@Autowired
private SmsUtils smsUtils;
@JmsListener(destination = "sms")
public void sendSms(Map map){
try {
//从map中获取消息
String signName = (String) map.get("sign_name");
//模板code
String 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(查询到的电话号码,名称,模板签名);
}
@Autowired
private 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天后自动下架的思路 ,肯定也有很多问题 ,希望各位大佬多指教一下