0%

很多时候写功能或者接口需要进行压力测试,
今天发现jwt在生成token的时候,如果输入都是一样的
仅有一个签发时间不一样,生成的token是有可能是一样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void testCreate() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("hisenyuan").build();
ExecutorService pool = new ThreadPoolExecutor(
20,
50,
10000L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(10240),
namedThreadFactory,
new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 50; i++) {
// 需要提交的内容
pool.execute(this::createTokenTest);
}
pool.shutdown();
try {
while (!pool.awaitTermination(500, TimeUnit.MILLISECONDS)) {
LOGGER.debug("Waiting for terminate");
}
} catch (InterruptedException e) {
LOGGER.error(e);
}
}

springmvc正常情况下redirect并且设置指定响应码,异常情况下返回json数据

背景介绍

需求就是正常情况下能redirect到指定的页面
异常的情况下,能够返回JSON格式的错误信息
正常情况和异常情况都需要设置HTTP Code

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String webCharge(HttpServletRequest request, HttpServletResponse response) {
if (1==1) {
response.setStatus(200);
return "redirect:https://github.com/hisenyuan";
} else {
try {
response.setStatus(405);
response.getWriter().write(JSON.toJSONString("hisenyuan"));
return null;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}

一、背景

在各种系统需要加签的时
一般都会把参与签名的数据以get请求参数拼接起来
并且要求有序,这个方法会比较方便

二、实现

2.1 拼接为有序的get请求类字符串

1
2
3
4
5
6
7
8
9
10
public String getSortedStr(Map<String, String> unSortedStr) {
String sortedStr= unSortedStr
.entrySet()
.stream()
.filter(entry -> !StringUtil.isEmpty(entry.getValue()))
.sorted(Map.Entry.comparingByKey())
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
return sortedStr;
}

2.2 把get类参数字符串转为map

1
2
3
4
5
6
7
8
9
private Map<String,String> getMapData(String getStr){
String[] strs = getStr.split("&");
HashMap<String, String> dataMap = new HashMap<>(16);
for (int i = 0; i < strs.length; i++) {
String[] str = strs[i].split("=");
dataMap.put(str[0], str[1]);
}
return dataMap;
}

对于get类字符串没有发现比较好的方法转换为map

这个接口为免费的

一、接口地址

等号后面的为需要翻译的英文

1
http://fanyi.youdao.com/openapi.do?keyfrom=xinlei&key=759115437&type=data&doctype=json&version=1.1&q=hisen

二、代码样例

把全世界200+国家和地区的名字翻译为英文,并且入库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Autowired
SmsCountryService countryService;

@Test
public void updateZhName(){
// 有道翻译接口
String url = "http://fanyi.youdao.com/openapi.do?keyfrom=xinlei&key=759115437&type=data&doctype=json&version=1.1&q=";
// 查询出所有的英文国家名字
List<SmsCountry> countries = countryService.queryAllName();
// httpclient
CloseableHttpClient client = HttpClientBuilder.create().build();
// 翻译每个国家的名字,并且更新数据库
for (SmsCountry country:countries) {
HttpGet request = new HttpGet(url + URLEncoder.encode(country.getName()));
try {
CloseableHttpResponse response = client.execute(request);
String str = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject jsonObject = JSON.parseObject(str);
// 取出json字符串中数组的值
String s = (String) jsonObject.getJSONArray("translation").get(0);
System.out.println(">>>>> " + s);
if (!StringUtil.isEmpty(s)){
SmsCountry smsCountry = new SmsCountry();
smsCountry.setId(country.getId());
smsCountry.setNameZh(s);
countryService.updateNameById(smsCountry);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

利用Java8的流和lambda表达式能很方便的对list对象进行去重
而且不会造成代码入侵

插播:Java8 对List进行求和、分组、提取对象单个属性:https://www.jianshu.com/p/c71eaeaaf30c

下面的例子仅供参考
github:https://github.com/hisenyuan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.hisen.collection.list.duplicate;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import Java.util.ArrayList;
import Java.util.List;
import Java.util.concurrent.ConcurrentHashMap;
import Java.util.function.Function;
import Java.util.function.Predicate;
import Java.util.stream.Collectors;

/**
* @author hisenyuan
* @time 2018/4/19 14:06
* @description list对象根据属性去重 lambda + stream
*/
public class ListDuplicateTest {


public static void main(String[] args) {
List<Person> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Person person = new Person();
person.setAge(10);
person.setName("hsien");
person.setWeight(i);
list.add(person);
}
Person bean = new Person();
bean.setName("hisenyuan");
bean.setAge(33);
bean.setWeight(65);
list.add(bean);

List<Person> collect = list.stream().filter(distinctByKey(Person::getName))
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(collect));
}

/**
* 函数式接口 T -> bollean
* @param keyExtractor
* @param <T>
* @return
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>(16);
return t -> map.putIfAbsent(keyExtractor.apply(t),Boolean.TRUE) == null;

// 这个也可以,不过感觉效率要低一些,线程不是那么安全
// Set<Object> seen = ConcurrentHashMap.newKeySet();
// return t -> seen.add(keyExtractor.apply(t));
}

/**
* 内部类
*/
static class Person {
private int age;
private String name;
private int weight;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getWeight() {
return weight;
}

public Person() {
}
public void setWeight(int weight) {
this.weight = weight;
}
}
}

1
effective Java # 2

Builder模式,不直接生成想要的对象,而是让客户端利用所有必要的参数调用构造器,

得到一个builder对象。然后客户端在builder对象上调用类似于setter的方法,

来设置每个相关可选的参数,最后调用无参的build来生成不可变的对象。

完整代码+测试:github:完整代码+测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class NutritionFacts {

private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;

private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}

public static class Builder {

// Required parameters
private final int servingSize;
private final int servings;

private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;

public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}

public Builder calories(int val) {
calories = val;
return this;
}

public Builder fat(int val) {
this.fat = val;
return this;
}

public Builder carbohydrate(int val) {
this.carbohydrate = val;
return this;
}

public Builder sodium(int val) {
this.sodium = val;
return this;
}

public NutritionFacts build() {
return new NutritionFacts(this);
}
}



@Override
public String toString() {
return "NutritionFacts{" +
"servingSize=" + servingSize +
", servings=" + servings +
", calories=" + calories +
", fat=" + fat +
", sodium=" + sodium +
", carbohydrate=" + carbohydrate +
'}';
}
}

effective Java:

1
单元素的枚举类型已经成为实现Singleton的最佳方法

理由:

  1. 因为枚举单例有序列化和线程安全的保证
  2. 避免反射和并发困扰

单例模式模式:完整代码+测试

主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class EnumSingleton {

private EnumSingleton() {
}

public static EnumSingleton getInstance() {
return Singleton.INSTANCE.getInstance();
}

private enum Singleton {
INSTANCE;
private EnumSingleton singleton;

Singleton() {
singleton = new EnumSingleton();
}

public EnumSingleton getInstance() {
return singleton;
}
}
}

忘记开始是在什么地方看到docker这个东西

后面觉得挺好玩,也试了很多次,找过很多的教程。

搭建了一个zookeeper的集群(docker-compose)

然后当我需要搭建redis集群的时候,发现里面很多的概念还不是很懂

比如:volume network

然后加了docker的群,遇到了在以前idea群里面熟的一个人

花了一天的时间看完他传的一本书,昨天买的几本书晚上也到了。
(docker从入门到实践、Java并发编程的艺术、effective Java中文版 2)

这本书总体来说还行,就是过时了,还在用link,毕竟三年前的东西

有些例子也报错,主要是ruby相关的不行,提醒需要2.2以上的版本

下面是随便做的一点笔记

Read more »

一个简单的工具类,更多Java小练习:https://github.com/hisenyuan/IDEAPractice

一、添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- POI start -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<!-- POI end -->

二、测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.hisen.jars.poi;

import Java.io.File;
import Java.io.IOException;
import Java.util.Arrays;
import Java.util.List;

/**
* @author hisenyuan
* @time 2018/1/12 17:43
* @description 测试读取
*/
public class TestPoiExcelUtil {

public static void main(String[] args) {
File file = new File("C:\\work\\document\\银行信息.xlsx");
try {
// 每一个excelData为一行数据(存放在数组)
List<String[]> excelData = POIExcelUtil.readExcel(file);
for (String[] data:excelData) {
System.out.println(Arrays.toString(data));
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

三、工具类代码

完整类:POIExcelUtil.Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.hisen.jars.poi;

import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileNotFoundException;
import Java.io.IOException;
import Java.io.InputStream;
import Java.util.ArrayList;
import Java.util.List;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;

/**
* @author hisenyuan
* @time 2018/1/12 16:35
* @description 利用POI读取excel表格
*/
public class POIExcelUtil {

private static Logger logger = Logger.getLogger(POIExcelUtil.class);
private final static String XLS = "xls";
private final static String XLSX = "xlsx";

public static List<String[]> readExcel(File file) throws IOException {
// 检查文件
checkFile(file);
Workbook workBook = getWorkBook(file);
// 返回对象,每行作为一个数组,放在集合返回
ArrayList<String[]> rowList = new ArrayList<>();
if (null != workBook) {
for (int sheetNum = 0; sheetNum < workBook.getNumberOfSheets(); sheetNum++) {
// 获得当前sheet工作表
Sheet sheet = workBook.getSheetAt(sheetNum);
if (sheet == null) {
continue;
}
// 获得当前sheet的开始行
int firstRowNum = sheet.getFirstRowNum();
// 获得当前sheet的结束行
int lastRowNum = sheet.getLastRowNum();
// 循环所有行(第一行为标题)
for (int rowNum = firstRowNum; rowNum < lastRowNum; rowNum++) {
// 获得当前行
Row row = sheet.getRow(rowNum);
if (row == null) {
continue;
}
// 获得当前行开始的列
short firstCellNum = row.getFirstCellNum();
// 获得当前行的列数
int lastCellNum = row.getPhysicalNumberOfCells();
String[] cells = new String[row.getPhysicalNumberOfCells()];
// 循环当前行
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
cells[cellNum] = getCellValue(cell);
}
rowList.add(cells);
}
}
}
return rowList;
}
}