Authored by LUOXC

test

package com.yohoufo.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by yoho on 2015/11/12.
*/
public class ExecutorServiceUtils {
private static Logger logger = LoggerFactory.getLogger(ExecutorServiceUtils.class);
public static void shutdownAndAwaitTermination(ExecutorService pool) {
shutdownAndAwaitTermination(pool, 10, TimeUnit.SECONDS);
}
public static void shutdownAndAwaitTermination(ExecutorService pool, long timeout, TimeUnit unit) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(timeout, unit)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(timeout, unit)) {
logger.error("Pool did not terminate");
}
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
... ...
... ... @@ -7,10 +7,13 @@ import com.yohoufo.common.annotation.IgnoreSignature;
import com.yohoufo.common.annotation.InnerApi;
import com.yohoufo.common.cache.RedisLock;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.ExecutorServiceUtils;
import com.yohoufo.common.utils.RandomUtil;
import com.yohoufo.order.model.QuickDeliverOrderContext;
import com.yohoufo.order.model.request.TransferMoneyRequest;
import com.yohoufo.order.service.impl.TransferService;
import com.yohoufo.order.utils.NamedThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
... ... @@ -18,8 +21,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
@Slf4j
@RestController
@RequestMapping("/erp/order/help")
public class OrderHelpController {
... ... @@ -52,27 +60,36 @@ public class OrderHelpController {
@InnerApi
@RequestMapping(value = "/lock")
public ApiResponse lock(String key) {
ExecutorService executorService = new ThreadPoolExecutor(5, 10,
60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000), NamedThreadFactory.newThreadFactory("test"));
IntStream.range(0, 100)
.forEach(i -> executorService.submit(() -> lockTest(key)));
ExecutorServiceUtils.shutdownAndAwaitTermination(executorService);
return new ApiResponse.ApiResponseBuilder()
.code(200)
.message("ok")
.build();
}
private void lockTest(String key) {
RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
.appendFixed("ufo:order:lock:test:")
.appendVar(key);
String value = UUID.randomUUID().toString();
String message;
if (redisLock.acquire(redisLockKey, value, 5, TimeUnit.SECONDS)) {
try {
log.info("lock test {}, {} i got the lock", key, value);
Thread.sleep(RandomUtils.nextInt(10, 50));
message = "ok";
} catch (InterruptedException e) {
message = "ko doing";
} finally {
redisLock.release(redisLockKey, value);
}
} else {
message = "ko acquire";
log.info("lock test {}, {} i not got the lock", key, value);
}
return new ApiResponse.ApiResponseBuilder()
.code(200)
.message(message)
.build();
}
... ...