Categories

Spring

  • Making queries on demand: MongoDB outbound gateway

    In order to read data from MongoDb, Spring Integration comes with the MongoDb inbound channel adapter. This adapter uses a poller to continuously retrieve documents from the database. However, sometimes we may need to query the database on demand, based on the result of another endpoint.

    Taking advantage of Spring’s extensibility, I implemented a MongoDb outbound gateway. The purpose of...

  • Spring Integration MongoDB adapters with Java DSL

    This post explains how to save and retrieve entities from a MongoDB database using Spring Integration. In order to accomplish that, we are going to configure inbound and outbound MongoDB channel adapters using the Java DSL configuration extension. As an example, we are going to build an application to allow you to write orders to a MongoDB store, and then...

  • Spring Integration – Polling file creation and modification

    File support is another of Spring Integration’s endpoints to communicate with external systems. In this case, it provides several components to read, write and transform files. During this post, we are going to write an application which monitors a directory in order to read all files in there. In concrete it does the following:

    • When the application starts, it...
  • Understanding Callable and Spring DeferredResult

    Asynchronous support introduced in Servlet 3.0 offers the possibility to process an HTTP request in another thread. This is specially interesting when you have a long running task, since while another thread processes this request, the container thread is freed and can continue serving other requests.

    This topic has been explained many times, but there seems to be a little...

  • Spring Integration – Configure web service client timeout

    With the support of Spring Integration, your application can invoke a web service by using an outbound web service gateway. The invocation is handled by this gateway. Hence, you just need to worry about building the request message and handling the response. However, with this approach it is not obvious how to configure additional options like setting timeouts or caching...

  • Thymeleaf integration with Spring (Part 2)

    This is the second part of the Thymeleaf integration with Spring tutorial. You can read the first part here, where you will learn how to configure this project.

    As explained at the beginning of the first part of this tutorial, the web application will send two types of requests:

    • Insert a new guest: Sends a...

  • Thymeleaf integration with Spring (Part 1)

    This article is focused on how Thymeleaf can be integrated with the Spring framework. This will let our MVC web application take advantage of Thymeleaf HTML5 template engine without losing any of the Spring features. The data layer uses Spring Data to interact with a mongoDB database.

    The example consists in a Hotel’s single page web application from where we...

  • How error handling works in Spring Integration

    The target of this post is to show you how error handling works in Spring Integration, using the messaging system. You will see that error handling is different between synchronous and asynchronous messaging. As usual, I’ll skip the chat and proceed with some examples.

    You can get the source code at my Github repository

    1 The...

  • Spring Integration – Using RMI Channel Adapters

    This article explains how to send and receive messages over RMI using Spring Integration RMI channel adapters. It is composed of the following sections:

    • Implement the service: The first section focuses on creating and exposing a service.
    • Implement the client: Shows how to invoke the service using the MessagingTemplate class.
    • Abstracting SI logic: Finally, I’ve added another section explaining...
  • Creating contract-first web services with Spring WS

    This article explains how to implement and test SOAP web services with Spring WS project. This example uses JAXB2 for (un)marshalling. To develop the service, I’ll use the contract-first approach, which consists in defining the service contract first, and based on this contract implement the service.

    The article is divided into the following sections:

    2  ...
  • Processing messages in transactions with Spring JMS

    This post shows how to process messages in transactions with Spring JMS. We will see how an error in the execution of the consumer during the asynchronous reception of messages with JMS, can lead to the loss of messages. I then will explain how you can solve this problem using local transactions.

    You will also see that this solution can...

  • Introduction to messaging with Spring JMS

    In this post I will show you how to configure a standalone application in order to see different ways of sending and receiving messages using Spring JMS. Basically, I will divide the examples into the following sections:

    • Point-to-point messaging (queue)
      • Synchronous reception
      • Asynchronous reception
    • Publish-subscribe messaging (topic)

    The source code with all the examples shown...

  • Applying aspect oriented programming

    This article explains how to apply aspect oriented programming with Spring AOP. The main target of the aspect oriented programming is the separation of cross-cutting concerns. When we talk about cross-cutting concerns we are referring to generic functionality that is used in several places in our system or application.

    These concepts are, among others:

    • Logging
    • Transaction management
    • Error handling
    • ...
  • Retrieving method annotations with Spring AnnotationUtils

    The JDK provides us with several lookup methods that allow us to retrieve annotations from a class, method, field or added to method parameters. The Spring AnnotationUtils is a general utility class for annotations which extends the basic functionalities. In this post I will explain the main features of this class, focusing on retrieving annotations from a method.

    <h2…

  • Communication in Spring Webflow 2

    This article tries to complement the reference documentation with examples on variables, scopes and flows in Spring Webflow 2. It shows different ways to share data between controllers and views that form a flow. The article is divided into the following sections:

    • Setting flow variables
    • Setting attribute values
    • Using additional scopes
    • Communication with sub flows
    • Communication with other flows
    • ...
  • Accessing Restful services. HTTP Message converters

    When accessing to Restful services, the Spring class RestTemplate maintains a list of message converters. This list will be used to marshal objects into the request body, or unmarshalling them from the response. When instantiating the RestTemplate class, it automatically fills a list with several converters:

    • ByteArrayHttpMessageConverter
    • StringHttpMessageConverter
    • ResourceHttpMessageConverter
    • SourceHttpMessageConverter
    • AllEncompassingFormHttpMessageConverter
     <span...
  • Spring property-placeholder: External properties configuration

    A common way of setting the configuration of the web application is by defining it in a properties file. We can find this file in the classpath. The way of doing this is using the PropertyPlaceholderConfigurer class, or simplifying the configuration, using the context namespace property-placeholder.

    <context:property-placeholder location="datasource.properties"/>

    That’s ok but, what if you need a different configuration...

  • Create and test REST services with Spring MVC

    The first part of this example shows how to create and test REST services using Spring MVC. The controller contains CRUD operations on warehouses and its products. For this example, the repository is a stub that simulates access to the database.
    The second part will...
  • Testing Spring Webflow 2 with inheritance

    This blog entry shows how to test a flow with inheritance in Spring Webflow 2. The flow to be tested consists of a simple navigation which starts with a view state and ends getting to another view state that will depend on the result of the execution of a controller. This flow extends another flow which basically contains...

  • Configure Spring Webflow 2 with jsp views

    This tutorial explains how to configure Spring Webflow 2, showing how it integrates with the view layer, in this case, JSP pages. The web application will allow two different types of request, flow executions and requests to Spring Web MVC. When a request comes in, it will try to find a flow. If not successful, it will try...


