跳至主要内容

博文

目前显示的是标签为“java”的博文

Class Not Found vs No Class Def Found

Class Not Found vs No Class Def Found When we are working on JavaEE, we will always has multiple issues related to classes and ClassLoader . We may struggle to find some strange memory leaks caused by ClassLoader ; we may fail to run some applications because it fail to find some class; we may encounter cases when multiple versions of library conflict. In order to solve all similar problems one for all, today we focus the understanding of two exception (to be precise, one exception and one error): java.lang.ClassNotFoundException java.lang.NoClassDefFoundError ClassLoader Before we get to solve the problem, we have to skim some basic knowledge of class loading. In the world of Java, almost all things are abstracted into a class. Class is also a class, which organized by classpath and package structure. In order to load classes when suitable, JVM use the abstraction of ClassLoader to locate or generate data that constitutes a definition for the class. Th...

How to Implement String Case Insensitive Compare?

How to Implement String Case Insensitive Compare? How would you implement a String Comparator used in String#compareToIgnoreCase ? I may convert all character to upper case then compare like the following does: int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; int min = Math . min ( n1 , n2 ) ; for ( int i = 0 ; i < min ; i ++ ) { char c1 = Character . toUpperCase ( s1 . charAt ( i ) ) ; char c2 = Character . toUpperCase ( s2 . charAt ( i ) ) ; if ( c1 != c2 ) { return c1 - c2 ; } } return n1 - n2 ; It seems work and we may also use toLowerCase to replace toUpperCase . But the implementation in JDK source code doesn’t agree: int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; int min = Math . min ( n1 , n2 ) ; for ( int i = 0 ; i < min ; i ++ ) { char c1 = s1 . charAt ( i ) ; char c2 = s2 . charAt ( i ) ; if ( c1 != c2 ) { c1 = Character . to...

Get Java Generic Type?

Get Java Generic Type? Tony is now implementing some persistence/serialization library using Java. Because the implementation of generic of Java is the erasure, the generic has many limitations . The first interface Tony needed to implement is creating a new instance by using type parameter. public class SomeLib < T > { public T fromString ( String string ) { } } Extra Class Because the type parameter is erased at runtime, Tony can’t use new T() to create a new instance or use T.class to get real class etc. The common way to do the similar things is to put an extra Class parameter in interface: public class SomeLib { public < T > T fromString ( String string , Class < T > clazz ) { T t = ( T ) clazz . newInstance ( ) ; } } Superclass Workaround This way works, but also very inconvenient. If Tony want to deserialize a type with nested generic types, the interfaces may become: public < T , S >...

Elasticsearch MySQL Sync Challenge (4): Quality Attributes

Elasticsearch MySQL Sync Challenge (4): Quality Attributes Tony was called into the leader’s office. After the basic functionality is fulfilled, Tony has started to review the quality attributes of the project. Extensibility “The syncer you have completed works great and it has good extensibility for its relative loose restriction between different modules (input, filter, output). Now, we need one more output choice: to output to MySQL server. How long do you think to finish this feature?” “Em, at least three days, for both coding and testing.” “Fine, go for it.” The process to extend one more output choice is relative simple: Add MySQL output config config package, which is used to mapping config item in yaml to class; Add MySQL output channel, which is the abstraction remote destination; Add SqlMapper , which convert the SyncData to sql statement when SyncData reach the MySQL output channel; The first item is implemented very fast but the seco...

Java JIT Inline

Java JIT Inline This is a follow-up blog that I write after answering a related question on SO . Through this question, we can understand why we need to know the internal of machine/os/language etc. Introduction of JIT The JIT (just-in-time) compiler is a important component of JVM which is used to optimize the performance of Java. As it name indicates, JIT can perform just-in-time compilation, which is the compilation process from byte code into native code of specific platform to make program run faster. And JIT compiler can not only makes standard compilation, but also it can performs optimizations like OSR (on stack replacement) , class hierarchy analysis etc. Today, we will see how JIT do inline optimization and how it affects the performance of program. Inline Function First, we get to understand what is inline function . To inline a function is to substitute the function call with the function body. Compiler can inline some small hot functions into cal...

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi...

Elasticsearch Endless Queue?

Elasticsearch Endless Queue? This day, Tony was notified that the Kibana which is used to query logs can’t working now. This a crucial part for log aggregation in new log system used in Microservice environment , so Tony set out to deal the it. Rejected Task Found nothing related in the Kibana’s log, Tony decided to checkout the Elasticsearch’s log. He found that there exists many long EsRejectedExecutionException . [DEBUG][o.e.a.s.TransportSearchAction] [xLI0MAD] All shards failed for phase: [query] org.elasticsearch.common.util.concurrent.EsRejectedExecutionException: rejected execution of org.elasticsearch.transport.TransportService$7@30fadff6 on EsThreadPoolExecutor[search, queue capacity = 1000, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@6c9b1b8d [Running, pool size = 25, active threads = 25, queued tasks = 1921, completed tasks = 169477]] Just google the exception show some SO threads which introduced the thread pool and request q...

Spring Boot Introduction

Spring Boot Introduction What is Spring Boot? What the usage of it and what’s the differences between Spring Boot and Spring? In this blog, we are going to solve those questions by introducing some basic idea. Project Dependency Structure First, we can see the projects hierarchy from the pom dependency: spring-boot-dependencies # list all dependencies of spring boot: include spring boot jar and some third party jar spring-boot-parent spring-boot-starters # list all starter project, which is a set of convenient dependency descriptor spring-boot-starter-xxx ( data-elastcisearch, web, etc ) # specific tech dependencies spring-boot-starter-parent # can include `spring-boot-starter-xxx` So, what is starter and boot ? Boot Boot is the project to start web application more easily with the help of: Embedded tomcat or jetty in spring-boot-starter-web Spring ApplicationContext etc Starter Starters are a set of convenient dependency descrip...

Design a Logging System?

Design a Logging System? Long long ago, when there is not any mature logging system, Tom is facing the problem of logging. The native logging system form Java SDK has some issues of class loading, in which it may cause memory leaks 1 . Requirement He discussed this problem with leader and they decided to write their own logging system with the following requirements: The logging system can be used by different package, class at the same time and we can easily differentiate them; The logging system can output to different location, for example, console, file or even network; The logging system can output formatted log content: plain text, JSON, XML etc; The logging system can have different levels to control the behavior in different environments: in development env, we can log more, but in production env, we maybe log only exception; Design Tom now begin to analyze the requirements of logging system using OO related patterns and try to make some basic...