From 3070e8c5516caa1c332f79307b15f5b7233bd9f2 Mon Sep 17 00:00:00 2001
From: fanghongchao <601470458@qq.com>
Date: Fri, 14 Dec 2018 18:17:40 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E5=8A=A8=E6=80=81?=
=?UTF-8?q?=E6=B6=88=E6=81=AF=E8=BD=AC=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE?=
=?UTF-8?q?=20=E5=AE=9A=E6=97=B6=E5=99=A8=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 29 +-
.../config/ScheduledAsyncConfig.java | 38 +++
.../elasticsearch/ElasticsearchConfig.java | 69 ++++
.../elasticsearch/ElasticsearchUtil.java | 300 ++++++++++++++++++
.../scheduled/FlightHisScheduled.java | 137 ++++++++
.../flightInfo/FlightInfoServiceImpl.java | 14 +
.../flightInfo/IFlightInfoService.java | 8 +
.../msgexchangeapi/utils/DateTimeUtil.java | 29 ++
src/main/resources/application-dev.yml | 22 +-
9 files changed, 644 insertions(+), 2 deletions(-)
create mode 100644 src/main/java/com/gzzn/omms/msgexchangeapi/config/ScheduledAsyncConfig.java
create mode 100644 src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java
create mode 100644 src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtil.java
create mode 100644 src/main/java/com/gzzn/omms/msgexchangeapi/scheduled/FlightHisScheduled.java
diff --git a/pom.xml b/pom.xml
index 12f63651..13ad97e0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,12 +93,39 @@
spring-boot-starter-data-redis
-
+
net.logstash.logback
logstash-logback-encoder
5.2
+
+
+ org.elasticsearch
+ elasticsearch
+ 6.4.2
+
+
+
+
+ org.elasticsearch.client
+ transport
+ 6.4.2
+
+
+ org.elasticsearch
+ elasticsearch
+
+
+
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.15
+
+
diff --git a/src/main/java/com/gzzn/omms/msgexchangeapi/config/ScheduledAsyncConfig.java b/src/main/java/com/gzzn/omms/msgexchangeapi/config/ScheduledAsyncConfig.java
new file mode 100644
index 00000000..32cbb633
--- /dev/null
+++ b/src/main/java/com/gzzn/omms/msgexchangeapi/config/ScheduledAsyncConfig.java
@@ -0,0 +1,38 @@
+package com.gzzn.omms.msgexchangeapi.config;
+
+import java.util.concurrent.Executor;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+/**
+ * 定时器多线程配置
+ * @author fhc
+ *
+ */
+@Configuration
+@EnableAsync
+public class ScheduledAsyncConfig {
+
+ /*
+ 此处成员变量应该使用@Value从配置中读取
+ */
+ @Value("${scheduled.corePoolSize}")
+ private int corePoolSize;
+ @Value("${scheduled.maxPoolSize}")
+ private int maxPoolSize;
+ @Value("${scheduled.queueCapacity}")
+ private int queueCapacity;
+ @Bean
+ public Executor taskExecutor() {
+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+ executor.setCorePoolSize(corePoolSize);
+ executor.setMaxPoolSize(maxPoolSize);
+ executor.setQueueCapacity(queueCapacity);
+ executor.initialize();
+ return executor;
+ }
+}
diff --git a/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java
new file mode 100644
index 00000000..11ab89d8
--- /dev/null
+++ b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java
@@ -0,0 +1,69 @@
+package com.gzzn.omms.msgexchangeapi.elasticsearch;
+
+import java.net.InetAddress;
+
+import org.elasticsearch.client.transport.TransportClient;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.transport.TransportAddress;
+import org.elasticsearch.transport.client.PreBuiltTransportClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class ElasticsearchConfig {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);
+
+ /**
+ * elk集群地址
+ */
+ @Value("${elasticsearch.ip}")
+ private String hostName;
+
+ /**
+ * 端口
+ */
+ @Value("${elasticsearch.port}")
+ private String port;
+
+ /**
+ * 集群名称
+ */
+ @Value("${elasticsearch.cluster.name}")
+ private String clusterName;
+
+ /**
+ * 连接池
+ */
+ @Value("${elasticsearch.pool}")
+ private String poolSize;
+
+ /**
+ * Bean name default 函数名字
+ *
+ * @return
+ */
+ @Bean(name = "transportClient")
+ public TransportClient transportClient() {
+ LOGGER.info("Elasticsearch初始化开始。。。。。");
+ TransportClient transportClient = null;
+ try {
+ // 配置信息
+ Settings esSetting = Settings.builder()
+ .put("cluster.name", clusterName) // 集群名字
+// .put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群 (当前api无需嗅探,而且此处开启嗅探会导致链接失败)
+ .put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5
+ .build();
+ // 配置信息Settings自定义
+ transportClient = new PreBuiltTransportClient(esSetting);
+ TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
+ transportClient.addTransportAddresses(transportAddress);
+ } catch (Exception e) {
+ LOGGER.error("elasticsearch TransportClient create error!!", e);
+ }
+ return transportClient;
+ }
+}
diff --git a/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtil.java b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtil.java
new file mode 100644
index 00000000..ac3f07e1
--- /dev/null
+++ b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtil.java
@@ -0,0 +1,300 @@
+package com.gzzn.omms.msgexchangeapi.elasticsearch;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import javax.annotation.PostConstruct;
+
+import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
+import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
+import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
+import org.elasticsearch.action.delete.DeleteResponse;
+import org.elasticsearch.action.get.GetRequestBuilder;
+import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.action.search.SearchRequestBuilder;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.client.transport.TransportClient;
+import org.elasticsearch.common.text.Text;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
+import org.elasticsearch.search.sort.SortOrder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import com.alibaba.fastjson.JSONObject;
+
+@Component
+public class ElasticsearchUtil {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchUtil.class);
+
+ @Autowired
+ private TransportClient transportClient;
+ private static TransportClient client;
+
+ /**
+ * @PostContruct是spring框架的注解 spring容器初始化的时候执行该方法
+ */
+ @PostConstruct
+ public void init() {
+ client = this.transportClient;
+ }
+
+ /**
+ * 创建索引
+ *
+ * @param index
+ * @return
+ */
+ public static boolean createIndex(String index) {
+ if (!isIndexExist(index)) {
+ LOGGER.info("Index is not exits!");
+ }
+ CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
+ LOGGER.info("执行建立成功?" + indexresponse.isAcknowledged());
+ return indexresponse.isAcknowledged();
+ }
+
+ /**
+ * 删除索引
+ *
+ * @param index
+ * @return
+ */
+ public static boolean deleteIndex(String index) {
+ if (!isIndexExist(index)) {
+ LOGGER.info("Index is not exits!");
+ }
+ DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
+ if (dResponse.isAcknowledged()) {
+ LOGGER.info("delete index " + index + " successfully!");
+ } else {
+ LOGGER.info("Fail to delete index " + index);
+ }
+ return dResponse.isAcknowledged();
+ }
+
+ /**
+ * 判断索引是否存在
+ *
+ * @param index
+ * @return
+ */
+ public static boolean isIndexExist(String index) {
+ IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
+ if (inExistsResponse.isExists()) {
+ LOGGER.info("Index [" + index + "] is exist!");
+ } else {
+ LOGGER.info("Index [" + index + "] is not exist!");
+ }
+ return inExistsResponse.isExists();
+ }
+
+ /**
+ * @Author: LX
+ * @Description: 判断inde下指定type是否存在
+ * @Date: 2018/11/6 14:46
+ * @Modified by:
+ */
+ public boolean isTypeExist(String index, String type) {
+ return isIndexExist(index)
+ ? client.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists()
+ : false;
+ }
+
+ /**
+ * 数据添加,正定ID
+ *
+ * @param jsonObject 要增加的数据
+ * @param index 索引,类似数据库
+ * @param type 类型,类似表
+ * @param id 数据ID
+ * @return
+ */
+ public static String addData(JSONObject jsonObject, String index, String type, String id) {
+ IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();
+ LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());
+ return response.getId();
+ }
+
+ /**
+ * 数据添加
+ *
+ * @param jsonObject 要增加的数据
+ * @param index 索引,类似数据库
+ * @param type 类型,类似表
+ * @return
+ */
+ public static String addData(JSONObject jsonObject, String index, String type) {
+ return addData(jsonObject, index, type, ElasticsearchUtil.getUUID32());
+ }
+
+ /**
+ * 通过ID删除数据
+ *
+ * @param index 索引,类似数据库
+ * @param type 类型,类似表
+ * @param id 数据ID
+ */
+ public static void deleteDataById(String index, String type, String id) {
+
+ DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
+
+ LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());
+ }
+
+ /**
+ * 通过ID 更新数据
+ *
+ * @param jsonObject 要增加的数据
+ * @param index 索引,类似数据库
+ * @param type 类型,类似表
+ * @param id 数据ID
+ * @return
+ */
+ public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {
+
+ UpdateRequest updateRequest = new UpdateRequest();
+
+ updateRequest.index(index).type(type).id(id).doc(jsonObject);
+
+ client.update(updateRequest);
+
+ }
+
+ /**
+ * 通过ID获取数据
+ *
+ * @param index 索引,类似数据库
+ * @param type 类型,类似表
+ * @param id 数据ID
+ * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
+ * @return
+ */
+ public static Map searchDataById(String index, String type, String id, String fields) {
+
+ GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
+
+ if (!StringUtils.isEmpty(fields)) {
+ getRequestBuilder.setFetchSource(fields.split(","), null);
+ }
+
+ GetResponse getResponse = getRequestBuilder.execute().actionGet();
+
+ return getResponse.getSource();
+ }
+
+
+
+
+ /**
+ * 使用分词查询
+ *
+ * @param index 索引名称
+ * @param type 类型名称,可传入多个type逗号分隔
+ * @param query 查询条件
+ * @param size 文档大小限制
+ * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
+ * @param sortField 排序字段
+ * @param sortOrder
+ * @param highlightField 高亮字段
+ * @return
+ */
+ public static List