Are you still reinventing the wheel? Share these 3 commonly used open source tool libraries to improve production efficiency!

In our actual project development, we are quite taboo to make wheels, but it is definitely beneficial to ourselves to make wheels in the learning process! Making wheels is a special way to improve your system programming ability.

Today I will share a few open source tool libraries that I often use, I hope it will be helpful to my friends!

  1. OSHI[1]  : A JNA-based (native) operating system and hardware information library for the Java language.
  2. EasyExcel[2]  : A fast and simple Java processing Excel tool that avoids OOM.
  3. Hutool[3]  : A very useful Java tool class library that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, regularization, threading, and XML.

The following is a more detailed introduction. It is recommended that you finish reading it so that you can get started quickly and use it in your own projects to improve production efficiency.

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Introduction

OSHI is a JNA-  based (native) operating system and hardware information library for the Java language  .

JNA (Java Native Access) [4] is an open source Java framework, a technology for invoking local methods launched by Sun, and a framework based on the classic JNI. The reason why it is a substitute for JNI is that JNA greatly simplifies the process of calling local methods, it is very convenient to use, basically it can be done without leaving the Java environment.

JNI (Java Native Interface) is a programming interface provided by the JDK, which allows Java programs to call programs or code libraries written in other languages. In fact, the implementation of the JDK itself uses a lot of JNI technology to call local C libraries.

Through OSHI, we do not need to install any other native libraries to view memory and CPU usage, disk and partition usage, equipment, sensors and other information.

OSHI aims to provide a cross-platform implementation to retrieve system information, and supports mainstream operating systems such as Windows, Linux, MacOS, and Unix.

The official introduction of oshi is as follows: (Translation Chrome plug-in: Mate Translate ):

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Using oshi, you can easily create system monitoring functions commonly used in the project, as shown in the following figure:

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Introduce dependencies

Maven

 

<!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
<dependency>
    <groupId>com.github.oshi</groupId>
    <artifactId>oshi-core</artifactId>
    <version>5.2.5</version>
</dependency>

Gradle

 

// https://mvnrepository.com/artifact/com.github.oshi/oshi-core
compile group: 'com.github.oshi', name: 'oshi-core', version: '5.2.5'

Demo

Obtain the hardware information object HardwareAbstractionLayer:

 

//系统信息
SystemInfo si = new SystemInfo();
//操作系统信息
OperatingSystem os = si.getOperatingSystem();
//硬件信息
HardwareAbstractionLayer hal = si.getHardware();

After having the object HardwareAbstractionLayer representing hardware information, we can get hardware-related information!

The following is a brief demonstration of obtaining memory and CPU related information.

1. Get memory related information

 

//内存相关信息
GlobalMemory memory = hal.getMemory();
//获取内存总容量
String totalMemory = FormatUtil.formatBytes(memory.getTotal());
//获取可用内存的容量
String availableMemory = FormatUtil.formatBytes(memory.getAvailable());

With the total memory capacity and the available memory capacity, you can calculate the current memory utilization.

2. Get CPU related information

 

//CPU相关信息
CentralProcessor processor = hal.getProcessor();
//获取CPU名字
String processorName = processor.getProcessorIdentifier().getName();
//获取物理CPU数
int physicalPackageCount = processor.getPhysicalPackageCount();
//获取物理核心数
int physicalProcessorCount = processor.getPhysicalProcessorCount();

EasyExcel

Introduction

Java parsing, generating Excel common framework Apache poi, jxl, but the two are not enough to use framework elegant, and very memory consumption , severe cases can lead to memory overflow .

How to solve this problem?

It is recommended that you use Ali's open source EasyExcel. As the official website of this project introduces, this is a fast and simple Java processing Excel tool that avoids OOM.

The official introduction of EasyExcel is as follows:

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Introduce dependencies

Maven

 

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.6</version>
</dependency>

Gradle

 

// https://mvnrepository.com/artifact/com.alibaba/easyexcel
compile group: 'com.alibaba', name: 'easyexcel', version: '2.2.6'

Demo

Here directly share the official example of reading Excel ( in the actual project I did a simple package for this part, and there are many places involved, so I won't share it ).

Entity object  (Excel import and export entity object)

 

@Data
public class DemoData {
    private String string;
    private Date date;
    private Double doubleData;
}

Listener  (customized AnalysisEventListener reads 5 pieces of data at a time and stores it in the database)

 

// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
public class DemoDataListener extends AnalysisEventListener<DemoData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class);
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    List<DemoData> list = new ArrayList<DemoData>();
    /**
     * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。     */
    private DemoDAO demoDAO;
    public DemoDataListener() {
        // 这里是demo,所以随便new一个。实际使用如果到了spring,请使用下面的有参构造函数
        demoDAO = new DemoDAO();
    }
    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来     *     * @param demoDAO     */
    public DemoDataListener(DemoDAO demoDAO) {
        this.demoDAO = demoDAO;
    }
    /**
     * 这个每一条数据解析都会来调用     *     * @param data     *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(DemoData data, AnalysisContext context) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
        list.add(data);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }
    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
        saveData();
        LOGGER.info("所有数据解析完成!");
    }
    /**
     * 加上存储数据库
     */
    private void saveData() {
        LOGGER.info("{}条数据,开始存储数据库!", list.size());
        demoDAO.save(list);
        LOGGER.info("存储数据库成功!");
    }
}

Persistence layer  (mybatis or jpa can do it)

 

/**
 * 假设这个是你的DAO存储。当然还要这个类让spring管理,当然你不用需要存储,也不需要这个类。
 **/
public class DemoDAO {
    public void save(List<DemoData> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        System.out.println(list);
    }
}

Read data

 

String fileName = "src/test/resources/demo/demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();

Output result  (non-essential data has been filtered)

 

Hutool2020-09-16 08:14:33.727 DEBUG [main] com.alibaba.excel.context.AnalysisContextImpl:91 - Began to read:ReadSheetHolder{sheetNo=0, sheetName='Sheet1'} com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder@6f3b5d16
2020-09-16 08:14:33.870 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1577811661000,"doubleData":1.0,"string":"字符串0"}
2020-09-16 08:14:33.870 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1577898061000,"doubleData":2.0,"string":"字符串1"}
2020-09-16 08:14:33.871 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1577984461000,"doubleData":3.0,"string":"字符串2"}
2020-09-16 08:14:33.871 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578070861000,"doubleData":4.0,"string":"字符串3"}
2020-09-16 08:14:33.872 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578157261000,"doubleData":5.0,"string":"字符串4"}
2020-09-16 08:14:33.872 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:80 - 5条数据,开始存储数据库!
[DemoData(string=字符串0, date=Wed Jan 01 01:01:01 CST 2020, doubleData=1.0), DemoData(string=字符串1, date=Thu Jan 02 01:01:01 CST 2020, doubleData=2.0), DemoData(string=字符串2, date=Fri Jan 03 01:01:01 CST 2020, doubleData=3.0), DemoData(string=字符串3, date=Sat Jan 04 01:01:01 CST 2020, doubleData=4.0), DemoData(string=字符串4, date=Sun Jan 05 01:01:01 CST 2020, doubleData=5.0)]
2020-09-16 08:14:33.874 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:82 - 存储数据库成功!
2020-09-16 08:14:33.875 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578243661000,"doubleData":6.0,"string":"字符串5"}
2020-09-16 08:14:33.875 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578330061000,"doubleData":7.0,"string":"字符串6"}
2020-09-16 08:14:33.876 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578416461000,"doubleData":8.0,"string":"字符串7"}
2020-09-16 08:14:33.876 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578502861000,"doubleData":9.0,"string":"字符串8"}
2020-09-16 08:14:33.876 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:54 - 解析到一条数据:{"date":1578589261000,"doubleData":10.0,"string":"字符串9"}
2020-09-16 08:14:33.877 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:80 - 5条数据,开始存储数据库!
[DemoData(string=字符串5, date=Mon Jan 06 01:01:01 CST 2020, doubleData=6.0), DemoData(string=字符串6, date=Tue Jan 07 01:01:01 CST 2020, doubleData=7.0), DemoData(string=字符串7, date=Wed Jan 08 01:01:01 CST 2020, doubleData=8.0), DemoData(string=字符串8, date=Thu Jan 09 01:01:01 CST 2020, doubleData=9.0), DemoData(string=字符串9, date=Fri Jan 10 01:01:01 CST 2020, doubleData=10.0)]
2020-09-16 08:14:33.877 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:82 - 存储数据库成功!
2020-09-16 08:14:33.877 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:80 - 0条数据,开始存储数据库!
[]2020-09-16 08:14:33.877 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:82 - 存储数据库成功!
2020-09-16 08:14:33.877 INFO [main] com.alibaba.easyexcel.test.demo.read.DemoDataListener:73 - 所有数据解析完成!
Process finished with exit code 0

Hutool

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Introduction

Hutool is a very useful Java tool class library that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, regularization, threads, and XML.

Very practical open source tool library, recommended for friends to use in their own projects.

The official introduction of Hutool is as follows:

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

Introduce dependencies

Maven

 

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.2</version>
</dependency>

Gradle

 

// https://mvnrepository.com/artifact/cn.hutool/hutool-all
compile group: 'cn.hutool', name: 'hutool-all', version: '5.4.2'

Demo

Simply demonstrate a few more practical functions.

mail

Sending mail in Java mainly relies on the javax.mail package, but because it is cumbersome to use, Hutool encapsulates it. Because it relies on third-party packages, this tool is classified into the extra module.

You can customize the mailbox configuration in the actual project, and then save the configuration in the database and save a copy in the cache.

If you don't want to customize the configuration, just write the mailbox configuration directly in the configuration file.

After you have the mail configuration, you can send mail after defining the mail server, which is very convenient:

 

MailAccount account = new MailAccount();
account.setHost("smtp.yeah.net");
account.setPort("25");
account.setAuth(true);
account.setFrom("[email protected]");
account.setUser("hutool");
account.setPass("q1w2e3");
MailUtil.send(account, CollUtil.newArrayList("[email protected]"), "测试", "邮件来自Hutool测试", false);

Unique ID

In a distributed environment, the unique ID generation application is very extensive, and the generation methods are also diverse. Hutool has made simple packages for some common generation strategies.

Still reinventing the wheel? Increase productivity! 3 commonly used open source tool libraries to share

The tool class of unique ID generator provided by Hutool covers:

  • UUID
  • ObjectId(MongoDB)
  • Snowflake(Twitter)

Take UUID for example!

Hutool rewrites the logic of java.util.UUID, the corresponding class is cn.hutool.core.lang.UUID, so that the UUID string without-does not need to be replaced, and the performance is doubled .

 

//生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
String uuid = IdUtil.randomUUID();
//生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
String simpleUUID = IdUtil.simpleUUID();

Http request tool

For the most commonly used GET and POST requests, HttpUtil encapsulates two methods,

  • HttpUtil.get
  • HttpUtil.post

 

// 最简单的HTTP请求,可以自动通过header等信息判断编码,不区分HTTP和HTTPS
String result1 = HttpUtil.get("https://www.baidu.com");
// 当无法识别页面编码的时候,可以自定义请求页面的编码
String result2 = HttpUtil.get("https://www.baidu.com", CharsetUtil.CHARSET_UTF_8);
//可以单独传入http参数,这样参数会自动做URL编码,拼接在URL中
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "北京");
String result3 = HttpUtil.get("https://www.baidu.com", paramMap);

Cache

Hutool provides the implementation of several common caching strategies:

  1. FIFO (first in first out)  : First in first out strategy.
  2. LFU (least frequently used)  : Least frequently used strategy.
  3. LFU (least frequently used)  : Least frequently used strategy.
  4. Timed cache: Define an expiration time for the cached object. When the object exceeds the expiration time, it will be cleaned up.
  5. ......

FIFO (first in first out) strategy buffer usage:

 

Cache<String,String> fifoCache = CacheUtil.newFIFOCache(3);
//加入元素,每个元素可以设置其过期时长,DateUnit.SECOND.getMillis()代表每秒对应的毫秒数,在此为3秒
fifoCache.put("key1", "value1", DateUnit.SECOND.getMillis() * 3);
fifoCache.put("key2", "value2", DateUnit.SECOND.getMillis() * 3);
fifoCache.put("key3", "value3", DateUnit.SECOND.getMillis() * 3);
//由于缓存容量只有3,当加入第四个元素的时候,根据FIFO规则,最先放入的对象将被移除
fifoCache.put("key4", "value4", DateUnit.SECOND.getMillis() * 3);
//value1为null
String value1 = fifoCache.get("key1");

Console printing package-Console

Under normal circumstances, we should be familiar with printing information to the console!

 

System.out.println("Hello World");

However, this method does not meet the needs of many scenarios:

  1. Parameters are not supported, and object printing needs to be spliced ​​string
  2. You cannot print the array directly, you need to manually call Arrays.toString

To this end, Hutool encapsulates the Console object.

The use of the Console object is more similar to Javascript's console.log() method, which is also a syntactic sugar borrowed from JS.

 

String[] a = {"java", "c++", "c"};
Console.log(a);//控制台输出:[java, c++, c]
Console.log("This is Console log for {}.", "test");//控制台输出:This is Console log for test.

Guess you like

Origin blog.csdn.net/AMSRY/article/details/108625161