Java NIO Overview and Example

Java NIO Purpose Netty is a library that abstracts Java NIO, so Java NIO is often discussed. This post provides a brief explanation. Features NonBlocking I/O I/O operations can be performed asynchronously without waiting Multiple connections can be implemented in a non-blocking way (Selectors) Channels Bidirectional communication Unlike InputStream and OutputStream, a single channel can perform both read and write Direct Buffer support Performance improvement by reducing buffer copy overhead through memory-mapped buffers Read/write for socket communication is only supported with native memory - stack overflow (Java Champion’s answer) Native memory is part of the JVM’s native area If you use a buffer created in JVM heap memory for socket communication, there is overhead from copying to native memory To reduce buffer copy overhead, direct buffers are introduced, which are created and written directly in native memory Due to direct buffer optimization, there is a difference in the number of direct buffers between Webflux and MVC models - https://oss.navercorp.com/nspa/nspa-server/issues/3219#issuecomment-14853076 MVC: 200~300 Webflux: 75 Main Components Selector Handles events for multiple channels in a non-blocking way ServerSocketChannel Server socket channel. Supports bidirectional communication. By implementing SelectableChannel, Selector can register to receive events for the channel. SelectionKey Token issued when registering a channel with a Selector. Represents the connection between selector and channel. ...

June 2, 2025

Stream API - peek

peek? Why does the documentation recommend using Stream.peek mainly for debugging? Let’s find out. Reference Stream API Since peek is part of the Stream API, let’s start by looking at the Stream API documentation. According to the Stream API documentation, functions passed to methods like filter and map must satisfy two constraints for correct operation: They must be non-interfering In most cases, they should be stateless Let’s focus on non-interfering. non-interfering Dictionary meaning of interfering Interfering, meddlesome ...

December 13, 2022

Collections Summary - Interfaces

Related Posts https://jurogrammer.tistory.com/172 Interfaces https://docs.oracle.com/javase/tutorial/collections/interfaces/index.html Overview Core collection interfaces encapsulate different types of collections. Therefore, you can manipulate different collections without worrying about the details. The collection interface can be considered the foundation of the Java Collection Framework. The Interface chapter will provide general guidelines for efficient use of collection interfaces. Important Notes Map is not a Collection Literally, it’s not. All Core collections are Generic When declaring a collection, you must specify the type that will go into the collection. Generics can verify at compile time whether the object being put into the collection is of the correct type. Java Platform does not provide interface variations To facilitate the management of numerous core collection interfaces, the Java Platform does not provide interface variations (e.g., interfaces with characteristics like immutable, fixed-size, append-only). Instead, modification operations are optional. That is, since not all operations are implemented, calling a specific operation may throw an UnsupportedOperationException. Therefore, this must be documented. In summary, if you need to implement a specific collection interface, implement only what you need. Don’t implement everything. ...

June 30, 2022

Logging AOP Implementation - 1

Introduction Logging is not code for handling business logic, but rather for monitoring. That’s why logging appearing in the middle of business logic is very uncomfortable to see. Spring provides AOP, but I wanted to try implementing it myself without looking at Spring’s implementation. So, this time, I want to think about how to implement a great logging AOP. Situation Let’s write a sayHello method in the Person class and call it from main. Here, let’s assume the person object is provided by a framework. ...

September 19, 2021

Java - Exception Handling (Code)

Parent Topic Exception handling journey Situation Here is some smelly code I used incorrectly. Situation A function receives an order number and returns the corresponding order sheet. If the requested order number does not exist, an error should be raised and the client should be notified that the order does not exist. Code Controller 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 package com.company.exceptiontest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Controller { private final Service service; private final Logger logger = LoggerFactory.getLogger(this.getClass()); public Controller(Service service) { this.service = service; } public String orderInfo(Long id) { try { OrderSheet orderSheet = service.searchOrderSheet(id); return String.valueOf(orderSheet); } catch (CustomException e) { logger.info("controller - orderInfo method: Error occurred while requesting order sheet info!"); e.printStackTrace(); return e.getMessage(); } } } In the controller, the order sheet information is returned as a string. If an error occurs, it is caught, logged, and the error message is returned to the user. ...

August 8, 2021

Exception handling - Java Exception Handling (Theory)

Parent Topic Exception handling journey References clean code effective java oracle java docs This time, let’s talk about what an exception is and how to handle exceptions. What is an exception? Let’s see how Oracle defines it: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. It means an event that disrupts the normal flow during program execution. ...

August 8, 2021

[Spring] Circular Reference Error Caused by Field Injection

References edwith DobiSocks reviewer https://www.mimul.com/blog/di-constructor-injection/ (Why DI is needed) https://madplay.github.io/post/why-constructor-injection-is-better-than-field-injection (Why constructor injection is recommended over field injection) https://d2.naver.com/helloworld/1230 (JVM Internal) Overview In an edwith course, I submitted a project using field injection for DI. However, the reviewer recommended constructor injection and kindly provided related materials. Although I understood most of the points, I didn’t quite get how constructor injection prevents circular reference errors, so I decided to summarize it here. ...

June 1, 2020