在springboot里使用task定时任务特别容易,简单的说就启动类添加一个enableXXX注解,然后再需要的方法上添加Scheduledz注解,注解里cron表达式网上百度一下一大把。
启动类添加注解
@SpringBootApplication
@EnableScheduling
public class TaskApplication {
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class,args);
}
}
方法上使用 @Scheduled 注解
@Scheduled(cron = "0 0 0 1/1 * ? ") //这代表每天的凌晨12点开始执行定时方法
通过以上操作定时器的任务就可以正常运行了,但是要通过定时去向百度推送数据,不会像怎么简单了,直接看代码吧
//postUrl是需要推送的url链接,domain是你的站点url链接,token是登录百度站长之后生成的token
public String pushPost(String PostUrl,String domain, String bdToken){
String linkSubmitUrl="http://data.zz.baidu.com/urls";
String host="data.zz.baidu.com";
linkSubmitUrl+="?site="+domain+"&token="+bdToken;
String result="";
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//HttpClient
CloseableHttpClient client = httpClientBuilder.build();
client = (CloseableHttpClient) wrapClient(client);
Map<String, String> msg=new HashMap<>();
HttpPost post = new HttpPost(linkSubmitUrl);
//发送请求参数
try
{
StringEntity s = new StringEntity(PostUrl,"utf-8");
s.setContentType("application/json");
post.setEntity(s);
post.setHeader("Host", host);
post.setHeader("User-Agent", "curl/7.12.1");
post.setHeader("Content-Type", "text/plain");
HttpResponse res = client.execute(post);
HttpEntity entity = res.getEntity();
String str= EntityUtils.toString(entity, "utf-8");
result=str;
}
catch (Exception e)
{
result=null;
e.printStackTrace();
}
return result;
}
private static HttpClient wrapClient(HttpClient client) {
try {
SSLContext ctx = SSLContext.getInstance("TLSv1");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(ctx,
new String[] {"TLSv1"}, null,new DefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslSf).build();
return httpclient;
} catch (Exception ex) {
return null;
}
}
把需要导的包也放出来吧,万一导错了就尴尬了
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
好了,到这里就可以对你的网站进行百度推送了,慢慢等待被百度收录就可以了
需要注意的是在进行定时任务的时候需要留意一下是否成功推送到百度,是否成功可以看返回的状态码,若成功的话就会返回当天剩余可推送的次数,若失败则返回状态码以及错误提示信息!!!
博客地址:徐启君的个人博客 xqijun.top