Spring Webflow

  • Communication in Spring Webflow 2

    This article tries to complement the reference documentation with examples on variables, scopes and flows in Spring Webflow 2. It shows different ways to share data between controllers and views that form a flow. The article is divided into the following sections:

    • Setting flow variables
    • Setting attribute values
    • Using additional scopes
    • Communication with sub flows
    • Communication with other flows
    • ...
  • Testing Spring Webflow 2 with inheritance

    This blog entry shows how to test a flow with inheritance in Spring Webflow 2. The flow to be tested consists of a simple navigation which starts with a view state and ends getting to another view state that will depend on the result of the execution of a controller. This flow extends another flow which basically contains...

  • Configure Spring Webflow 2 with jsp views

    This tutorial explains how to configure Spring Webflow 2, showing how it integrates with the view layer, in this case, JSP pages. The web application will allow two different types of request, flow executions and requests to Spring Web MVC. When a request comes in, it will try to find a flow. If not successful, it will try...


Spring MVC

  • Understanding Callable and Spring DeferredResult

    Asynchronous support introduced in Servlet 3.0 offers the possibility to process an HTTP request in another thread. This is specially interesting when you have a long running task, since while another thread processes this request, the container thread is freed and can continue serving other requests.

    This topic has been explained many times, but there seems to be a little...

  • Thymeleaf integration with Spring (Part 2)

    This is the second part of the Thymeleaf integration with Spring tutorial. You can read the first part here, where you will learn how to configure this project.

    As explained at the beginning of the first part of this tutorial, the web application will send two types of requests:

    • Insert a new guest: Sends a...

  • Thymeleaf integration with Spring (Part 1)

    This article is focused on how Thymeleaf can be integrated with the Spring framework. This will let our MVC web application take advantage of Thymeleaf HTML5 template engine without losing any of the Spring features. The data layer uses Spring Data to interact with a mongoDB database.

    The example consists in a Hotel’s single page web application from where we...

  • Applying aspect oriented programming

    This article explains how to apply aspect oriented programming with Spring AOP. The main target of the aspect oriented programming is the separation of cross-cutting concerns. When we talk about cross-cutting concerns we are referring to generic functionality that is used in several places in our system or application.

    These concepts are, among others:

    • Logging
    • Transaction management
    • Error handling
    • ...
  • Accessing Restful services. HTTP Message converters

    When accessing to Restful services, the Spring class RestTemplate maintains a list of message converters. This list will be used to marshal objects into the request body, or unmarshalling them from the response. When instantiating the RestTemplate class, it automatically fills a list with several converters:

    • ByteArrayHttpMessageConverter
    • StringHttpMessageConverter
    • ResourceHttpMessageConverter
    • SourceHttpMessageConverter
    • AllEncompassingFormHttpMessageConverter
     <span...
  • Create and test REST services with Spring MVC

    The first part of this example shows how to create and test REST services using Spring MVC. The controller contains CRUD operations on warehouses and its products. For this example, the repository is a stub that simulates access to the database.
    The second part will...

Java

  • Grouping, transforming and reduction with Java 8

    In this previous post, I wrote about how we can group collections of objects with streams and grouping. This is useful but does not cover specific use cases. For example, sometimes we do not only need to group things but also transform the result into a more appropriate object.

    In this post, we will learn how to...

  • Multi level grouping with streams

    With Java 8 streams it is pretty easy to group collections of objects based on different criteria. In this post, we will see how we can make stream grouping, from simple single level groupings to more complex, involving several levels of groupings.

    We will use two classes to represent the objects we want to group by: person and pet.

    Person.class

  • Improving performance: non-blocking processing of streams

    Imagine we have an application that needs to access an external web service in order to gather information about clients and then process it. More specifically, we can’t get all this information in a single invocation. If we want to look up different clients, we will need several invocations.

    As shown in the graphic below, the example application will retrieve...

  • Java Concurrency Tutorial – Locking: Explicit locks

    In many cases, using implicit locking is enough. Other times, we will need more complex functionalities. In such cases, java.util.concurrent.locks package provides us with lock objects. When it comes to memory synchronization, the internal mechanism of these locks is the same as with implicit locks. The difference is that explicit locks offer additional features.

    The main advantages or improvements over...

  • Java Concurrency Tutorial

    This Java concurrency tutorial consists of several posts that explain the main concepts of concurrency in Java. It starts with the basics with posts about the main concerns or risks of using non synchronised programs.

    This tutorial consists on two main areas:

    • Basic concepts about how concurrency works
    • This section explains different synchronisation mechanisms

    Therefore, each section concept...

  • Java Concurrency Tutorial – Thread-safe designs

    After reviewing what the main risks are when dealing with concurrent programs (like atomicity or visibility), we will go through some class designs that will help us prevent the aforementioned bugs. Some of these designs result in the construction of thread-safe concurrency solutions. This will allow us to share objects safely between threads. As...

  • Java Concurrency Tutorial – Visibility between threads

    When sharing an object’s state between different threads, other issues besides atomicity come into play. One of them is visibility between threads.

    The key fact is that without synchronization, instructions are not guaranteed to be executed in the order in which they appear in your source code. This won’t affect the result in a single-threaded program but,...

  • Java Concurrency Tutorial – Atomicity and race conditions

    Atomicity and race conditions is one of the key concepts in multi-threaded programs. We say a set of actions is atomic if they all execute as a single operation, in an indivisible manner. Taking for granted that a set of actions in a multi-threaded program will be executed serially may lead to incorrect results. The reason is due to thread...

  • Unit testing with PowerMock

    In this article I will implement unit testing with PowerMock library. This framework is more powerful than other libraries and allows you to mock static methods, private methods and change field properties among other things. I will use the Mockito extension but it also supports EasyMock.

    1   Installation

    If you use Maven, you can add the following...


