Ihre Browserversion ist veraltet. Wir empfehlen, Ihren Browser auf die neueste Version zu aktualisieren.

JMeter and SpringBoot REST services

It is important to ensure effective performance for software applications, and software testing is often required to ensure that the application runs without any failures. This blog on JMeter will provide in-depth knowledge about this load testing tool for analyzing and measuring the performance in the following sequence.

Recording with JMeter

If you need to simulate uploading a file to application under test, the fastest way of creating a JMeter test plan is recording the corresponding request via the HTTP(S) Test Script Recorder. JMeter comes with a proxy server which captures all traffic between the browser and the application under test, and converts it into the relevant HTTP Request samplers.

Spring the ultimate guides

Whatever you're building, these guides are designed to get you productive as quickly as possible – using the latest Spring project releases and techniques as recommended by the Spring team.

All the Spring Guides Together

SpringBoot Milestones

Spring, Swagger uploading files

So what is the entity fetching strategy anyway?

When JPA loads an entity, it also loads all the EAGER or “join fetch” associations too. As long as the persistence context is opened, navigating the LAZY association results in fetching those as well, through additionally executed queries.Planning your fetching strategy from the very beginning, and adjusting it all along the development cycle isn’t a “premature optimization”; it’s just a natural part of any ORM design.

The LazyInitializationException is undoubtedly one of the most common exceptions you can get when using Hibernate. Unfortunately, JPA 1.0 decided that @ManyToOne and @OneToOne should default to FetchType.EAGER, so now you have to explicitly mark these two associations as FetchType.LAZY:

The solution, DTO projections with Spring Data Projections and Open Projections and the most beautiful Dynamic Projections

Spring Data JPA Specification

Spring Data JPA was created primarily to allow easy query creation with query generation by method name. However, sometimes, we need to create complex queries and cannot take advantage of a query generator. The Spring Data JPA provides a repository programming model that starts with an interface per managed domain object. Defining these interfaces serves two purposes: first, by extending the JpaRepository, we get a bunch of generic CRUD methods, like save, findAll, delete, and so on.

To Create Complex Queries, Why Specification?

Yes, complex queries can be build using the Criteria API. For any complex-enough API – searching/filtering your resources by very simple fields is simply not enough. A query language is more flexible and allows you to filter down to exactly the resources you need.

Specifications and Querydsl

To be able to define reusable predicates, Spring introduced the specification interface that is derived from concepts introduced in Eric Evans' Domain Driven Design book. It defines a specification as a predicate over an entity, which is exactly what our specification interface represents. This actually only consists of a single method:

 
public interface Specification<T> {
Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb);
}
 
When using Java 8, the code becomes very clear and easy to understand.
 
public static Specification<Customer> customerHasBirthday() {
  return (root, query, cb) ->{
return cb. equal(root. get(Customer_. birthday), today);
  };
}
A client can now perform the following:
 
customerRepository. findAll(hasBirthday)());
 
We just created the reusable predicates that can be individually executed. We can combine these individual predicates to meet our business requirement. We have a helper class, specifications, that provides and methods to concatenate atomic specifications.and(…)or(…)
 
customerRepository. findAll(where(customerHasBirthday()) and(isLongTermCustomer)()));
 
This reads fluently, improving readability as well as providing additional flexibility when compared to the use of the JPA Criteria API alone.

Here are some benefits of specifications:

  1. All "basic" queries are already implemented, i.e. findById, save, and delete
  2. Pagination works out of the box. You can pass an object simply from the Controller to the Service to your Repository, and it just works (even sorting)!Pageable
  3. Using Spring's API is a little bit more simple than plain JPA stuff because you just create predicates and don't have to mess around with the and .SpecificationEntityManagerPersistenceContext

Hibernate specifics

Strategy Design Pattern and Lambda's

Read this wonderful post about different classifications of patterns!!!

The idea of the strategy pattern is to define a family of algorithms, encapsulate what varies into separate classes and make their objects interchangeable in the context. Varies here means: may change over time due to the evolving requirements.

Lambda expressions have changed the world in Java, and we can effectively use lambda expressions to avoid writing a lot of ceremonial code. As far as the strategy design pattern is concerned we don't have to create a hierarchy of classes. Instead, we can directly pass the strategy implementation of the interface as a lambda expression to the context.

Take one functional interface that fits your family of algorithms, declare them all statically in one place (single class).

Micro-Services with Eureka and Zuul proxy with Feign Client

As an edge service, Zuul provides a lot of different functions. The main pieces we will talk about are the dynamic proxy and security.

Zuul will serve as our API gateway. This, along with the service discovery of Eureka, makes setting up new services a breeze. We will demonstrate a small Zuul application. It will register with a Eureka server and automatically set up dynamic routing based on other services that are also registered with Eureka to provide access to our APIs through one singular point. So Zuul wants to be configured with Spring Security in order to provide edge security across all our APIs.

All You Need To Know For Migrating To Java 11

We haven't fully immersed ourselves in Java 10 yet, and Java 11 is here. Java 11 is important for more than just a few reasons. Oracle has revamped its support model and come up with a release train that'll bring rapid updates, about every 6 months.

Negate a Predicate with Java 11

The Predicate.not() static method has been added to Java 11 in order to negate an existing Predicate.

people.stream() .filter(Predicate.not(Person::isAdult)) .collect(Collectors.toList());

 

Mockito

MockMvc Testing

Migrating from JUnit 4 to JUnit 5

Let's start with the previous version – JUnit 4 has some clear limitations:

  • The entire framework was contained in a single jar library. The whole library needs to be imported even when only a particular feature is required. In JUnit 5, we get more granularity and can import only what is necessary
  • One test runner can only execute tests in JUnit 4 at a time (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously
  • JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of Java 8 features
  1. Migrating from a JUnit4-Based Runner
  2. junit 5 migration for Springboot applications
  3. Junit5 and @RunWith
  4. Guide to JUnit5
  5. how to integrate Mockito with the JUnit 5 extension model
  6. Unit testing with JUnit 5, Mockito, and Hamcrest

Java 8 Date – LocalDate, LocalDateTime, Instant

Java 8 Date Time API is one of the most sought after change for developers. Java has been missing a consistent approach for Date and Time from start and Java 8 Date Time API is a welcome addition to the core Java APIs.

Time Complexity of Java Collections

we'll talk about the performance of different collections from the Java Collection APIUsually, when we talk about time complexity, we refer to Big-O notation. Simply put, the notation describes how the time to perform the algorithm grows with the size of the input. This notation approximately describes how the time to do a given task grows with the size of the input. ... Roughly speaking, on one end we have O(1) which is “constant time” and on the opposite end we have O(xn) which is “exponential time”.

  1. Constant means that the time is always the same, it doesn't matter the length of the List
  2. Linear time means the longer the ArrayList is (more object it contains) the longer time the op will take, linear means that the more the List grows, the longer is the time, but in a linear way, so for example to perform a linear operation on a list that contains 20 elements it will take two times the time needed for a list with 10 elements.
Performing the fastest search - which collection should i use?
  • If you need fast access to elements using indexArrayList should be choice.
  • If you need fast access to elements using a key, use HashMap.
  • If you need fast add and removal of elements, use LinkedList (but it has a very poor seeking performance).
Which collection is best for insertion and deletion?
1) As explained above the insert and remove operations give good performance ( O(1) ) in LinkedList compared to ArrayList( O(n) ). Hence if there is a requirement of frequent addition and deletion in application then LinkedList is a best choice.
Which collection is best for sorting in Java?
HashSet has slightly better performance than LinkedHashSet , but its iteration order is undefined. TreeSet is ordered and sorted, but slower. TreeMap is ordered and sorted, but slower. LinkedList has fast adding to the start of the list, and fast deletion from the interior via iteration.
Which is better ArrayList or LinkedList in Java?
ArrayList is faster than LinkedList if I randomly access its elements. ... ArrayList has direct references to every element in the list, so it can get the n-th element in constant time. LinkedList has to traverse the list from the beginning to get to the n-th element. LinkedList is faster than ArrayList for deletion

ML Machine Learning and Springboot

"A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E". So if you want your program to predict, for example, traffic patterns at a busy intersection (task T), you can run it through a machine learning algorithm with data about past traffic patterns (experience E) and, if it has successfully “learned”, it will then do better at predicting future traffic patterns (performance measure P).

Intellij Java and Springboot

Java interview questions

How to write Clean Java Code

Besides studying hard, to become a good software developer in Java or any other language, you must master concepts and code conventions to make a clean code and easy to maintain.

About Maps and EntrySets

Literally initialize HashMap

// this works for up to 10 elements:
Map<String, String> test1 = Map.of(
    "a", "b",
    "c", "d"
);

// this works for any number of elements:
import static java.util.Map.entry;    
Map<String, String> test2 = Map.ofEntries(
    entry("a", "b"),
    entry("c", "d")
);

Streams

Characteristics:

  1. Does not store data, never modifies the underlying data source
  2. Apply functions to sequences of objects
  3. Lazy initialization, because intermediate operations are not evaluated until terminal operation is invoke.
  4. Does not safe data.
  5. Each intermediate operation creates a new stream to operate on

Good links

However, while there is no problem with doing this using either Collection.forEach() or stream().forEach(), Java requires an operation on a stream to be non-interfering. This means that elements shouldn't be modified during the execution of the stream pipeline.

The reason behind this is that the stream should facilitate parallel execution. Here, modifying elements of a stream could lead to unexpected behavior.

Hosted by WEBLAND.CH