diff --git a/common-api/src/main/java/cn/lili/controller/common/UploadController.java b/common-api/src/main/java/cn/lili/controller/common/UploadController.java index 767db2fa..027b66ae 100644 --- a/common-api/src/main/java/cn/lili/controller/common/UploadController.java +++ b/common-api/src/main/java/cn/lili/controller/common/UploadController.java @@ -75,7 +75,7 @@ public class UploadController { } - if (!CharSequenceUtil.containsAny(file.getContentType().toLowerCase(), "image")) { + if (!CharSequenceUtil.containsAny(Objects.requireNonNull(file.getContentType()).toLowerCase(), "image", "video")) { throw new ServiceException(ResultCode.FILE_TYPE_NOT_SUPPORT); } diff --git a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java index 7f08ddf7..e5976870 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java +++ b/framework/src/main/java/cn/lili/elasticsearch/BaseElasticsearchService.java @@ -389,11 +389,17 @@ public abstract class BaseElasticsearchService { */ protected void deleteIndexRequest(String index) { DeleteIndexRequest deleteIndexRequest = buildDeleteIndexRequest(index); - try { - client.indices().delete(deleteIndexRequest, COMMON_OPTIONS); - } catch (IOException e) { - throw new ElasticsearchException("删除索引 {" + index + "} 失败:" + e.getMessage()); - } + client.indices().deleteAsync(deleteIndexRequest, COMMON_OPTIONS, new ActionListener() { + @Override + public void onResponse(AcknowledgedResponse acknowledgedResponse) { + log.info("删除索引 {} 成功", index); + } + + @Override + public void onFailure(Exception e) { + log.error("删除索引 {} 失败", index, e); + } + }); } /** diff --git a/framework/src/main/java/cn/lili/elasticsearch/config/ElasticsearchConfig.java b/framework/src/main/java/cn/lili/elasticsearch/config/ElasticsearchConfig.java index b80da178..6010a2a4 100644 --- a/framework/src/main/java/cn/lili/elasticsearch/config/ElasticsearchConfig.java +++ b/framework/src/main/java/cn/lili/elasticsearch/config/ElasticsearchConfig.java @@ -8,7 +8,7 @@ import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.nio.reactor.IOReactorConfig; +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; @@ -39,35 +39,46 @@ public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { @Override @Bean public RestHighLevelClient elasticsearchClient() { - RestClientBuilder restBuilder = RestClient - .builder(this.getHttpHosts()); - restBuilder.setHttpClientConfigCallback(httpClientBuilder -> - httpClientBuilder - .setKeepAliveStrategy(getConnectionKeepAliveStrategy()) - .setMaxConnPerRoute(10). - setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build())); String username = elasticsearchProperties.getAccount().getUsername(); String password = elasticsearchProperties.getAccount().getPassword(); - if (username != null && password != null) { - final CredentialsProvider credential = new BasicCredentialsProvider(); - credential.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); - restBuilder.setHttpClientConfigCallback(httpClientBuilder -> - httpClientBuilder - .setDefaultCredentialsProvider(credential) - .setKeepAliveStrategy(getConnectionKeepAliveStrategy()) - .setMaxConnPerRoute(10) - .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).build())); - } + final CredentialsProvider credential = createCredentialsIfNotNull(username, password); - restBuilder.setRequestConfigCallback(requestConfigBuilder -> - requestConfigBuilder.setConnectTimeout(1000) //time until a connection with the server is established. - .setSocketTimeout(12 * 1000) //time of inactivity to wait for packets[data] to receive. - .setConnectionRequestTimeout(-1)); //time to fetch a connection from the connection pool 0 for infinite. + RestClientBuilder restBuilder = createRestClientBuilderWithConfig(credential); client = new RestHighLevelClient(restBuilder); return client; } + private CredentialsProvider createCredentialsIfNotNull(String username, String password) { + if (username == null || password == null) { + return null; + } + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + return credentialsProvider; + } + + private RestClientBuilder createRestClientBuilderWithConfig(CredentialsProvider credentialsProvider) { + return RestClient + .builder(this.getHttpHosts()) + .setHttpClientConfigCallback(httpClientBuilder -> configureHttpClientBuilder(httpClientBuilder, credentialsProvider)) + .setRequestConfigCallback(requestConfigBuilder -> + requestConfigBuilder + .setConnectTimeout(1000) + .setSocketTimeout(12 * 1000)); + } + + private HttpAsyncClientBuilder configureHttpClientBuilder(HttpAsyncClientBuilder httpClientBuilder, + CredentialsProvider credentialsProvider) { + httpClientBuilder + .setKeepAliveStrategy(getConnectionKeepAliveStrategy()) + .setMaxConnPerRoute(10); + if (credentialsProvider != null) { + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + } + return httpClientBuilder; + } + private HttpHost[] getHttpHosts() { List clusterNodes = elasticsearchProperties.getClusterNodes(); HttpHost[] httpHosts = new HttpHost[clusterNodes.size()]; diff --git a/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsSku.java b/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsSku.java index 0c89fdc1..309fea1a 100644 --- a/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsSku.java +++ b/framework/src/main/java/cn/lili/modules/goods/entity/dos/GoodsSku.java @@ -189,7 +189,7 @@ public class GoodsSku extends BaseEntity { this.goodsId = goods.getId(); this.goodsName = goods.getGoodsName(); this.goodsType = goods.getGoodsType(); - + this.goodsVideo = goods.getGoodsVideo(); this.selfOperated = goods.getSelfOperated(); this.sellingPoint = goods.getSellingPoint(); this.categoryPath = goods.getCategoryPath(); diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsImportServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsImportServiceImpl.java index 349ac5d9..bcf056c2 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsImportServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsImportServiceImpl.java @@ -175,14 +175,24 @@ public class GoodsImportServiceImpl implements GoodsImportService { } } - String categoryId = objects.get(2).toString().substring(0, objects.get(2).toString().indexOf("-")); + String categoryId = null; + try { + categoryId = objects.get(2).toString().substring(0, objects.get(2).toString().indexOf("-")); + } catch (Exception e) { + throw new ServiceException("请选择商品分类"); + } Category category = categoryService.getCategoryById(categoryId); if (category == null) { throw new ServiceException("商品分类不存在:" + objects.get(2).toString().substring(objects.get(2).toString().indexOf("-"))); } - String templateId = objects.get(3).toString().substring(0, objects.get(3).toString().indexOf("-")); + String templateId = null; + try { + templateId = objects.get(3).toString().substring(0, objects.get(3).toString().indexOf("-")); + } catch (Exception e) { + throw new ServiceException("请选择物流模板"); + } FreightTemplateVO freightTemplateVO = freightTemplateService.getFreightTemplate(templateId); if (freightTemplateVO == null) { throw new ServiceException("配送模板不存在:" + objects.get(3).toString().substring(objects.get(3).toString().indexOf("-"))); diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java index a747c377..c9c7fba5 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillServiceImpl.java @@ -153,7 +153,7 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl 0) { - throw new RetryException("更新商品索引失败,es内容版本冲突"); + + this.client.updateByQueryAsync(update, RequestOptions.DEFAULT, new ActionListener() { + @Override + public void onResponse(BulkByScrollResponse bulkByScrollResponse) { + if (bulkByScrollResponse.getVersionConflicts() > 0) { + throw new RetryException("更新商品索引失败,es内容版本冲突"); + } } - } catch (IOException e) { - log.error("更新商品索引异常", e); - } + + @Override + public void onFailure(Exception e) { + log.error("更新商品索引异常", e); + } + }); } /** @@ -433,24 +439,32 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements */ @Override public void updateBulkIndex(List goodsIndices) { - try { - //索引名称拼接 - String indexName = getIndexName(); + //索引名称拼接 + String indexName = getIndexName(); - BulkRequest request = new BulkRequest(); + BulkRequest request = new BulkRequest(); - for (EsGoodsIndex goodsIndex : goodsIndices) { - UpdateRequest updateRequest = new UpdateRequest(indexName, goodsIndex.getId()); + for (EsGoodsIndex goodsIndex : goodsIndices) { + UpdateRequest updateRequest = new UpdateRequest(indexName, goodsIndex.getId()); - JSONObject jsonObject = JSONUtil.parseObj(goodsIndex); - jsonObject.set("releaseTime", goodsIndex.getReleaseTime()); - updateRequest.doc(jsonObject); - request.add(updateRequest); - } - client.bulk(request, RequestOptions.DEFAULT); - } catch (IOException e) { - log.error("批量更新商品索引异常", e); + JSONObject jsonObject = JSONUtil.parseObj(goodsIndex); + jsonObject.set("releaseTime", goodsIndex.getReleaseTime()); + updateRequest.doc(jsonObject); + request.add(updateRequest); } + this.client.bulkAsync(request, RequestOptions.DEFAULT, new ActionListener() { + @Override + public void onResponse(BulkResponse bulkItemResponses) { + if (bulkItemResponses.hasFailures()) { + throw new RetryException("批量更新商品索引失败,es内容版本冲突"); + } + } + + @Override + public void onFailure(Exception e) { + log.error("批量更新商品索引异常", e); + } + }); } /** @@ -469,14 +483,21 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements deleteByQueryRequest.setQuery(boolQueryBuilder); deleteByQueryRequest.indices(getIndexName()); deleteByQueryRequest.setConflicts("proceed"); - try { - BulkByScrollResponse bulkByScrollResponse = client.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT); - if (bulkByScrollResponse.getVersionConflicts() > 0) { - throw new RetryException("删除索引失败,es内容版本冲突"); + this.client.deleteByQueryAsync(deleteByQueryRequest, RequestOptions.DEFAULT, new ActionListener() { + + @Override + public void onResponse(BulkByScrollResponse bulkByScrollResponse) { + if (bulkByScrollResponse.getVersionConflicts() > 0) { + throw new RetryException("删除索引失败,es内容版本冲突"); + } } - } catch (IOException e) { - log.error("删除索引异常", e); - } + + @Override + public void onFailure(Exception e) { + throw new RetryException("删除索引失败," + e.getMessage()); + } + }); + } /** @@ -883,16 +904,22 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements if (bulkRequest.requests().isEmpty()) { return; } - try { - BulkResponse responses = this.client.bulk(bulkRequest, RequestOptions.DEFAULT); - if (responses.hasFailures()) { - log.info("批量更新商品索引的促销信息中出现部分异常:{}", responses.buildFailureMessage()); - } else { - log.info("批量更新商品索引的促销信息结果:{}", responses.status()); + this.client.bulkAsync(bulkRequest, RequestOptions.DEFAULT, new ActionListener() { + @Override + public void onResponse(BulkResponse bulkItemResponses) { + if (bulkItemResponses.hasFailures()) { + log.info("批量更新商品索引的促销信息中出现部分异常:{}", bulkItemResponses.buildFailureMessage()); + } else { + log.info("批量更新商品索引的促销信息结果:{}", bulkItemResponses.status()); + } } - } catch (IOException e) { - log.error("批量更新商品索引的促销信息出现异常!", e); - } + + @Override + public void onFailure(Exception e) { + log.error("批量更新商品索引的促销信息出现异常!", e); + } + }); + } /**