Commit 0999d4c4 by chentian

solr定时更新项目

parents
HELP.md
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/
### VS Code ###
.vscode/
<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.founder</groupId>
<artifactId>xzxt_solr_update</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xzxt_solr_update</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>xzxt_solr_update</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.founder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
// 启用异步任务
@EnableAsync
//进行定时任务
@EnableScheduling
@SpringBootApplication
public class XzxtSolrUpdateApplication {
public static void main(String[] args) {
SpringApplication.run(XzxtSolrUpdateApplication.class, args);
}
}
package com.founder.controller;
import com.founder.model.solrdata;
import com.founder.util.SolrUpdateJobTask;
import com.founder.util.propertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Future;
/**
* Created by changc on 2019/3/5.
*/
@RestController
public class TaskController {
@Autowired
private SolrUpdateJobTask solrUpdateJobTask;
/*@Autowired
private SimpMessagingTemplate template;*/
@Autowired
private ServletContext servletContext;
@ResponseBody
@RequestMapping("/sourcesolrdatatask")
//每隔5秒执行一次:*/5 * * * * ?
//每隔5分执行一次:0 */5 * * * ?
//每个整点执行一次:0 0 0/1 * * ?
//每天凌晨2点执行一次:0 0 2 * * ?
@Scheduled(cron = "0 */15 * * * ?")
public String sourcesolrdatatask() throws Exception {
System.out.println("==========================开始====================");
long starttime = System.currentTimeMillis();
Properties properties= propertiesUtil.propertiesUtil("solr.properties");
String solrurl=properties.getProperty("sourcesolrdata");
String solrs=properties.getProperty("sourcesolrdatahome");
String [] solrhomes=solrs.split(",");
String msg="";
//String remsg="";
List<solrdata> solrdatalist =new ArrayList<>();
for(int i=0;i<solrhomes.length;i++){
String url=solrurl+solrhomes[i];
Future<String> future = solrUpdateJobTask.solrdataupdate(url,solrhomes[i]);
msg=future.get();
long endtime = System.currentTimeMillis();
System.out.println("执行原数据更新总用时:" + (endtime - starttime) + "毫秒");
//处理结果
String [] msgs=msg.split(",");
String remsg="";
for(int n=0;n<msgs.length;n++){
String [] msgs2=msgs[n].split("=");
remsg+=msgs2[1]+",";
}
String [] res=remsg.split(",");
System.out.println("res:"+res[0]+"===="+res[1]);
solrdata solrdata=new solrdata();
solrdata.setSolrhomes(res[0]);
solrdata.setNumFound(res[1]);
// solrdata.setUpdatecount((res[2].split("}"))[0]); //"数据为0}} 做截取"
solrdata.setUestimes( (endtime - starttime)+"");
solrdatalist.add(solrdata);
}
//this.template.convertAndSend("/topic/getResponse", solrdatalist);
System.out.println("==========================结束====================");
return solrdatalist.toString();
}
}
package com.founder.model;
import lombok.Data;
/**
* Created by changc on 2019/3/12.
*/
@Data
public class solrdata {
private String solrhomes;
private String numFound;
private String updatecount;
private String uestimes;
private String flag;
}
package com.founder.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();
httpGet.setConfig(requestConfig);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();
httpPost.setConfig(requestConfig);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJson(String url, String json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doRequest(CloseableHttpClient httpclient, HttpUriRequest httpUriRequest, BasicCookieStore cookieStore) throws IOException {
//返回相应
CloseableHttpResponse response = httpclient.execute(httpUriRequest);
//获取response返回的相应实体
HttpEntity entity = response.getEntity();
entity.getContent();
//转码
String res= EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
System.out.println("返回参数==="+res.toString());
return res;
}
}
package com.founder.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
/**
* Created by changc on 2018/10/19.
*/
@Component
public class SolrUpdateJobTask {
@Autowired
private SolrClient client;
//更新
@Async("taskExecutor")
public Future<String> solrdataupdate(String url,String solrhomes) throws IOException, SolrServerException {
//查询参数
Map<String, String> param = new HashMap<>();
System.out.println("更新实例地址============="+url+"/dataimport?command=delta-import&clean=false&commit=true");
String json = HttpClientUtil.doPost(url+"/dataimport?command=delta-import&clean=false&commit=true", param);
//根据第二次运行的结果才是准确的更新
String json1 = HttpClientUtil.doPost(url+"/dataimport?command=delta-import&clean=false&commit=true", param);
/* JSONObject jsonObject = JSONObject.parseObject(json1);
String statusMessages = jsonObject.getString("statusMessages");
JSONObject jsonObject1 = JSONObject.parseObject(statusMessages);
String count = jsonObject1.getString("Total Changed Documents");*/
SolrQuery params = new SolrQuery();
//q 查询字符串,如果查询所有*:* keywords:*15* 模糊查询
params.set("q", "*:*");
//执行搜索
QueryResponse queryResponse = client.query(solrhomes,params);
//搜索结果
SolrDocumentList results = queryResponse.getResults();
//查询出来的数量---------------------------------------
String numFound = results.getNumFound()+"";
HashMap<String,String> res=new HashMap<>();
res.put("solrhomes",solrhomes);
res.put("numFound",numFound);
//res.put("count",count);
// 消息汇总
return new AsyncResult<>(String.format("{%s}", res));
}
}
package com.founder.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* Created by changc on 2017/9/13.
*/
public class propertiesUtil {
public static Properties propertiesUtil(String file){
Properties properties = new Properties();
InputStream inStream = propertiesUtil.class.getClassLoader().getResourceAsStream(file);
try {
properties.load(inStream);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
public static String getConfig(String file,String key){
Properties pros = new Properties();
String value="";
try {
pros.load(new InputStreamReader(Object.class.getResourceAsStream(file),"UTF-8"));
value=pros.get(key).toString();
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
}
spring:
data:
solr:
host: http://95.1.122.29:9041/solr
\ No newline at end of file
#˰ɸ
sourcesolrdata=http://95.1.122.29:9041/solr/
sourcesolrdatahome=GNLKZSXX,MHLKDPXX,MHLKLGXX,TLDPXX,WBSWRY
\ No newline at end of file
package com.founder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class XzxtSolrUpdateApplicationTests {
@Test
public void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment