Posts Tagged - java

Java Index

This are my Java-related notes. Here I have all the knowledge I refer to when I have doubts about how to use or how to implement a framework / feature I’ve already implemented once.

Version changes

Interesting changes, new functionality and APIs that come to Java with each new version. They don’t include the full changes but the ones I deemed most useful or most interesting.

From Java 8 to Java 11
Java12
Java13

Experience

Small, functional snipets on how to implement a determined feature.

Java experience sheet
How to create a database intermediate table
Java date time API
New script files in Java

Frameworks

How to use and implement determined frameworks in a Java project (using Maven).

Spring in Action (Book)
Spring Cache
Spring Beans
Thymeleaf
Spring Cors

Maven (builder)
Testing (JUnit, TestNG, Mockito)
Vert.x (microservices)
Lombok (builder)
MapStruct (mapper)

Read More

JMeter performance testing

We’re going to use a test app. Supposedly, this app has passed a first development iteration, and has passed unit and functional testing. With JMeter we measure its performance.
Performance testing should start early and continue through the whole dev process.

JMeter is not a browser. It doesn’t execute JavaScript!

Performance Testing

Performance is related to how fast an app executes an operation. Performance testing is about how an app or resource performs under a given load, to see its impact. It should be done after the app has passed all functional tests and we’re sure it works correctly.

Performance testing is measured in terms of:

  • response time: request time + processing time + network latency.
  • throughput: number of transactions (request/response) / unit of time (ms / seconds).
  • reliability: how well the app detects and handles errors. number of errors / number of requests.
  • scalability: tells how well the system expands its capacity in terms of response time, throughput and reliability when additional resources are added.

    • vertical scalability: adding more memory or additional CPUs.
    • horizontal scalability: adding servers to a cluster.

Performance requirements

They’re are usually set up in contracts, such as:

  • average / maximum response time.
  • pages per second the system should be able to support.
  • users per hour the system should be able to support.

Process to follow

  • Design and build tests: We have to know the app and its usage patterns.
  • Prepare test environment: configure a test env similar to prod.
  • Run the test: validate the script and test data. Monitor server logs!
  • Analyze the results
  • Optimize
  • Retest

Types of Performance Tests

  • Increase the number of users.
  • Increase the number of requests.

Almost always you should execute a smoke test first. This is a test with light load to verify the test works correctly.

A load test is a test that’s performed at a specific load level. Usually you’ll perform them at many load levels to monitor the app’s behavior.

A stress test tests the app with loads past its normal working range, to see up to which point it stays stable and responsive.

At a spike test the app is subjected to brief periods of sudden increments in the load beyond its maximum capacity, to see if the app is robust enough to work during and after the spike.

Endurance tests, where the app is subjected to load within its limits for a long duration (hours or even days) to search for memory leaks.

Read More

JMeter

JMeter is a testing tool to simulate thousands of users. It uses Java Threads for this.

Configure a Test Plan

In JMeter the components of a test are organized in a tree, where each component has a scope that determines to what other components it has access to.

Test Plan

Test Plan is the root element of a test, where all overall settings are specified and all the other elements are contained.

It allows us to:

  • Define variables to the entire test.
  • There we can set to run Thread Groups consecutively, instead of parallel.
  • We may run tearDown Thread Groups. They run at the end of the test, after a graceful shutdown of the main Threads. They won’t be run if the test is forcibly stopped.
  • If we select Functional Test Mode, JMeter will save information from the server responses to result files. Beware, as this files grow quickly and they impact performance. Only for small tests.
  • We may add jar files or directories to classpath, so they will load just for the test script.

Thread Groups

They’re the entry point of a test and controls the number of threads (users) JMeter will use to execute a test. We also have setUp and tearDown Thread Groups.

We may:

  • Set the number of users and the time it will take to create them. This creation can be delayed until they’re needed.

Configuration Elements

They’re used to set up default configurations and variables for later use by other components. They can be placed under any other component.

They are classified into:

  • elements that allows us to set variables: CSV Data Set Config, Counter or Random Variable.
  • elements that allows us to define a Configuration: JDBC Connection Configuration, KeyStore Configuration, Login Config Element.
  • managers that allow us to manage configuration params: HTTP Header Manager, HTTP Cookie Manager, HTTP Cache Manager.
  • default configuration elements: HTTP Request Defaults, FTP Request Defaults, Java Request Defaults.

The most common used are:

  • CSV Data Set Config used to read the content of a CSV file and turn it into variables.
  • HTTP Header Manager allows us to add or override HTTP Request Headers.
  • HTTP Cookie Manager For each user, it stores and sends a cookie like a browser; Allows to manually add a cookie that will be shared by all users.
  • HTTP Cache Manager Adds caching functionality to HTTP Request. Each user has its own cache.
  • User defined variables allows you to define a set of variables. They’re processed once at the start of the test and are shared between old thread groups.

