Starting from Gradle 7.4, a feature has been added that allows you to aggregate multiple Jacoco test reports into a single, unified report. In the past, it was very difficult to view the test results across multiple modules in one file, but now it has become much more convenient to merge these reports.
In the case of a Gradle multi-project setup, there is an issue where packages that were properly excluded in a single project are not excluded in the aggregate report.
By adding the following configuration, you can generate a report that excludes specific packages.
The jvm-test-suite plugin, which is introduced alongside jacoco-aggregation-report in Gradle, also seems very useful. Since these plugins are complementary, it would be beneficial to use them together.
A containerization technology that allows you to create and use Linux containers, and also the name of the largest company supporting this technology as well as the name of the open-source project.
The image everyone has seen at least once when searching for Docker
Introduced in 2013, Docker has transformed the infrastructure world into a container-centric one. Many applications are now deployed using containers, with Dockerfiles created to build images and deploy containers, becoming a common development process. In the 2019 DockerCon presentation, it was reported that there were a staggering 105.2 billion container image pulls.
Using Docker allows you to handle containers like very lightweight modular virtual machines. Additionally, containers can be built, deployed, copied, and moved from one environment to another flexibly, supporting the optimization of applications for the cloud.
As long as the container runtime is installed, Docker containers guarantee the same behavior anywhere. For example, team member A using Windows OS and team member B using MacOS are working on different OSs, but by sharing the image through a Dockerfile, they can see the same results regardless of the OS. The same goes for deployment. If the container has been verified to work correctly, it will operate normally without additional configuration wherever it is run.
Docker's containerization approach focuses on the ability to decompose, update, or recover parts of an application without needing to break down the entire application. Users can share processes among multiple applications in a microservices-based approach, similar to how service-oriented architecture (SOA) operates.
Each Docker image file consists of a series of layers, which are combined into a single image.
Docker reuses these layers when building new containers, making the build process much faster. Intermediate changes are shared between images, improving speed, scalability, and efficiency.
Docker-based containers can reduce deployment time to mere seconds. Since there is no need to boot the OS to add or move containers, deployment time is significantly reduced. Moreover, the fast deployment speed allows for cost-effective and easy creation and deletion of data generated by containers, without users needing to worry about whether it was done correctly.
In short, Docker technology emphasizes efficiency and offers a more granular and controllable microservices-based approach.
When deploying with Docker, images are used with tags. For example, if you deploy using version 1.2 of an image, and version 1.1 of the image is still in the repository, you can simply run the command without needing to prepare the jar file again.
docker run --name app image:1.2 docker stop app ## Run version 1.1 docker run --name app image:1.1
Package the jar file to be deployed on the local machine.
Transfer the jar file to the production server using file transfer protocols like scp.
Write a service file using systemctl for status management.
Run the application with systemctl start app.
If multiple apps are running on a single server, the complexity increases significantly in finding stopped apps. The process is similarly cumbersome when running multiple apps on multiple servers, requiring commands to be executed on each server, making it a tiring process.
Use a Dockerfile to create an image of the application. β Build βοΈ
Push the image to a repository like Dockerhub or Gitlab registry. β Shippingπ’
Run the application on the production server with docker run image.
You don't need to waste time on complex path settings and file transfer processes. Docker works in any environment, ensuring it runs anywhere and uses resources efficiently.
Docker is designed to manage single containers effectively. However, as you start using hundreds of containers and containerized apps, management and orchestration can become very challenging. To provide services like networking, security, and telemetry across all containers, you need to step back and group them. This is where Kubernetes1 comes into play.
Developers can find Docker extremely useful in almost any situation. In fact, Docker often proves superior to traditional methods in development, deployment, and operations, so Docker containers should always be a top consideration.
When you need a development database like PostgreSQL on your local machine.
When you want to test or quickly adopt new technologies.
When you have software that is difficult to install or uninstall directly on your local machine (e.g., reinstalling Java on Windows can be a nightmare).
When you want to run the latest deployment version from another team, like the front-end team, on your local machine.
When you need to switch your production server from NCP to AWS.
Using Docker containers allows for convenient operations while solving issues that arise with traditional deployment methods. Next, we'll look into the Dockerfile, which creates an image of your application.
There are various ways to run Kubernetes, but the official site uses minikube for demonstration. This article focuses on utilizing Kubernetes using Docker Desktop. If you want to learn how to use minikube, refer to the official site.
One major drawback is that sometimes the command to view the dashboard causes hang-ups. This issue is the primary reason why I am not using minikube while writing this article.
When a deployment is created, pods are also generated simultaneously.
kubectl get pods -o wide
Having confirmed that everything is running smoothly, let's send a request to our web server. Instead of using curl, we will use httpie1. If you are more comfortable with curl, feel free to use it.
http localhost:8080/ping
Even though everything seems to be working fine, why can't we receive a response? π€
This is because our service is not exposed to the outside world yet. By default, Kubernetes pods can only communicate internally. Let's make our service accessible externally.
// Java Collection that implements Iterable. publicinterfaceCollection<E>extendsIterable<E>
First-class collections are a very useful way to handle objects. However, despite the name "first-class collection," it only holds Collection as a field and is not actually a Collection, so you cannot use the various methods provided by Collection. In this article, we introduce a way to make first-class collections more like a real Collection using Iterable.
It's not bad, but AssertJ provides various methods to test collections.
has..
contains...
isEmpty()
You cannot use these convenient assert methods with first-class collections because they do not have access to them due to not being a Collection.
More precisely, you cannot use them because you cannot iterate over the elements without iterator(). To use iterator(), you just need to implement Iterable.
By implementing Iterable, you can use much richer functionality. The implementation is not difficult, and it is close to extending functionality, so if you have a first-class collection, actively utilize Iterable.
If you are a developer who frequently uses Linux, you probably use the curl command often. It is an essential command for sending external API requests from a server, but it has the disadvantage of poor readability in the output. HTTPie is an interesting tool that can alleviate this drawback, so let's introduce it.
First, here is how you would send a GET request using curl.
curl https://httpie.io/hello
Now, let's compare it with HTTPie.
https httpie.io/hello
The readability is much better in every aspect of the command. The response and header values are included by default, so you can get various information at a glance without using separate commands.
Note that https and http are distinguished in the command.
http localhost:8080
You can send a POST request as described on the official website.
http -a USERNAME POST https://api.github.com/repos/httpie/httpie/issues/83/comments body='HTTPie is awesome! :heart:'
Various other features are explained on GitHub, so if you make good use of them, you can greatly improve productivity.
When you search for getter/setter on Google, you'll find a plethora of articles. Most of them explain the reasons for using getter/setter, often focusing on keywords like encapsulation and information hiding.
The common explanation is that by declaring field variables as private to prevent external access and only exposing them through getter/setter, encapsulation is achieved.
However, does using getter/setter truly encapsulate data?
In reality, getter/setter cannot achieve encapsulation at all. To achieve encapsulation, one should avoid using getters and setters. To understand this, it is necessary to have a clear understanding of encapsulation.
Encapsulation in object-oriented programming has two aspects: bundling an object's attributes (data fields) and behaviors (methods) together and hiding some of the object's implementation details internally. - Wikipedia
Encapsulation means that the external entities should not have complete knowledge of an object's internal attributes.
As we have learned, encapsulation dictates that external entities should not know the internal attributes of an object. However, getter/setter blatantly exposes the fact that a specific field exists to the outside world. Let's look at an example.
publicclassStudent{ privateString name; privateint age; publicStringgetName(){ return name; } publicvoidsetName(String name){ this.name = name; } publicintgetAge(){ return age; } publicvoidsetAge(int age){ this.age = age; } publicStringintroduce(){ returnString.format("My name is %s and I am %d years old.", name, age); } }
classStudentTest{ @Test voidstudent(){ Student student =newStudent(); student.setName("John"); student.setAge(20); String introduce = student.introduce(); assertThat(student.getName()).isEqualTo("John"); assertThat(student.getAge()).isEqualTo(20); assertThat(introduce).isEqualTo("My name is John and I am 20 years old."); } }
From outside the Student class, it is evident that it has attributes named name and age. Can we consider this state as encapsulated?
If the age attribute were to be removed from Student, changes would need to be made everywhere getter/setter is used. This creates strong coupling.
True encapsulation means that modifications to an object's internal structure should not affect the external entities, except for the public interface.
Let's try to hide the internal implementation.
publicclassStudent{ privateString name; privateint age; publicStudent(String name,int age){ this.name = name; this.age = age; } publicStringintroduce(){ returnString.format("My name is %s and I am %d years old.", name, age); } }
classStudentTest{ @Test voidstudent(){ Student student =newStudent("John",20); String introduce = student.introduce(); assertThat(introduce).isEqualTo("My name is John and I am 20 years old."); } }
Now, the object does not expose its internal implementation through the public interface. It is not possible to know what data it holds, prevent it from being modified, and only communicate through messages.
Encapsulation is a crucial topic in object-oriented design, emphasizing designs that are not dependent on external factors. Opinions vary on the level of encapsulation, with some advocating against using both getter and setter and others suggesting that using getter is acceptable.
Personally, I believe in avoiding getter usage as much as possible, but there are situations, especially in testing, where having getters or setters can make writing test code easier. Deciding on the level of encapsulation depends on the current situation and the purpose of the code being developed.
Good design always emerges through the process of trade-offs.
I had registered the sitemap.xml for my blog to ensure it gets indexed by Google, but all I was getting was an error message saying 'sitemap not found'. Finally, I managed to resolve it, and I am sharing the method I used.
While this method may not solve every case, it seems worth a try.
Simply run the following command:
curl https://www.google.com/ping\?sitemap\={path to your submitted sitemap}
And then, when you check the search console again...!
Now, when you run the batch job, consumer groups are automatically created based on the information in application.yml, and the job starts subscribing to the topic.
Let's use the kafka console producer to add data from 1 to 10 to the test topic.
Qodana is a code quality improvement tool provided by JetBrains. It is very easy to use, so I would like to introduce it briefly.
First, you need an environment with Docker installed.
docker run --rm-it-p8080:8080 \ -v<source-directory>/:/data/project/ \ -v<output-directory>/:/data/results/ \ jetbrains/qodana-jvm --show-report
I am analyzing a Java application, so I used the jvm image. If you are using a different language, you can find the appropriate image on Qodana's website.
Replace <source-directory> with the path to the project you want to analyze.
Enter the path where the analysis results will be stored in <output-directory>. I will explain this further below.
To store the analysis results, I created a folder named qodana in the root directory.
mkdir ~/qodana # Then replace <output-directory> with ~/qobana.
Now, execute the docker run ~ command written above and wait for a while to see the results as shown below.
If you have Docker installed, you can easily obtain the code analysis results of your current project.
Such analysis tools serve as a form of code review, reducing the reviewer's fatigue and allowing them to focus on more detailed reviews. Actively utilizing code quality management tools like this can lead to a very convenient development experience.
Recently, I designed a batch process that uses Upsert in PostgreSQL for a specific logic. During implementation, due
to a change in business requirements, I had to add a specific column to a composite unique condition.
The issue arose from the fact that the unique constraint of the composite unique column does not prevent duplicates
with null values in a specific column.
Let's take a look at an example of the problematic situation.
createtable student ( id integernotnull constraint student_pk primarykey, name varchar, major varchar, constraint student_unique unique(name, major) );
id
name
major
1
song
korean
2
kim
english
3
park
math
4
kim
NULL
5
kim
NULL
To avoid allowing null duplicates, the idea of inserting dummy data naturally came to mind, but I felt reluctant to
store meaningless data in the database. Especially if the column where null occurs stores complex data like UUID, it
would be very difficult to identify meaningless values buried among other values.
Although it may be a bit cumbersome, using a unique partial index allows us to disallow null values without
inserting dummy data. I decided to pursue the most ideal solution, even if it is challenging.
CREATEUNIQUEINDEX stu_2col_uni_idx ON student (name, major) WHERE major ISNOTNULL; CREATEUNIQUEINDEX stu_1col_uni_idx ON student (name) WHERE major ISNULL;
PostgreSQL provides the functionality of partial indexes.
Partial Index
: A feature that creates an index only when certain conditions are met. It allows for efficient index creation and
maintenance by narrowing the scope of the index.
When a value with only name is inserted, stu_1col_uni_idx allows only one row with the same name where major
is null. By creating two complementary indexes, we can skillfully prevent duplicates with null values in a specific
column.
An error occurs when trying to store a value without major
However, when there are two unique constraints like this, since only one constraint check is allowed during Upsert
execution, the batch did not run as intended.
After much deliberation, I decided to check if a specific value is missing before executing the SQL and then execute the
SQL that meets the conditions.
I implemented this by overriding the write method of the JdbcBatchItemWriter that was previously used. By checking
the presence of major in the code and selecting and executing the appropriate SQL, we can ensure that the Upsert
statement works correctly instead of encountering a duplicateKeyException.
Here is an example of usage:
@Bean SelectConstraintWriterstudentItemWriter(){ String sql1 = "INSERT INTO student(id, name, major) " +"VALUES (nextval('hibernate_sequence'), :name, :major) " +"ON CONFLICT (name, major) WHERE major IS NOT NULL " +"DO UPDATE " +"SET name = :name, " +" major = :major"; String sql2 = "INSERT INTO student(id, name, major) " +"VALUES (nextval('hibernate_sequence'), :name, :major) " +"ON CONFLICT (name) WHERE major IS NULL " +"DO UPDATE " +"SET name = :name, " +" major = :major"; SelectConstraintWriter writer =newSelectConstraintWriter(); writer.setSql(sql1); writer.setAnotherSql(sql2); writer.setDataSource(dataSource); writer.setItemSqlParameterSourceProvider(newBeanPropertyItemSqlParameterSourceProvider<>()); writer.afterPropertiesSet(); return writer; }
It's regrettable that if PostgreSQL allowed multiple constraint checks during Upsert execution, we wouldn't have
needed to go to such lengths. I hope for updates in future versions.