1.线程池工具类
package com.idc.utils;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class CommonsThreadPoolUtils {
private static final int CPU_NUMS = Runtime.getRuntime().availableProcessors();
private static final int MAXIMUM_POOL_SIZE = CPU_NUMS * 2 + 1;
private static final int BLOCKING_QUEUE_LENGTH = 1000;
private static final int KEEP_ALIVE_TIME = 60;
private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
private static final String THREAD_NAME = "CommonsThreadPoolUtils";
private static ThreadPoolExecutor executor;
private CommonsThreadPoolUtils(){
}
public static ThreadPoolExecutor getThreadPool() {
if (executor == null) {
synchronized (CommonsThreadPoolUtils.class) {
if (executor == null) {
executor = new ThreadPoolExecutor(
MAXIMUM_POOL_SIZE - 1,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE_TIME,
KEEP_ALIVE_TIME_UNIT,
new LinkedBlockingDeque<>(BLOCKING_QUEUE_LENGTH),
new DefaultThreadFactory(THREAD_NAME),
new ThreadPoolExecutor.AbortPolicy() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
log.warn("线程等待队列已满,当前运行线程总数:{},活动线程数:{},等待运行任务数:{}", e.getPoolSize(), e.getActiveCount(), e.getQueue().size());
}
});
}
}
}
return executor;
}
public static void execute(Runnable runnable) {
if (runnable != null) {
getThreadPool().execute(runnable);
}
}
public static void cancel(Runnable runnable) {
if (executor != null) {
getThreadPool().getQueue().remove(runnable);
}
}
public static int getSize() {
return getThreadPool().getPoolSize();
}
public static void shutdown() {
if (!getThreadPool().isShutdown()) {
getThreadPool().shutdown();
}
}
public static int getActiveCount() {
return getThreadPool().getActiveCount();
}
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = name + "-pool-" + poolNumber.getAndIncrement() + "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,namePrefix + threadNumber.getAndIncrement(),0);
if (t.isDaemon()){
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY){
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
}
2.POI Excel工具类
package com.jwyy.utils;
import com.jwyy.model.ExcelModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class ExcelUtils {
private static final String excel07 = "xlsx";
private static final String excel03 = "xls";
private static final int skipRow = 2;
public static List<ExcelModel> readExcel(String filePath) {
String fileSuffix = filePath.substring(filePath.lastIndexOf('.') + 1);
if (!excel07.equalsIgnoreCase(fileSuffix) && !excel03.equalsIgnoreCase(fileSuffix)) {
throw new RuntimeException("不支持的格式,请上传xls或xlsx格式的文件");
}
List<ExcelModel> values = null;
try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
values = readExcel(fileSuffix, fileInputStream);
} catch (Exception e) {
log.error("读取excel出现异常:", e);
}
return values;
}
public static List<ExcelModel> readExcel(MultipartFile file) throws IOException {
String filePath = file.getOriginalFilename();
String fileSuffix = filePath.substring(filePath.lastIndexOf('.') + 1);
return readExcel(fileSuffix,file.getInputStream());
}
public static List<ExcelModel> readExcel(String fileSuffix, InputStream inputStream) {
Workbook workbook = null;
POIFSFileSystem fileSystem = null;
List<ExcelModel> values = null;
try {
if (excel07.equalsIgnoreCase(fileSuffix)) {
workbook = new XSSFWorkbook(inputStream);
} else if (excel03.equalsIgnoreCase(fileSuffix)) {
fileSystem = new POIFSFileSystem(inputStream);
workbook = new HSSFWorkbook(fileSystem);
}
if (null == workbook) {
throw new RuntimeException("excel读取类型错误");
}
values = getValue(workbook,skipRow);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(fileSystem);
}
return values;
}
private static List<ExcelModel> getValue(Workbook workbook,int skipRow) {
Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
int lastRow = sheet.getLastRowNum();
List<ExcelModel> values = new ArrayList<>();
for (int i = skipRow; i <= lastRow; i++) {
Row row = sheet.getRow(i);
if (null == row) {
continue;
}
ExcelModel excelModel = buildExcelModel(row);
values.add(excelModel);
}
fillExcelModel(values);
return values;
}
private static ExcelModel buildExcelModel(Row row){
short cellNum = row.getLastCellNum();
ExcelModel rowValue = new ExcelModel();
for (int j = 0; j <= cellNum; j++) {
Cell cell = row.getCell(j);
if (null != cell) {
Field[] fields = rowValue.getClass().getDeclaredFields();
for (Field field : fields) {
ExcelModel.ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelModel.ExcelProperty.class);
if (excelProperty != null && excelProperty.index() == j) {
try {
String name = field.getName();
String setterMethod = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = rowValue.getClass().getDeclaredMethod(setterMethod, String.class);
method.invoke(rowValue, getCellValue(cell));
} catch (Exception e) {
log.error("反射封装属性错误:", e);
}
}
}
}
}
return rowValue;
}
private static void fillExcelModel(List<ExcelModel> values){
String orderNumber = null;
String targetName = null;
String targetValue = null;
String targetGrate = null;
String targetDefinition = null;
String scoringMethod = null;
for (int i = 0; i < values.size(); i++) {
ExcelModel excelModel = values.get(i);
if (StringUtils.isNotBlank(excelModel.getOrderNumber())){
orderNumber = excelModel.getOrderNumber();
}else{
excelModel.setOrderNumber(orderNumber);
}
if (StringUtils.isNotBlank(excelModel.getTargetName())){
targetName = excelModel.getTargetName();
}else{
excelModel.setTargetName(targetName);
}
if (StringUtils.isNotBlank(excelModel.getTargetValue())){
targetValue = excelModel.getTargetValue();
}else{
excelModel.setTargetValue(targetValue);
}
if (StringUtils.isNotBlank(excelModel.getTargetGrate())){
targetGrate = excelModel.getTargetGrate();
}else{
excelModel.setTargetGrate(targetGrate);
}
if (StringUtils.isNotBlank(excelModel.getTargetDefinition())){
targetDefinition = excelModel.getTargetDefinition();
}else{
excelModel.setTargetDefinition(targetDefinition);
}
if (StringUtils.isNotBlank(excelModel.getScoringMethod())){
targetDefinition = excelModel.getScoringMethod();
}else{
excelModel.setScoringMethod(scoringMethod);
}
}
}
private static String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
Object value = "";
switch (cell.getCellType()) {
case BLANK:
break;
case ERROR:
value = cell.getErrorCellValue();
break;
case STRING:
value = cell.getStringCellValue();
break;
case BOOLEAN:
value = cell.getBooleanCellValue();
break;
case NUMERIC:
value = cell.getNumericCellValue();
DecimalFormat df = new DecimalFormat("0");
value = df.format(value);
break;
case _NONE:
}
return String.valueOf(value);
}
}
@Data
public class ExcelModel {
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelProperty{
int index();
String value();
}
@TableId(type = IdType.AUTO)
private String id;
@ExcelProperty(index = 0,value = "序号")
private String orderNumber;
}
3.自定义token工具类
@Slf4j
public class TwoFactorTokenUtils {
private static Map<String, String> userToken = new HashMap<>(8);
private static Map<String, Long> tokenInvalid = new HashMap<>(8);
private static long expireTime = 1000 * 60 * 30;
private static long refreshTime = 1000 * 60 * 5;
public static boolean checkToken(String token) {
refresh(token);
printTokenInfo();
return tokenInvalid.containsKey(token);
}
public static void add(String userId, String token) {
userToken.put(userId, token);
tokenInvalid.put(token, System.currentTimeMillis() + expireTime);
log.info("用户userId:{},生成token:{}", userId, token);
}
public static void remove(String userId) {
String token = userToken.remove(userId);
tokenInvalid.remove(token);
log.info("删除用户userId:{},token:{}", userId, token);
}
public static void refresh(String token) {
synchronized (TwoFactorTokenUtils.class){
Long currTokenExpireTime = tokenInvalid.get(token);
int secondUnit = 1000;
if (currTokenExpireTime != null && ((currTokenExpireTime - System.currentTimeMillis()) / secondUnit) <= (refreshTime / secondUnit)) {
log.info("token:{}续签成功", token);
tokenInvalid.put(token, System.currentTimeMillis() + expireTime);
}
Set<String> userIds = new HashSet<>();
Iterator<Map.Entry<String, String>> userTokenItr = userToken.entrySet().iterator();
while (userTokenItr.hasNext()) {
Map.Entry<String, String> nextToken = userTokenItr.next();
String userId = nextToken.getKey();
String userToken = nextToken.getValue();
Long tokenExpireTime = tokenInvalid.get(userToken);
if (tokenExpireTime != null && ((tokenExpireTime - System.currentTimeMillis()) / secondUnit) <= 0) {
userIds.add(userId);
}
}
for (String userId : userIds) {
log.info("用户userId:{},token已过期", userId);
remove(userId);
}
}
}
public static String generateToken(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static void printTokenInfo(){
Iterator<Map.Entry<String, String>> iterator = userToken.entrySet().iterator();
log.info("====================在线用户列表====================");
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
log.info("当前在线userId:{},token:{}",next.getKey(),next.getValue() );
}
}
public static void main(String[] args) throws InterruptedException {
TwoFactorTokenUtils.add("10001", "10001-token-id");
TwoFactorTokenUtils.add("10002", "10002-token-id");
TwoFactorTokenUtils.add("10003", "10003-token-id");
int i = 0;
while (true) {
Thread.sleep(1000);
log.info("模拟web server 运行......" + i);
TwoFactorTokenUtils.checkToken("10001-token-id");
if (i == 121) {
TwoFactorTokenUtils.checkToken("10002-token-id");
TwoFactorTokenUtils.checkToken("10003-token-id");
}
i++;
}
}
}
4.动态数组工具类
@Slf4j
public class DataMonitorUtils {
private int position;
private int minCount;
private int capacity;
private int[] array;
public DataMonitorUtils(int capacity, int minCount) {
this.capacity = capacity;
this.minCount = minCount;
this.position = 0;
this.array = new int[capacity];
}
public void push(int data, Consumer<String> messageConsumer) {
if (this.position >= this.array.length) {
for (int i = 0; i < this.position; i++) {
if (i < this.position - 1) {
this.array[i] = this.array[i + 1];
} else {
this.array[this.position - 1] = data;
}
}
} else {
this.array[this.position++] = data;
}
if (this.position == this.array.length && this.check()) {
this.sendMsg(messageConsumer);
this.reset();
}
}
public void reset() {
this.position = 0;
for (int i = 0; i < this.array.length; i++) {
this.array[i] = 0;
}
}
public boolean check() {
for (int i = 0; i < this.array.length; i++) {
if (this.array[i] > minCount) {
return false;
}
}
return true;
}
public void print() {
log.info("====================遍历数据集合=========================");
for (int i = 0; i < this.array.length; i++) {
log.info("打印数据array[{}] = {}", i, this.array[i]);
}
}
public void sendMsg(Consumer<String> messageConsumer) {
String message = "this is a message";
messageConsumer.accept(message);
log.info("this is a message log");
}
}