Test

  • Unit testing with PowerMock

    In this article I will implement unit testing with PowerMock library. This framework is more powerful than other libraries and allows you to mock static methods, private methods and change field properties among other things. I will use the Mockito extension but it also supports EasyMock.

    1   Installation

    If you use Maven, you can add the following...


REST


AOP

  • Applying aspect oriented programming

    This article explains how to apply aspect oriented programming with Spring AOP. The main target of the aspect oriented programming is the separation of cross-cutting concerns. When we talk about cross-cutting concerns we are referring to generic functionality that is used in several places in our system or application.

    These concepts are, among others:

    • Logging
    • Transaction management
    • Error handling
    • ...

Integration

  • Making queries on demand: MongoDB outbound gateway

    In order to read data from MongoDb, Spring Integration comes with the MongoDb inbound channel adapter. This adapter uses a poller to continuously retrieve documents from the database. However, sometimes we may need to query the database on demand, based on the result of another endpoint.

    Taking advantage of Spring’s extensibility, I implemented a MongoDb outbound gateway. The purpose of...

  • Spring Integration MongoDB adapters with Java DSL

    This post explains how to save and retrieve entities from a MongoDB database using Spring Integration. In order to accomplish that, we are going to configure inbound and outbound MongoDB channel adapters using the Java DSL configuration extension. As an example, we are going to build an application to allow you to write orders to a MongoDB store, and then...

  • Spring Integration – Polling file creation and modification

    File support is another of Spring Integration’s endpoints to communicate with external systems. In this case, it provides several components to read, write and transform files. During this post, we are going to write an application which monitors a directory in order to read all files in there. In concrete it does the following:

    • When the application starts, it...
  • Spring Integration – Configure web service client timeout

    With the support of Spring Integration, your application can invoke a web service by using an outbound web service gateway. The invocation is handled by this gateway. Hence, you just need to worry about building the request message and handling the response. However, with this approach it is not obvious how to configure additional options like setting timeouts or caching...

  • How error handling works in Spring Integration

    The target of this post is to show you how error handling works in Spring Integration, using the messaging system. You will see that error handling is different between synchronous and asynchronous messaging. As usual, I’ll skip the chat and proceed with some examples.

    You can get the source code at my Github repository

    1 The...

  • Spring Integration – Using RMI Channel Adapters

    This article explains how to send and receive messages over RMI using Spring Integration RMI channel adapters. It is composed of the following sections:

    • Implement the service: The first section focuses on creating and exposing a service.
    • Implement the client: Shows how to invoke the service using the MessagingTemplate class.
    • Abstracting SI logic: Finally, I’ve added another section explaining...
  • Processing messages in transactions with Spring JMS

    This post shows how to process messages in transactions with Spring JMS. We will see how an error in the execution of the consumer during the asynchronous reception of messages with JMS, can lead to the loss of messages. I then will explain how you can solve this problem using local transactions.

    You will also see that this solution can...

  • Introduction to messaging with Spring JMS

    In this post I will show you how to configure a standalone application in order to see different ways of sending and receiving messages using Spring JMS. Basically, I will divide the examples into the following sections:

    • Point-to-point messaging (queue)
      • Synchronous reception
      • Asynchronous reception
    • Publish-subscribe messaging (topic)

    The source code with all the examples shown...


Spring JMS


Spring WS

  • Creating contract-first web services with Spring WS

    This article explains how to implement and test SOAP web services with Spring WS project. This example uses JAXB2 for (un)marshalling. To develop the service, I’ll use the contract-first approach, which consists in defining the service contract first, and based on this contract implement the service.

    The article is divided into the following sections:

    2  ...