He created an HTTP Request Defaults and added the IP and port number, so he doesn’t need to repeate this at every component.

Read More

MapStruct - Java Mapper

MapStruct is a Java Bean Mapper.

It contains functions that automatically maps between two Java functions. We only need to create the interface, and the library will automatically create a concrete implementation.

Implementation

config

pom.xml modifications for maven

<dependency>
	<groupId>org.mapstruct</groupId>
	<artifactId>mapstruct</artifactId>
	<version>1.3.1.Final</version>
</dependency>

Read More

Spring in Action (1/5) - Foundational Spring

(This are my notes taken from the book Spring in Action 5th Edition)

Spring parts

  • Spring core Provides the core container, dependency injection framework, Spring MVC (Rest APIs), Spring’s web framework and support for template-based JDBC and reactive programming with Spring WebFlux.
  • Spring boot Starter dependencies and autoconfiguration.
  • Spring data Provides the ability to define your application’s data repository as Java interfaces. Works with relational (JPA), document (Mongo) and graph (Neo4j) databases.
  • Spring security Authentication, authorization and API security.
  • Spring integration & Batch helps integrate with other applications
  • Spring cloud helps with microservices

Read More

Java8 Date Time API

The main improvements over previous classes are Thread safety and ease of use.

LocalDate

Represents a date in ISO format (yyyy-MM-dd) without time.

// Declaration
final LocalDate ld1 = LocalDate.now();
final LocalDate ld2 = LocalDate.of(2015, 02, 20);
final LocalDate ld3 = LocalDate.parse("2015-02-20");

Read More

Changes in Java13

Both text blocks and switch expressions still are preview features!

Text Blocks

New way to use multiline strings. They start with 3 " and a newline.

String jsonBlock = """
{
  greeting: "Hello",
  audience: "World"
}
"""

Read More

Spring CORS

CORS (Cross-Origin Resource Sharing)

It’s a mechanism to let a web application running at one domain, protocol or port have permission to access resources from a server at a different one.

This is needed if you have, for example, a Frontend running on port :3000 (React) consuming a Backend API running on port :34831 (custom port for Spring). Unless CORS are set, FE will not be able to access BE resources.

In Spring

It’s possible to enable them for a single RestResource or globally for the whole application.

(This example has been done in Kotlin)

By RestResource

@RestController
@RequestMapping("courses")
@CrossOrigin("http://localhost:3000")
class CourseRestResource {
  // dependencies and methods
}

Read More

Generate a builder with Lombok

Is possible to auto-generate builders for a Java class using @Builder lombok annotation. They’re really simple though and do not provide auto-filling. They just create an API to fill them with test data.

All we need is to put the annotation into a Java class

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PersonDto {

  private long id;

  private String name;
  private int age;

  @Singular
  private Set<String> hobbies;

}

Read More

How to link an intermediary table

(This was done with MySQL, Hibernate and Lombok)

Setting: We have two Entities, Category and Code. Some categories must contain x codes, others cannot contain y codes and we want to leave open the possibility to “must contain z but not p” at the same time.

How: At database level we’ll have 4 tables with the following structure

client_category

FIELD_NAME FIELD_TYPE CONSTRAINTS
id bigint(19) PK (primary key)
example_string varchar(255)  
example_bool tinyint(1)  

Read More

Thymeleaf

Thymeleaf is an HTML template engine, which provides full Spring support.

<h3>Designate your wrap:<h3>
  <div th:each="ingredient: ${wrap}">
    <input name="ingredients" type="checkbox"
            th:value="${ingredient.id}" />
    <span th:text="${ingredient.name}">ING</span>
    <br/>
  </div>

Operators

  • @{} produces a context-relative path to the /static/ folder

Simple Tags

  • th:src="@{images/taco.png}" retrieves an image with relative path from /static/
  • th:href="@{/styles.css}" retrieve a css file

Read More

Changes in Java12

Switch expression

It has been revamped to act as an expression. It removes the usage of break.

switch(day) {
  case SATURDAY, SUNDAY -> System.out.println(1);
  case TUESDAY, FRIDAY -> System.out.println(2);
  case THURSDAY, MONDAY -> System.out.println(3);
  case WEDNESDAY -> System.out.println(4);
}

Read More

Spring Beans

A spring bean is the basic building block of a Spring App. In its basis, it’s an Object which Spring Framework manages at runtime.

This management includes:

  • Creating an Object
  • Filling dependencies
  • Intercepting method calls
  • Destroying the Object

Define a Spring Bean

There’re 3 ways:

  • declare it with @Component annotation
  • @Bean annotation at a Bean Factory
  • .xml config. file (old way)

Read More

Java11 - Run file as a script

