Authored by ken.hu

resource

package com.yohoufo.common.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/**
* A handler for rejected tasks that silently discards the
* rejected task.
*/
public class DiscardPolicyRejectedExecutionHandler implements RejectedExecutionHandler {
private static final Logger log = LoggerFactory.getLogger(DiscardPolicyRejectedExecutionHandler.class);
/**
* Creates a {@code DiscardPolicyRejectedExecutionHandler}.
*/
public DiscardPolicyRejectedExecutionHandler() {
}
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
log.warn("Task {} rejected from {}", r, e);
}
}
\ No newline at end of file
... ...
... ... @@ -8,23 +8,20 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.yoho.core.rest.annotation.ServiceDesc;
import com.yoho.service.model.resource.request.ResourcesRequestBody;
import com.yoho.tools.docs.ApiOperation;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.common.annotation.IgnoreSession;
import com.yohoufo.common.annotation.IgnoreSignature;
import com.yohoufo.resource.service.IResourcesService;
@RestController
@RequestMapping(value = "/resources")
@ServiceDesc(serviceName = "resources")
public class ResourcesController {
public class ResourceController {
private static final Logger logger = LoggerFactory.getLogger(ResourceController.class);
private static final Logger logger = LoggerFactory.getLogger(ResourcesController.class);
@Autowired
private IResourcesService resourcesService;
... ... @@ -33,27 +30,15 @@ public class ResourceController {
* 根据内容码,获取单个内容个体
*
* @return
*//*
*/
@RequestMapping(value = "/get", method = RequestMethod.POST)
@RequestMapping(params = "method=ufo.resources.get")
public @ResponseBody List<Object> get(@RequestBody ResourcesRequestBody request) {
logger.info("Get content by content code[{}] and client type [{}].", request.getContentCode(),
public @ResponseBody ApiResponse get(@RequestBody ResourcesRequestBody request) {
logger.info("Get resources by content code[{}] and client type [{}].", request.getContentCode(),
request.getClientType());
List<Object> data = resourcesService.get(request);
logger.info("Get content by content code[{}] and client type [{}] success.", request.getContentCode(),
logger.info("Get resources by content code[{}] and client type [{}] success.", request.getContentCode(),
request.getClientType());
return data;
}*/
@ApiOperation(name = "ufo.resource.get", desc="资源位")
@IgnoreSignature
@IgnoreSession
@RequestMapping(params = "method=ufo.resource.get")
public ApiResponse get(
@RequestParam(value = "product_id", required = false) Integer productId) {
System.out.println("ss");
return new ApiResponse.ApiResponseBuilder().data("sds").code(200).message("product data").build();
}
return new ApiResponse.ApiResponseBuilder().data(data).code(200).message("resources data").build();
}
}
... ...
... ... @@ -14,16 +14,4 @@ public interface IResourcesService {
*/
List<Object> get(ResourcesRequestBody request);
/**
* 推荐楼层回填
* @param contentCode
* @param clientType
* @param uid
* @param udid
* @param gender
* @param templateId
* @return
*//*
List<Object> handlerRecommendFloors(String contentCode, String clientType, Integer uid, String udid, String gender,
List<Integer> templateId);*/
}
... ...
package com.yohoufo.resource.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/*import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;*/
import com.google.common.collect.Lists;
/*import com.yoho.core.config.ConfigReader;
import com.yoho.core.config.ZKConfigWriter;
import com.yoho.service.model.resource.ResourcesBO;*/
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
... ... @@ -28,31 +42,11 @@ import com.yohoufo.resource.service.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@Slf4j
public class ResourcesServiceImpl implements IResourcesService {
/* public static final ResourcesBO EMPTY_RESOURCES_BO;
static {
EMPTY_RESOURCES_BO = new ResourcesBO();
EMPTY_RESOURCES_BO.setList(Collections.emptyList());
}*/
/* private int resourcesMetadataCacheTag;*/
@javax.annotation.Resource(name="NoSyncGracefulRedisTemplate")
private NoSyncGracefulRedisTemplate resourcesRedisCache;
... ... @@ -68,14 +62,6 @@ public class ResourcesServiceImpl implements IResourcesService {
@Autowired
private IResourceParseService resourceParseService;
/* private Cache<String, Optional<ResourcesMetadata>> resourcesMetadataCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(5, TimeUnit.MINUTES).build();
@javax.annotation.Resource(name = "core-config-writer")
private ZKConfigWriter zkConfigWriter;
@javax.annotation.Resource(name = "core-config-reader")
private ConfigReader configReader;*/
@Autowired
@Qualifier("resourceAsyncTaskExecutor")
private AsyncTaskExecutor resourceAsyncTaskExecutor;
... ... @@ -124,25 +110,6 @@ public class ResourcesServiceImpl implements IResourcesService {
}
}
/* @Override
public List<Object> handlerRecommendFloors(String contentCode, String clientType,Integer uid,String udid,String gender,List<Integer> templateId){
RedisKeyBuilder cacheKey = CacheEnum.RESOURCE_RECOMMEND_FLOORS.generateKeyLowerCase(contentCode, clientType, String.valueOf(uid), udid, gender);
List recommendFloors = resourcesRedisCache.get(cacheKey, List.class);
if (Objects.nonNull(recommendFloors)) {
return recommendFloors;
}
Resources resources = new Resources();
resources.setCode(contentCode);
ResourcesMetadata metadata = getResourcesMetadataFromCache(contentCode, templateId);
if (Objects.isNull(metadata)) {
return Collections.emptyList();
}
recommendFloors = parseContentData(metadata,clientType,gender,uid,udid);
resourcesRedisCache.setEx(cacheKey, recommendFloors, CacheEnum.RESOURCE_RECOMMEND_FLOORS.getCacheTime());
return recommendFloors;
}*/
/**
* 查找并解析资源内容数据
*
... ... @@ -213,42 +180,6 @@ public class ResourcesServiceImpl implements IResourcesService {
return list;
}
/* private ResourcesMetadata getResourcesMetadataFromCache(@NonNull String contentCode, List<Integer> resourcesContentIds) {
if (CollectionUtils.isEmpty(resourcesContentIds)) {
return null;
}
ResourcesMetadata metadata = getResourcesMetadataFromCache(contentCode);
if (Objects.isNull(metadata)) {
return null;
}
metadata.getResourcesContentDatas().removeIf(e -> !resourcesContentIds.contains(e.getResourceContentId()));
return metadata;
}*/
/* private ResourcesMetadata getResourcesMetadataFromCache(@NonNull String contentCode) {
try {
//判断静态对象的值是否和zk中的数值相等
int resourcesMetadataCacheTag = configReader.getInt(Constant.RESOURCES_METADATA_CACHE, 0);
if (this.resourcesMetadataCacheTag != resourcesMetadataCacheTag) {
log.info("clear Metadata cache");
resourcesMetadataCache.invalidateAll();
this.resourcesMetadataCacheTag = resourcesMetadataCacheTag;
}
// 从内存中取出数据的克隆对象
Optional<ResourcesMetadata> metadata = resourcesMetadataCache.get(contentCode, () -> getResourcesMetadataFromDatabase(contentCode));
if (metadata.isPresent()) {
return metadata.get().cloneBean();
} else {
return null;
}
} catch (ExecutionException e) {
log.error("get resources metadata from cache fail:{},{}", contentCode, e.getMessage());
throw new RuntimeException(e);
}
}*/
private Optional<ResourcesMetadata> getResourcesMetadataFromDatabase(@NonNull String contentCode) {
log.info("get resources metadata from database by {}.", contentCode);
Resources resource = getResource(contentCode);
... ...
... ... @@ -27,6 +27,11 @@
<artifactId>yohoufo-fore-product</artifactId>
</dependency>
<dependency>
<groupId>com.yohoufo.fore</groupId>
<artifactId>yohoufo-fore-resource</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-trace</artifactId>
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="resourceAsyncTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="50" />
<!-- 缓存队列 -->
<property name="queueCapacity" value="500" />
<!-- 对拒绝task的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="com.yohoufo.common.task.DiscardPolicyRejectedExecutionHandler" />
</property>
</bean>
</beans>
\ No newline at end of file
... ...
... ... @@ -39,4 +39,15 @@ datasources:
- com.yoho.favorite.dal.BrandFavoriteTotalMapper
- com.yoho.favorite.dal.ShopsFavoriteNumBaseMapper
- com.yoho.other.dal.ShareCommandMapper
ufo_resource:
servers:
- 192.168.102.219:3306
- 192.168.102.219:3306
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
daos:
- com.yohoufo.dal.resource.ResourcesContentDataMapper
- com.yohoufo.dal.resource.ResourcesContentMapper
- com.yohoufo.dal.resource.ResourcesMapper
readOnlyInSlave: true
\ No newline at end of file
... ...
... ... @@ -39,4 +39,15 @@ datasources:
- com.yoho.favorite.dal.BrandFavoriteTotalMapper
- com.yoho.favorite.dal.ShopsFavoriteNumBaseMapper
- com.yoho.other.dal.ShareCommandMapper
ufo_resource:
servers:
- 192.168.102.219:3306
- 192.168.102.219:3306
username: yh_test
password: 9nm0icOwt6bMHjMusIfMLw==
daos:
- com.yohoufo.dal.resource.ResourcesContentDataMapper
- com.yohoufo.dal.resource.ResourcesContentMapper
- com.yohoufo.dal.resource.ResourcesMapper
readOnlyInSlave: true
\ No newline at end of file
... ...