0%

一、背景简介

今天头给我们开会,说到团队对外沟通的问题。

谈到对外需要积极给人解决问题,而不是各种推脱,即使自己不知道,也可以给个眼神找到对的人。

继而谈到需要安排人轮流负责跟外部接洽

由于这个活呢,大伙儿认为不是什么好差事,那就抓阄决定吧

于是乎就感觉可以写一个简单的排班系统小bug,不过我这里只是提供一个简单的思路

二、程序代码

主要的逻辑在这,当然并没有考虑数据持久化的问题

性能等其他的问题,纯粹是一个思路,用hashCode取模主要是打的比较散,很均匀

加上日期什么的,就一个排班表出来了。

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
/**
*
* @param person 目前所有的人
* @param lucklys 这一轮已经值班了的人
*/

private static void findLucklyOneByHashCode(List<String> person, ArrayList<String> lucklys) {
List<String> result = new ArrayList<>(person);
result.removeAll(lucklys);
if (result.size() == 0) {
lucklys.clear();
result = new ArrayList<>(person);
}
String code = System.nanoTime() + UUID.randomUUID().toString();
int index = Math.abs(code.hashCode()) % result.size();
String lucklyOne = result.get(index);
System.out.println("lucklyOne:" + lucklyOne);
lucklys.add(lucklyOne);
}
/**
* hashCode随机取出来的数据
* lucklyOne:G
* lucklyOne:B
* lucklyOne:C
* lucklyOne:D
* lucklyOne:H
* lucklyOne:F
* lucklyOne:E
* lucklyOne:A
*/

mac自带的终端总感觉不大好用,于是乎就被安利了iTerm2

然后就搜了下自动连接远程服务,于是就发现了一个不错的脚步

整个设置过程还是比较顺畅,

操作步骤:

  1. 新建一个sh文件
1
vi auto_ssh.sh
  1. 输入如下内容,[lindex $argv 0] 这个为第一个参数的占位符
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/expect

set timeout 30
spawn ssh -p[lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]
expect {
"(yes/no)?"
{send "yes\n";exp_continue}
"password:"
{send "[lindex $argv 3]\n"}
}
interact
  1. 复制脚本到bin下并且赋予执行权限
1
2
3
sudo cp auto_ssh.sh /usr/local/bin/
cd /usr/local/bin/
sudo chmod +x auto_ssh.sh
  1. 设置
    在iTerm2中Command+o呼出profile
1
2
3
4
5
6
7
8
9
10
11
12
13
# 界面右下角,点击:
edit profiles
# 界面左下角,点击:
+
# 界面上方,点击:
General
# 界面靠上,写这个登录的名字:
name
# 界面中间,点击选择:
Login shell
# 找到这个提示符:Send text at start,输入如下字符
auto_ssh.sh 8022 root 10.10.20.20 hisenyuan
# 脚本名称 端口 用户名 ip地址 密码
  1. 运行
    设置完了之后,关闭窗口,重新command+o呼出配置文件,双击刚刚配置的即可自动登录

重温经典,特意看了下为什么读文件非得不让等于 -1

fileChannelIn.read(byteBuffer) != -1

EOF = End Of File,其默认值就是-1

这是一个约定好的结束符,不是认为改变的

代码

建议使用大文件进行测试,看效果比较明显

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
89
90
@Test
public void testCopy() {
String oldFileName = "/Users/hisenyuan/hisen/blog/source/_posts/Test-Java-Code.md";
String newFileName = "/Users/hisenyuan/hisen/test/Test-Java-Code.md";
nioCopy(oldFileName, newFileName);
ioCopy(oldFileName, newFileName.replace(".md", ".md.bak"));
ioCopyByLine(oldFileName,newFileName.replace(".md",".bak." + System.currentTimeMillis()));
}

/**
* 利用NIO进行读写文件
*
* @param oldFileName 原文件的路径
* @param newFileName 新文件的路径
*/
public static void nioCopy(String oldFileName, String newFileName) {
try {
FileChannel fileChannelIn = new FileInputStream(new File(oldFileName)).getChannel();
FileChannel fileChannelOut = new FileOutputStream(new File(newFileName)).getChannel();
//获取文件大小
long size = fileChannelIn.size();
System.out.printf("文件大小为:%s byte \n", size);
//缓冲
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

long start = System.currentTimeMillis();
while (fileChannelIn.read(byteBuffer) != -1) {
//准备写
byteBuffer.flip();
fileChannelOut.write(byteBuffer);
//准备读
byteBuffer.clear();
}
long end = System.currentTimeMillis();
System.out.printf("NIO方式复制完成,耗时 %s 秒\n", (end - start) / 1000);
//关闭
fileChannelIn.close();
fileChannelOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* IO方式复制文件
*
* @param oldFileName
* @param newFileName
*/
public static void ioCopy(String oldFileName, String newFileName) {
try {
FileInputStream fileInputStream = new FileInputStream(new File(oldFileName));
FileOutputStream fileOutputStream = new FileOutputStream(new File(newFileName));

long length = new File(oldFileName).length();

System.out.printf("文件大小为:%s byte \n", length);
byte[] buffer = new byte[1024];

long start = System.currentTimeMillis();
int len = 0;
//EOF = End Of File,其默认值就是-1
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
long end = System.currentTimeMillis();
System.out.printf("IO方式复制完成,耗时 %s 秒\n", (end - start) / 1000);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void ioCopyByLine(String oldFileName, String newFileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(oldFileName));
BufferedWriter writer = new BufferedWriter(new FileWriter(newFileName));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.flush();
}
} catch (IOException e) {
System.out.println("error:" + e);
}
}

最近在慢慢适应mbp

然后就在研究怎么方便的写博客

首先就是找了几款markdown编辑器,发现GitHub出品的Atom还不错

插件很丰富,然而下载了第一个terminal插件但是无效,一度折腾了很久

后来搜索了一下,发现这个插件很不错:platformio-ide-terminal

利用快捷键:control + 点(1左边那个)

就可以在当前界面呼出终端,而且是当前目录

也就是在_post目录下,写完了之后执行命令就上传了

还是很方便的。后续还需要慢慢熟悉更多的插件与工具

很多时候写功能或者接口需要进行压力测试,
今天发现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;
}
}
}