(This uses > java11)

To run a file as Java, we don’t need to do anything special to the .java file. Just write a class with a main() method and call it with java --source 11 file.java

Linux Shebang

To start it as a script in Linux we need to add java’s shebang #!/opt/jdk-11/bin/java --source 11 and do it executable chmod +x file.java. The shebang may need to be replaced if the java path is different.

Important

If we’re starting a java file as a script, the file’s name cannot end with .java or it won’t work.

Read More

Maven

Checkstyle

To check all the errors in checkstyle and where they’re located:

  1. go to the project’s folder where the error triggers
  2. open target/checkstyle-result.xml
  3. search for ="error"
  4. there the classes’ name and the error line # can be seen

PMD

To find where the error is located. Search for the String priority="1". If nothing is found, search for prio 2, 3…

Read More

Java experience sheet

Read files > 1 GB lazily

This reads big files (>200 MBs) sequentially and without loading the whole File in memory. This way we’re able to read text files on the Gigabyte level. This example was done reading from a remote SFTP server.

final ChannelSftp sftpClient = this.connect();
final InputStream is = sftpClient.get(file);
final InputStreamReader isReader
      = new InputStreamReader(is);  

try (final BufferedReader bffReader  
      = new BufferedReader(isReader)) {
  bffReader.lines()
        .forEach(this::doAction);
} catch(final IOException ex) {
  log.error("bla", ex);
}

Read More

Vertx

Compile & execute:

mvn clean install  
java -jar target/[substitute_with_name]-fat.jar -cluster

Standard vs Worker Verticle

Concurrency is handled completely by Vert.x

When created, a standard verticle will have one Event-loop assigned to it (it’ll always use the same) and the start method it’s called within that Event-loop. If it calls other Handlers, it can guarantee that they’ll be executed in the same Event-Loop
Meanwhile, a worker verticle will have a different Thread assigned to it, everytime it wants to perform a Job.
If you’re able to use a standard verticle for non-blocking jobs, you’ll save resources every time you execute code with it.

A standard verticle runs in an Event-Loop thread (one of many). If they’re completely blocked, the whole Program will be blocked and it will just halt. On the other side, the worker verticles run on a different Thread than the main event-loop, so they’re perfect to execute blocking code (another option is an inline .executeBlocking() call). They will never be executed by more than one Thread simultaneously, but they may be executed each time by different Threads.

The downside of using always workers, is that the max. concurrency achievable is much lesser than using normal verticles + workers. With a lot of blocking tasks, you may create a processing queue.

Read More

Java testing notes

Codearte’s Catch exception

The only thing it does, it’s to do a bit easier to test and assert for exceptions in a Test Driven Development-like way. To use together with AssertJ. It only has two methods which are useful to me:

// When
BDDCatchException.when(this.instance)  
	.methodWhichThrowsException();

// Then
Assertions  
.assertThat(BDDCatchException.caughtException())  
.isNotNull()  
.isExactlyInstanceOf(IndexOutOfBoundsException.class);

Reference

https://github.com/Codearte/catch-exception

Read More

From Java8 to Java11

This is a list of the changes at Java’s API I found interesting or that I may use frecuently. Not all the changes from Java9, 10 & 11 are listed here.

Java 9

Java REPL (JShell)

It stands for Java Shell. It’s used to easily execute and test any Java construction like a class, interface, enum, etc.

Module System

The way we deploy Java-Based applications using jars has a lot of limitations & drawbacks. Some of these are: The JRE & JDK are too big; JAR files are too big to use in small devices and applications; There’s no strong encapsulation, public is open to everyone.

The new Module System introduces new features to avoid all this. More information here.

Factory Methods for Inmutable Collections (List, Map, Set & Map.Entry)

(I’ll use Lists as an example in this file, but this is valid for Maps and Sets too)

Until Java8 we could use Collections.unmodifiableList() to achieve this, but this is really verbose. Now we can do the same with

List inmutableList = List.of("bla", "ble", "bli");

Read More

Spring Cache

Spring Cache

A cache itself may be imagined as a key-value map. For a basic Cache we need:

  • @EnableCaching tag in @Configuration class
  • Declare a CacheManager Bean
  • Tag the method to cache w. @Cacheable
  • Create a method with @CacheEvict

We may declare +1 cache(s) at the cache manager and select the one we want to use in the method we tag. As key for the cache we may use any conjunction of the parameters given to the method, or if this is a class, any of it’s accessible variables. The cache will only be triggered when the exact key is given again. Then the method won’t be executed and the value will be directly given from the cache. If the parameters don’t match any key, the method will be executed as normal and then the value will be saved into the cache to be returned the next time.

Caution with logs in big apps as they need to be written accordingly.
The hard part is not knowing when to cache something, but to know when to Evict the cache.

Read More