mongoDB


Thymeleaf

  • Thymeleaf integration with Spring (Part 2)

    This is the second part of the Thymeleaf integration with Spring tutorial. You can read the first part here, where you will learn how to configure this project.

    As explained at the beginning of the first part of this tutorial, the web application will send two types of requests:

    • Insert a new guest: Sends a...

  • Thymeleaf integration with Spring (Part 1)

    This article is focused on how Thymeleaf can be integrated with the Spring framework. This will let our MVC web application take advantage of Thymeleaf HTML5 template engine without losing any of the Spring features. The data layer uses Spring Data to interact with a mongoDB database.

    The example consists in a Hotel’s single page web application from where we...


web services

  • Spring Integration – Configure web service client timeout

    With the support of Spring Integration, your application can invoke a web service by using an outbound web service gateway. The invocation is handled by this gateway. Hence, you just need to worry about building the request message and handling the response. However, with this approach it is not obvious how to configure additional options like setting timeouts or caching...


Concurrency

  • Improving performance: non-blocking processing of streams

    Imagine we have an application that needs to access an external web service in order to gather information about clients and then process it. More specifically, we can’t get all this information in a single invocation. If we want to look up different clients, we will need several invocations.

    As shown in the graphic below, the example application will retrieve...

  • Java Concurrency Tutorial – Locking: Explicit locks

    In many cases, using implicit locking is enough. Other times, we will need more complex functionalities. In such cases, java.util.concurrent.locks package provides us with lock objects. When it comes to memory synchronization, the internal mechanism of these locks is the same as with implicit locks. The difference is that explicit locks offer additional features.

    The main advantages or improvements over...

  • Java Concurrency Tutorial

    This Java concurrency tutorial consists of several posts that explain the main concepts of concurrency in Java. It starts with the basics with posts about the main concerns or risks of using non synchronised programs.

    This tutorial consists on two main areas:

    • Basic concepts about how concurrency works
    • This section explains different synchronisation mechanisms

    Therefore, each section concept...

  • Java Concurrency Tutorial – Thread-safe designs

    After reviewing what the main risks are when dealing with concurrent programs (like atomicity or visibility), we will go through some class designs that will help us prevent the aforementioned bugs. Some of these designs result in the construction of thread-safe concurrency solutions. This will allow us to share objects safely between threads. As...

  • Java Concurrency Tutorial – Visibility between threads

    When sharing an object’s state between different threads, other issues besides atomicity come into play. One of them is visibility between threads.

    The key fact is that without synchronization, instructions are not guaranteed to be executed in the order in which they appear in your source code. This won’t affect the result in a single-threaded program but,...

  • Java Concurrency Tutorial – Atomicity and race conditions

    Atomicity and race conditions is one of the key concepts in multi-threaded programs. We say a set of actions is atomic if they all execute as a single operation, in an indivisible manner. Taking for granted that a set of actions in a multi-threaded program will be executed serially may lead to incorrect results. The reason is due to thread...


Java8

  • Grouping, transforming and reduction with Java 8

    In this previous post, I wrote about how we can group collections of objects with streams and grouping. This is useful but does not cover specific use cases. For example, sometimes we do not only need to group things but also transform the result into a more appropriate object.

    In this post, we will learn how to...

  • Multi level grouping with streams

    With Java 8 streams it is pretty easy to group collections of objects based on different criteria. In this post, we will see how we can make stream grouping, from simple single level groupings to more complex, involving several levels of groupings.

    We will use two classes to represent the objects we want to group by: person and pet.

    Person.class

  • Improving performance: non-blocking processing of streams

    Imagine we have an application that needs to access an external web service in order to gather information about clients and then process it. More specifically, we can’t get all this information in a single invocation. If we want to look up different clients, we will need several invocations.

    As shown in the graphic below, the example application will retrieve...


Spring-Boot


Spring-Data