tag:blogger.com,1999:blog-60260734239546625092024-03-05T19:24:20.837+01:00Xavier Padró's BlogJava technology stuffAnonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.comBlogger41125tag:blogger.com,1999:blog-6026073423954662509.post-3796091522641406222016-12-06T10:19:00.000+01:002016-12-06T10:19:31.135+01:00Making queries on demand: MongoDB outbound gateway<h3>
<span style="color: #0b5394;">1- Introduction</span></h3>
<br />
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.<br />
<br />
Taking advantage of Spring's extensibility, I implemented a MongoDb outbound gateway. The purpose of this gateway is to react to some request, make a query to the database and return the result.<br />
<br />
In order to show you how the gateway works, I will use a simple example and modify it to implement the gateway with different configurations.<br />
<br />
This example consists in a messaging gateway as the entry point to the integration flow. Once a message enters the flow, the mongoDb outbound gateway makes a query to the database and the result is then sent to another channel where a service activator will process it.<br />
<br />
The source code for these examples and the gateway implementation can be found in <a href="https://github.com/xpadro/spring-integration/tree/master/mongodb" target="_blank">my repository</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2- Java DSL example</span></h3>
<br />
I implemented the <a href="https://github.com/xpadro/spring-integration/blob/master/mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/gateway/MongoDb.java" target="_blank">MongoDb</a> static class to ease the definition of the gateway. I took the idea from the Spring Integration <a href="http://docs.spring.io/autorepo/docs/spring-integration-java-dsl/1.2.0.BUILD-SNAPSHOT/api/org/springframework/integration/dsl/jpa/Jpa.html" target="_blank">Jpa</a> class.<br />
<br />
In the following configuration you can see the flow <i>requestPerson</i>. An invocation to PersonService's <i>send</i> method will send a message to the flow, where the mongoDb outbound gateway will then query the database with a pre-defined query ({id : 1}):
<br />
<br />
<script src="https://gist.github.com/xpadro/ef8d5b8ec7e35cfa1b334ae08d876944.js"></script>
The result handler is a "very useful" component which will log the retrieved person:<br />
<br />
<script src="https://gist.github.com/xpadro/5d21c24bf36f57193a507925d13c81ad.js"></script>
In order to start the flow, the following application sends a message to the PersonService gateway:<br />
<br />
<script src="https://gist.github.com/xpadro/b0f00ef2f794e31e235ec2dda0491bc4.js"></script>
As a note, the abstract class just contains the logic to set up the database, which is used along all the other examples.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3- Java DSL example with dynamic query expression</span></h3>
<br />
The previous example was useful to see how to define the gateway, but having a hardcoded query may not be the most used case.<br />
<br />
In this example, the query is defined in the message sent to the integration flow:<br />
<br />
<script src="https://gist.github.com/xpadro/0c29e01894b5a4011c141bb0e824ba7d.js"></script>
In the configuration file, the gateway's <i>queryExpression</i> property resolves the query dynamically by retrieving the <i>data</i> property of the message's payload:<br />
<br />
<script src="https://gist.github.com/xpadro/cabd64e1a2c876f4075309ef86c7cf3c.js"></script>
<br />
<h3>
<span style="color: #0b5394;">
4- Java DSL example returning multiple results</span></h3>
<br />
The two previous examples retrieved a single document from the database. In this next example, the query returns a list with all documents matching the query:<br />
<br />
In the request message we specify the query to find all documents in the persons collection:<br />
<br />
<script src="https://gist.github.com/xpadro/fead7776b801047cb5cd7415affa92a7.js"></script>
In the configuration, we have to remove the <i>expectSingleResult</i> property from the gateway (or set it to false). Additionally, we can specify a limit:<br />
<br />
<script src="https://gist.github.com/xpadro/bb21042c45b1e8abbd09bd2b380ed929.js"></script>
Finally, we have to define another method in the ResultHandler class to handle multiple results:<br />
<br />
<script src="https://gist.github.com/xpadro/875a0c159dec6ac0e64b46f906b1d16e.js"></script>
<br />
<h3>
<span style="color: #0b5394;">
5- Java Config example</span></h3>
<br />
In this last example, Java Config is used instead of Java DSL to configure the whole flow. On the application's side everything is the same. We just query the person service for a specific document:<br />
<br />
<script src="https://gist.github.com/xpadro/9ae4b3bf23794896263eb49ac376037c.js"></script>
When using Java Config, we have to build the <a href="https://github.com/xpadro/spring-integration/blob/master/mongodb/mongo-gateway/src/main/java/xpadro/spring/integration/mongodb/gateway/MongoDbExecutor.java" target="_blank">MongoDbExecutor</a>, which is used by the gateway to do the queries.<br />
<br />
<script src="https://gist.github.com/xpadro/99eb60a455afa165e7f513dbb0ceb01e.js"></script>
Listening to the gateway's output channel, we define a service activator to handle the retrieved person:<br />
<br />
<script src="https://gist.github.com/xpadro/7043a070a799269d511921d5ab11ff93.js"></script>
<br />
<h3>
<span style="color: #0b5394;">
6- Conclusion</span></h3>
<br />
An outbound gateway is suitable when you need to do queries on demand instead of actively polling the database. Currently, this implementation supports setup with Java Config and Java DSL. For now, I haven't implemented the parsers needed to support XML configuration since I think these two ways of configuration cover the main necessity.<br />
<br />
If you found this post useful, please share it or star my repository :)<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-88935113623211242382016-11-21T11:17:00.000+01:002016-11-21T11:21:50.482+01:00Spring Integration MongoDB adapters with Java DSL<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
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 retrieve them for processing.<br />
<br />
The application flow can be split in two parts:<br />
<br />
<ul>
<li>New orders are sent to the messaging system, where they will be converted to actual products and then stored to MongoDB.</li>
</ul>
<ul>
<li>On the other hand, another component is continuously polling the database and processing any new product it finds.</li>
</ul>
<br />
The source code can be found in my <a href="https://github.com/xpadro/spring-integration/tree/master/mongodb/mongo-basic" target="_blank">Spring Integration repository</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 MessagingGateway - Entering the messaging system</span></h3>
<br />
Our application does not know anything about the messaging system. In fact, it will just create new orders and send them to an interface (OrderService):<br />
<br />
<script src="https://gist.github.com/xpadro/d48ffd95b5b24e7760fed44bc0d69fa4.js"></script>
Taking an initial look at the configuration, we can see that the OrderService is actually a messaging gateway.
<br />
<br />
<script src="https://gist.github.com/xpadro/3c22dbd0d3b03cbc13cfb30bf36677e5.js"></script>
Any order sent to the order method will be introduced to the messaging system as a Message<Order> through the 'sendOrder.input' direct channel.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 First part - processing orders</span></h3>
<br />
The first part of the Spring Integration messaging flow is composed by the following components:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuDv3ppez_s7mu29ENK27uaXhFdE3rRAnvBzfI47lVsMaCutTMCwq67y5hcOJ9rnVTGLz0WAlGGWaDIfeyDAkW29tQRl0Ya5_dpaOQkp1Zt7nto0ujNTnYgvOxjRrH1kOrKYo5PKk5ZEU/s1600/flow_firstPart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuDv3ppez_s7mu29ENK27uaXhFdE3rRAnvBzfI47lVsMaCutTMCwq67y5hcOJ9rnVTGLz0WAlGGWaDIfeyDAkW29tQRl0Ya5_dpaOQkp1Zt7nto0ujNTnYgvOxjRrH1kOrKYo5PKk5ZEU/s1600/flow_firstPart.png" /></a></div>
<br />
<br />
We use a lambda to create an IntegrationFlow definition, which registers a DirectChannel as its input channel. The name of the input channel is resolved as 'beanName + .input'. Hence, the name is the one we specified in the gateway: 'sendOrder.input'
<br />
<br />
<script src="https://gist.github.com/xpadro/871675b999f8b40e718b4e58b8adcd1c.js"></script>
The first thing the flow does when receiving a new order is use a transformer to convert the order into a product. To register a transformer we can use the Transformers factory provided by the DSL API. Here, we have different possibilities. The one I chose is using a <a href="https://docs.spring.io/spring-integration/api/org/springframework/integration/transformer/PayloadTypeConvertingTransformer.html" target="_blank">PayloadTypeConvertingTransformer</a>, which delegates to a converter the transformation of the payload into an object.
<br />
<br />
<script src="https://gist.github.com/xpadro/53214f7031e4c40624dea4c0c82b1014.js"></script>
The next step in the orders flow is to store the newly created product to the database. Here, we use a MongoDB outbound adapter:
<br />
<br />
<script src="https://gist.github.com/xpadro/60f64edbc066be286f734e7e07521928.js"></script>
If you wonder what the message handler is actually doing internally, it uses a mongoTemplate to save the entity:
<br />
<br />
<script src="https://gist.github.com/xpadro/80f29d2cef916db1a2483cdb0ee4c9ab.js"></script>
<br />
<h3>
<span style="color: #0b5394;">4 Second part - processing products</span></h3>
<br />
In this second part we have another integration flow for processing products:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinyiMHhSdIHxMWjfq9iwnzre_udFk9w0x0s-zhEYZ6jNNBRTygtybbX5yYfTFkZ4wvCkOR1GcfCYVi2pG3QVF536wtsjzEK89ws-w2JbaBqrRcrjAomTtP060HizzQ2nUJxwLCvJAe2bE/s1600/flow_secondPart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinyiMHhSdIHxMWjfq9iwnzre_udFk9w0x0s-zhEYZ6jNNBRTygtybbX5yYfTFkZ4wvCkOR1GcfCYVi2pG3QVF536wtsjzEK89ws-w2JbaBqrRcrjAomTtP060HizzQ2nUJxwLCvJAe2bE/s1600/flow_secondPart.png" /></a></div>
<br />
<br />
In order to retrieve previously created products, we have defined an inbound channel adapter which will continuously be polling the MongoDB database:
<br />
<br />
<script src="https://gist.github.com/xpadro/4295bf98ecb992bd51aa314015933e7b.js"></script>
The MongoDB inbound channel adapter is the one responsible for polling products from the database. We specify the query in the constructor. In this case, we poll one non processed product each time:
<br />
<br />
<script src="https://gist.github.com/xpadro/96e3f87025d19ee7ab225a15b8af363e.js"></script>
The router definition shows how the product is sent to a different service activator method depending on the 'premium' field:
<br />
<br />
<script src="https://gist.github.com/xpadro/c7fd7b413da78fe52bb54cbf6c57fb01.js"></script>
As a service activator, we have a simple bean which logs a message and sets the product as processed. Then, it will return the product so it can be handled by the next endpoint in the flow.
<br />
<br />
<script src="https://gist.github.com/xpadro/6a80fdf999686c0d0b207c968a223aa2.js"></script>
The reason for setting the product as processed is because the next step is to update its status in the database in order to not poll it again. We save it by redirecting the flow to the mongoDb outbound channel adapter again.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion</span></h3>
<br />
You have seen what endpoints you do have to use in order to interact with a MongoDB database using Spring Integration. The outbound channel adapter passively saves products to the database, while the inbound channel adapter actively polls the database to retrieve new products.<br />
<br />
If you found this post useful, please share it or star my repository. I appreciate it :)<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-1228598285593494912016-07-19T14:48:00.001+02:002016-07-19T14:54:19.903+02:00Spring Integration - Polling file creation and modification<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
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:<br />
<br />
<ul>
<li>When the application starts, it reads all files present in the directory.</li>
<li>The application will then keep an eye on the directory to detect new files and existing files which have been modified.</li>
</ul>
<br />
The source code can be found in <a href="https://github.com/xpadro/spring-integration/tree/master/file">Github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Configuration</span></h3>
<br />
The application is built with Spring Boot, since it eases configuration significantly. To create the initial infrastructure of the application, you can go to <a href="https://start.spring.io/">https://start.spring.io/</a>, select the Integration module and generate the project. Then you can open the zip file in your favourite IDE.<br />
<br />
I added a couple of dependencies to the pom.xml like commons.io or Spring Integration Java DSL. My pom.xml file looks like as follows:
<br />
<br />
<script src="https://gist.github.com/xpadro/133cbe5b9492c95788198aabcf118efe.js"></script>
<br />
The starting point is FileReadDirectoryApplication:
<br />
<br />
<script src="https://gist.github.com/xpadro/620e192df68d636f966543dbfc7a709e.js"></script>
Starting from here, we are going to add the Spring Integration components for reading from a specific folder of the filesystem.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Adding the adapter</span></h3>
<br />
In order to read from the file system, we need an inbound channel adapter. The adapter is a file reading message source, which is responsible of polling the file system directory for files and create a message from each file it finds.
<br />
<br />
<script src="https://gist.github.com/xpadro/93c8e42727bf2a7028d847c95267adc2.js"></script>
We can prevent some types of files from being polled by setting a list of filters to the message source. For this example two filters have been included:<br />
<br />
<ul>
<li><i>SimplePatternFileListFilter</i>: Filter provided by Spring. Only files with the specified extension will be polled. In this case, only text files will be accepted.</li>
<li><i>LastModifiedFileFilter</i>: Custom filter. This filter keeps track of already polled files and will filter out files not modified since the last time it was tracked.</li>
</ul>
<br />
<br />
<h3>
<span style="color: #0b5394;">
4 Processing the files</span></h3>
<br />
For each polled file, we will transform its content to String before passing it to the processor. For this purpose, Spring already provides a component:
<br />
<br />
<script src="https://gist.github.com/xpadro/76ca6b2ac536feb1621df624b18db96e.js"></script>
<br />
Hence, instead of receiving a Message<File>, the processor will receive a Message<String>. The file processor is our custom component which will do something as advanced as printing the file content:
<br />
<br />
<script src="https://gist.github.com/xpadro/41cca679f5e05a1ffffa0dcb1c5600f9.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Building the flow</span></h3>
<br />
Now that we have all the required components in place, let's build the flow. We are using Spring Integration Java DSL, since it makes the flow more readable:
<br />
<br />
<script src="https://gist.github.com/xpadro/b3046400bdf76c73cf6208aebdce16b1.js"></script>
<br />
<h3>
<span style="color: #0b5394;">6 Running the application</span></h3>
<br />
In my directory, I already have a file called 'previousFile.txt'. After starting the application, we will create two files and modify one of them.<br />
<br />
<script src="https://gist.github.com/xpadro/297ac3161620e46a126d77541b0cff5b.js"></script>
If we run the application, we should see the following print statements:<br />
<br />
<span style="font-size: x-small;">previousFile.txt received. Content: previous content</span><br />
<span style="font-size: x-small;">file1.txt received. Content: content</span><br />
<span style="font-size: x-small;">file2.txt received. Content: another file</span><br />
<span style="font-size: x-small;">file1.txt received. Content: content modified</span><br />
<br />
<br />
<h3>
<span style="color: #0b5394;">7 Conclusion</span></h3>
<br />
This example shows how simple it is to read files from a directory using Spring Integration, obviously with the help of Spring Boot to simplify the configuration. Depending on your needs, you can add your own custom filters to the message source, or use another one of the provided by Spring, like the <a href="http://docs.spring.io/spring-integration/api/org/springframework/integration/file/filters/RegexPatternFileListFilter.html">RegexPatternFileListFilter</a>. You can check for other implementations <a href="http://docs.spring.io/spring-integration/api/org/springframework/integration/file/filters/FileListFilter.html">here</a>.<br />
<br />
If you found this post useful, please share it or star my repository :)<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com2tag:blogger.com,1999:blog-6026073423954662509.post-37325738321609114022016-05-17T15:27:00.000+02:002017-11-01T19:45:28.400+01:00Data Aggregation Spring Data MongoDB: Nested results<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
In a previous post, we built a basic example of an aggregation pipeline. Maybe you want to take a look at <a href="http://xpadro.blogspot.com/2016/04/data-aggregation-with-spring-data.html">Data aggregation with Spring Data MongoDB and Spring Boot</a> if you need more detail about how to create the project and configure the application. In this post, we will focus on learning a use case where it makes sense to group a portion of the result in a nested object.<br />
<br />
Our test data is a collection of football players, with data about the league they belong to and how many goals they scored. The document would be like this:<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/f4bd0e18174f79acf91846f81e673fc8.js"></script>
It may be interesting to know how many goals were scored in each league. Also, who was the league's top goalscorer. During the following section, we are going to implement our first simple example without using nested objects.<br />
<br />
You can find the source code of all these examples at my <a href="https://github.com/xpadro/spring-data-mongo" target="_blank">Github repository</a>.<br />
<br />
<h3>
<span style="color: #0b5394;">2 Basic example</span></h3>
<br />
We can use the following class to store each league's result:
<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/f68365d31b164a5cc42cb7ad46ddc488.js"></script>
In order to retrieve the top scorers, we will first need to sort the documents by scored goals and then group them by league. In the repository, these two phases of the pipeline are implemented in the following methods:
<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/0b80779759a8be5a6faa7ee6a5dde4af.js"></script>
That should do it. Let's aggregate the results using Spring's mongoTemplate:
<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/df5c82bd3b9a8effdea031b0243f55f6.js"></script>
If we retrieve the stats of the spanish league, we get the following result:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiacxeQpgWfadEq2sKYYKGG2_DKxxN5kwIQEcuSabtfZG83wXOz89G5Z5EfK7HtGH0E26VRVX4BCc79pDbNTbIgkNBE3MZZhou9tJIo1HQpHQUTViisJJwiH1AsXg5rM5ZcoDgDqfI6HzE/s1600/aggregationNotNestedResult.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiacxeQpgWfadEq2sKYYKGG2_DKxxN5kwIQEcuSabtfZG83wXOz89G5Z5EfK7HtGH0E26VRVX4BCc79pDbNTbIgkNBE3MZZhou9tJIo1HQpHQUTViisJJwiH1AsXg5rM5ZcoDgDqfI6HzE/s1600/aggregationNotNestedResult.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
Although this is fair enough, I don't feel comfortable with all top scorer's information scattered throughout the result class. I think it would make much more sense if we could encapsulate all scorer's data into a nested object. Fortunately, we can do that directly during the aggregation.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Nesting the result</span></h3>
<br />
Spring Data's nested method is designed to create sub-documents during the projection phase. This will allow us to create the top goalscorer class as a property of the output result class:
<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/2a9d44b14597a26af8898d3457e98f0d.js"></script>
In the line above, a nested document called topScorer is emitted by the nested method, which will contain all the data about the current league’s top goalscorer. Its properties are mapped to the output class using the bind method (topPlayer, topGoals and topCountry).<br />
<br />
MongoTemplate’s invocation reuses our previous sort and group operations, and then adds the projection operation:
<br />
<br />
<br />
<script src="https://gist.github.com/xpadro/e1feea38b1953cc9d1dc6ea3ac0f073b.js"></script>
Executing this query will result in a much more compact result, having all top goalscorer’s related data wrapped in its own class:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQzM1m2P4I10YCh1P_wkkyTmPqaEmFIzag5n6wfDrCZeOEtjr96TPc6Ne4q-gKhu0fp0nkPG35zgietQE5MfVk5jUvK1vaExop05pVg14FDQZsTNLq3fhSEkpKcwWbkUlx1v3_t2ZdfRY/s1600/aggregationNestedResult.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQzM1m2P4I10YCh1P_wkkyTmPqaEmFIzag5n6wfDrCZeOEtjr96TPc6Ne4q-gKhu0fp0nkPG35zgietQE5MfVk5jUvK1vaExop05pVg14FDQZsTNLq3fhSEkpKcwWbkUlx1v3_t2ZdfRY/s1600/aggregationNestedResult.png" /></a></div>
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Conclusion
</span></h3>
<br />
Spring Data MongoDB nested method is very useful for creating well structured output results from our aggregation queries. Doing this step during the aggregation helps us avoid having java code to post-process the result.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-65527774204123042892016-04-12T15:13:00.000+02:002016-04-12T15:13:17.855+02:00Data aggregation with Spring Data MongoDB and Spring BootMongoDB aggregation framework is designed for grouping documents and transforming them into an aggregated result. The aggregation query consists in defining several stages that will be executed in a pipeline. If you are interested in more in-depth details about the framework, then <a href="https://docs.mongodb.org/manual/aggregation/" target="_blank">mongodb docs</a> are a good point to start.<br />
<br />
The point of this post is to write a web application for querying mongodb in order to get aggregated results from the database. We will do it in a very easy way thanks to Spring Boot and Spring Data. Actually it is really fast to implement the application, since Spring Boot will take care of all the necessary setup and Spring Data will help us configure the repositories.<br />
<br />
The source code can be found on my <a href="https://github.com/xpadro/spring-data-mongo"><span id="goog_294291471"></span>Github repository<span id="goog_294291472"></span></a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">1 The application</span></h3>
<br />
Before going through the code let’s see what we want to do with our application.<br />
<br />
Our domain is a collection of products we have distributed across several warehouses:<br />
<br />
<script src="https://gist.github.com/xpadro/8f700f29925c419e2fc835d57fa6f67a.js"></script>
Our target is to collect all the products within a price range, grouped by warehouse and collecting the total revenue and the average price of each grouping.<br />
<br />
In this example, our warehouses are storing the following products:<br />
<br />
<script src="https://gist.github.com/xpadro/04dc410fe64be51bd6828a694340571b.js"></script>
The application will query for products with a price between 5.0 and 70.0. The required aggregation pipeline steps will be as follows:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIcte_pa5eb_0V1ZOWyhDSMhFvG8twj2yJvblpI2t5Q1Rp11xx8ZZ9JM5tRthrA2YneHXBLoAFeNaMdq4Eh12hgtgmQJr2S8Up4y7jxuoL5vJmcjFc-tb6T_jXoKa07GLi3U0gg82xFtg/s1600/aggregationPipeline.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="421" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIcte_pa5eb_0V1ZOWyhDSMhFvG8twj2yJvblpI2t5Q1Rp11xx8ZZ9JM5tRthrA2YneHXBLoAFeNaMdq4Eh12hgtgmQJr2S8Up4y7jxuoL5vJmcjFc-tb6T_jXoKa07GLi3U0gg82xFtg/s640/aggregationPipeline.png" width="640" /></a></div>
<br />
<br />
We will end up with aggregated results grouped by warehouse. Each group will contain the list of products of each warehouse, the average product price and the total revenue, which actually is the sum of the prices.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Maven dependencies</span></h3>
<br />
As you can see, we have a short <a href="https://github.com/xpadro/spring-data-mongo/blob/master/aggregation-basic/pom.xml" target="_blank">pom.xml</a> with Spring Boot dependencies:<br />
<br />
<script src="https://gist.github.com/xpadro/97bed9d296f65c86912a6dfe7239fb75.js"></script>
By defining spring-boot-starter-parent as our parent pom, we set the default settings of Spring Boot. Mainly it sets the versions of a bunch of libraries it may use, like Spring or Apache Commons. For example, Spring Boot 1.3.3, which is the one we are using, sets 4.2.5.RELEASE as the Spring framework version. Like stated in previous posts, it is not adding libraries to our application, it only sets versions.<br />
<br />
Once the parent is defined, we only need to add three dependencies:<br />
<br />
<ul>
<li>spring-boot-starter-web: Mainly includes Spring MVC libraries and an embedded Tomcat server.</li>
<li>spring-boot-starter-test: Includes testing libraries like JUnit, Mockito, Hamcrest and Spring Test.</li>
<li>spring-boot-starter-data-mongodb: This dependency includes the MongoDB Java driver, and the Spring Data Mongo libraries.</li>
</ul>
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Application setup</span></h3>
<br />
Thanks to Spring Boot, the application setup is as simple as the dependencies setup:<br />
<br />
<script src="https://gist.github.com/xpadro/30200d2d9206797c2685388007b595a6.js"></script>
When running the main method, we will start our web application listening to the 8080 port.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 The repository</span></h3>
<br />
Now that we have the application properly configured, we implement the repository. This isn’t difficult neither since Spring Data takes care of all the wiring.<br />
<br />
<script src="https://gist.github.com/xpadro/df5742d16293f38de4efdd7e24e41004.js"></script>
The following test proves that our application is correctly set up.<br />
<br />
<script src="https://gist.github.com/xpadro/3a95658adaaf165c80405c38592748b5.js"></script>
We didn’t implement save and findOne methods. They are already defined since our repository is extending MongoRepository.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 The aggregation query</span></h3>
<br />
Finally, we set up the application and explained all the steps. Now we can focus on the aggregation query.<br />
<br />
Since our aggregation query is not a basic query, we need to implement a custom repository. The steps are:<br />
<br />
Create the custom repository with the method we need:<br />
<br />
<script src="https://gist.github.com/xpadro/3a1bc08bcb7671337eec56b0d73a24f8.js"></script>
Modify the first repository in order to also extend our custom repository:<br />
<br />
<script src="https://gist.github.com/xpadro/0d2a1058b60c4b5e0ea0ac647c43e35c.js"></script>
Create an implementation to write the aggregation query:<br />
<br />
<script src="https://gist.github.com/xpadro/1cf7e1f2d84d0edfb1778cc7b96b4d37.js"></script>
Now we are going to implement the stages of the mongodb pipeline as explained in the beginning of the post.<br />
Our first operation is the <a href="https://docs.mongodb.org/manual/reference/operator/aggregation/match/" target="_blank">match</a> operation. We will filter out all product documents that are beyond our price range:<br />
<br />
<script src="https://gist.github.com/xpadro/d270c6734a9e1c786af9904569eee717.js"></script>
The next stage of the pipeline is the <a href="https://docs.mongodb.org/manual/reference/operator/aggregation/group/" target="_blank">group</a> operation. In addition to grouping documents by warehouse, in this stage we are also doing the following calculations:<br />
<br />
<ul>
<li>last: Returns the warehouse of the last document in the group.</li>
<li>addToSet: Collects all the unique product Ids of all the grouped documents, resulting in an array.</li>
<li>avg: Calculates the average of all prices in the group.</li>
<li>sum: Sums all prices in the group.</li>
</ul>
<br />
<br />
<script src="https://gist.github.com/xpadro/03a443f5d49095512e86c5357107046f.js"></script>
The last stage of the pipeline is the <a href="https://docs.mongodb.org/manual/reference/operator/aggregation/project/" target="_blank">project</a> operation. Here we specify the resulting fields of the aggregation:<br />
<br />
<script src="https://gist.github.com/xpadro/bde9113a621dec161afaaf3eb95a916f.js"></script>
The query is built as follows:<br />
<br />
<script src="https://gist.github.com/xpadro/8e766f61eea0956474f5494f354a701f.js"></script>
In the aggregate method, we indicate the input class, which is our Product document. The next argument is the output class, which is a DTO to store the resulting aggregation:<br />
<br />
<script src="https://gist.github.com/xpadro/92b39e70545a1fea70e8c4e3f1f99b37.js"></script>
We should end the post with a test proving that results are what we expect:
<br />
<br />
<script src="https://gist.github.com/xpadro/938ee69670a182d5d69d36c52c8f1e76.js"></script>
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
Spring Data has a good integration with MongoDB aggregation framework. Adding Spring Boot to configure the application let's us focus on building the query. For the building process, Aggregation class has several static methods that help us implement the different pipeline stages.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-24615196151379919252016-03-08T08:38:00.000+01:002017-11-01T19:46:28.024+01:00Grouping, transforming and reduction with Java 8<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
In <a href="http://xpadro.blogspot.com/2016/02/multi-level-grouping-with-streams.html" target="_blank">this previous post</a>, 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.<br />
<br />
In this post, we will learn how to apply transformations and reduction to the groupingBy result.<br />
<br />
<a href="https://github.com/xpadro/java8" target="_blank">Here</a> you can view the source code of the following examples.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Grouping by and transform</span></h3>
<br />
Let's take the model I used in the previous post where we had a collection of <a href="https://github.com/xpadro/java8/blob/master/grouping/src/main/java/xpadro/java/grouping/model/Person.java" target="_blank">persons</a> who owned a <a href="https://github.com/xpadro/java8/blob/master/grouping/src/main/java/xpadro/java/grouping/model/Pet.java" target="_blank">pet</a>.<br />
<br />
Now we want to know which pets belong to persons living in New York. We are asking for pets, so we can't just make a grouping since we would be returning a collection of persons. What we need to do is group persons by city and then transform the stream to a collection of pets.<br />
<br />
For this purpose, we use mapping on the result of the group by:
<br />
<br />
<script src="https://gist.github.com/xpadro/ffb30b86dfcce01b264f.js"></script>In the grouping phase, we group persons by city and then perform a mapping to get each person's pet.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Grouping, transforming and reducing</span></h3>
<br />
The previous example is useful for converting groupings of objects, but maybe we don’t want to obtain the whole list for each group. In this example, we still want to group pets by its owner’s city, but this time we only want to get the oldest pet of each list.<br />
<br />
The method collectingAndThen from <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html" target="_blank">Collectors</a> allow us to make a final transformation to the result of the grouping:
<br />
<br />
<script src="https://gist.github.com/xpadro/44ba4ad7f8e0139118d0.js"></script>After we group persons by city, in collectingAndThen we are transforming each person in each city’s list to its pet, and then applying a reduction to get the pet with the highest age in the list.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Conclusion</span></h3>
<br />
Collectors API not only allow us to group collections of things but also make transformations and reductions to obtain different objects depending on our needs.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-71831417367644055962016-02-29T08:10:00.000+01:002016-02-29T08:11:35.439+01:00Multi level grouping with streams<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
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 from simple single level groupings to more complex, involving several levels of groupings.<br />
<br />
We will use two classes to represent the objects we want to group by: person and pet.<br />
<br />
<b>Person.class
</b><br />
<br />
<script src="https://gist.github.com/xpadro/bc3873485c451e66f85d.js"></script>
<br />
<b>Pet.class
</b><br />
<br />
<script src="https://gist.github.com/xpadro/44a980b15de57a485672.js"></script>
<br />
In the main method we create the collection we will use in the following sections.<br />
<br />
<script src="https://gist.github.com/xpadro/c67b7bb5f0a23f14b9bd.js"></script>
<br />
You can take a look at the source code <a href="https://github.com/xpadro/java8" target="_blank">here</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Single level grouping</span></h3>
<br />
The simplest form of grouping is the single level grouping. In this example we are going to group all persons in the collection by their country:
<br />
<br />
<script src="https://gist.github.com/xpadro/a2d964dcff901985b8a4.js"></script>
If we take a look into the map, we can see how each country contains a list of its citizens:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTa7qnxm0f-a4S7Mvg-8_d-KHWCDmzIUo3rg1UIUp8pIsCE8_IRbXBO06c_QS8gHQ_lRMh1v1-tWER9C1Ee1Qk45HB69ETB2Ekb5auoZZOj58N4cPu53zDuk0cRSUk1ppHjo_G9jnhKok/s1600/singleGroup.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTa7qnxm0f-a4S7Mvg-8_d-KHWCDmzIUo3rg1UIUp8pIsCE8_IRbXBO06c_QS8gHQ_lRMh1v1-tWER9C1Ee1Qk45HB69ETB2Ekb5auoZZOj58N4cPu53zDuk0cRSUk1ppHjo_G9jnhKok/s1600/singleGroup.png" /></a></div>
<br />
<br />
The result shows persons living in the specified country:<br />
<br />
<i><span style="font-size: x-small;">Persons in USA: [Person{name='John', country='USA', city='New York'}, Person{name='Anna', country='USA', city='New York'}, Person{name='Mike', country='USA', city='Chicago'}]
</span></i><br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Two level grouping</span></h3>
<br />
In this example, we will group not only by country but also by city. To accomplish this, we need to implement a two level grouping. We will group persons by country and for each country, we will group its persons by the city where they live.<br />
<br />
In order to allow multi level grouping, the groupingBy method in class <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html" target="_blank">Collectors</a> supports an additional Collector as a second argument:<br />
<br />
<script src="https://gist.github.com/xpadro/8d7b020d3e16c2b77487.js"></script>
Let’s use this method to implement our two level grouping:
<br />
<br />
<script src="https://gist.github.com/xpadro/19f96c94f035df79214e.js"></script>
If we debug the execution, we will see how people is distributed:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOOksDF-xk0VT5WNPmGysfu7iye8qE19eKOhFQae1NCe7gm8r0XXevi3f2rL4y4E4BgRUWTWAv8w3syc3l7ryuC6iq3qacnIdDzwb1oyDBQaJFXbipbbt4ufrdzZ2O4Y90OuNZpLEpWf0/s1600/twoLevelGrouping.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOOksDF-xk0VT5WNPmGysfu7iye8qE19eKOhFQae1NCe7gm8r0XXevi3f2rL4y4E4BgRUWTWAv8w3syc3l7ryuC6iq3qacnIdDzwb1oyDBQaJFXbipbbt4ufrdzZ2O4Y90OuNZpLEpWf0/s1600/twoLevelGrouping.png" /></a></div>
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Three level grouping</span></h3>
<br />
In our final example, we will take a step further and group people by country, city and pet name. I have splitted it into two methods for readability:
<br />
<br />
<script src="https://gist.github.com/xpadro/1bf772cca311bef974cc.js"></script>
Now we have three nested maps containing each list of persons:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0mvS9eOFOK5G98FiPYK77yA-bh7o8D2GGghLf7Zolp1HThBYPa291r118tcjepjZfvO7O404MD7B4hUfYMW0GBF3cvvEeHsSQn-nwXuZTx4DtqHqwDE608YHUEnK3MtOS9vXwZtxg6Ys/s1600/threeLevelGrouping.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0mvS9eOFOK5G98FiPYK77yA-bh7o8D2GGghLf7Zolp1HThBYPa291r118tcjepjZfvO7O404MD7B4hUfYMW0GBF3cvvEeHsSQn-nwXuZTx4DtqHqwDE608YHUEnK3MtOS9vXwZtxg6Ys/s1600/threeLevelGrouping.png" /></a></div>
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion</span></h3>
<br />
The Java 8 Collectors API provides us with an easy way to group our collections. By nesting collectors, we can add different layers of groups to implement multi level groupings.
<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-42266079674325294772015-07-13T13:52:00.000+02:002015-07-13T13:52:34.993+02:00Understanding Callable and Spring DeferredResult<h3>
<span style="color: #3d85c6;">1- Introduction</span></h3>
<br />
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.<br />
<br />
This topic has been explained many times, but there seems to be a little bit of confusion regarding those classes provided by the Spring framework which take advantage of this functionality. I am talking about returning Callable and DeferredResult from a @Controller.<br />
<br />
In this post I will implement both examples in order to show its differences.<br />
<br />
All the examples shown here consist on implementing a controller which will execute a long running task, and then return the result to the client. The long running task is processed by the TaskService:
<br />
<br />
<script src="https://gist.github.com/xpadro/b7e387b67802b9aa8e98.js"></script>
The web application is built with <a href="http://projects.spring.io/spring-boot/" target="_blank">Spring Boot</a>. We will be executing the following class to run our examples:
<br />
<br />
<script src="https://gist.github.com/xpadro/8cd0ee2589fcf76ad333.js"></script>
The source code with all these examples can be found at the Github <a href="https://github.com/xpadro/spring-rest/tree/master/async-processing" target="_blank">Spring-Rest repository</a>.
<br />
<br />
<br />
<h3>
<span style="color: #3d85c6;">2- Starting with a blocking controller</span></h3>
<br />
In this example, a request arrives to the controller. The servlet thread won't be released until the long running method is executed and we exit the @RequestMapping annotated method.
<br />
<br />
<script src="https://gist.github.com/xpadro/eede464c443271ab1b91.js"></script>
If we run this example at http://localhost:8080/block, looking at the logs, we can see that the servlet request is not released until the long running task has been processed (5 seconds later):<br />
<br />
<span style="font-size: x-small;">2015-07-12 12:41:11.849 [nio-8080-exec-6] x.s.web.controller.BlockingController : Request received</span><br />
<span style="font-size: x-small;">2015-07-12 12:41:16.851 [nio-8080-exec-6] x.spring.web.service.TaskServiceImpl : Slow task executed</span><br />
<span style="font-size: x-small;">2015-07-12 12:41:16.851 [nio-8080-exec-6] x.s.web.controller.BlockingController : Servlet thread released</span><br />
<br />
<br />
<br />
<h3>
<span style="color: #3d85c6;">3- Returning Callable</span></h3>
<br />
In this example, instead of returning directly the result, we will return a Callable:
<br />
<br />
<script src="https://gist.github.com/xpadro/449b6570308ba9703e3d.js"></script>
Returning Callable implies that Spring MVC will invoke the task defined in the Callable in a different thread. Spring will manage this thread by using a TaskExecutor. Before waiting for the long task to finish, the servlet thread will be released.<br />
<br />
Let's take a look at the logs:<br />
<br />
<span style="font-size: x-small;">2015-07-12 13:07:07.012 [nio-8080-exec-5] x.s.w.c.AsyncCallableController : Request received</span><br />
<span style="font-size: x-small;">2015-07-12 13:07:07.013 [nio-8080-exec-5] x.s.w.c.AsyncCallableController : Servlet thread released</span><br />
<span style="font-size: x-small;">2015-07-12 13:07:12.014 [ MvcAsync2] x.spring.web.service.TaskServiceImpl : Slow task executed</span><br />
<br />
You can see that we have returned from the servlet before the long running task has finished executing. This doesn't mean the client has received a response. The communication with the client is still open waiting for the result, but the thread that received the request has been released and can serve another client's request.
<br />
<br />
<br />
<h3>
<span style="color: #3d85c6;">4- Returning DeferredResult</span></h3>
<br />
First, we need to create a DeferredResult object. This object will be returned by the controller. What we will accomplish is the same with Callable, to release the servlet thread while we process the long running task in another thread.
<br />
<br />
<script src="https://gist.github.com/xpadro/a79e8aed7e26218a97a8.js"></script>
So, what's the difference from Callable? The difference is this time the thread is managed by us. It is our responsibility to set the result of the DeferredResult in a different thread.<br />
<br />
What we have done in this example, is to create an asynchronous task with CompletableFuture. This will create a new thread where our long running task will be executed. Is in this thread where we will set the result.<br />
<br />
From which pool are we retrieving this new thread? By default, the supplyAsync method in CompletableFuture will run the task in the ForkJoin pool. If you want to use a different thread pool, you can pass an executor to the supplyAsync method:
<br />
<br />
<script src="https://gist.github.com/xpadro/7e8f733e9bb5640a01ed.js"></script>
If we run this example, we will get the same result as with Callable:<br />
<br />
<span style="font-size: x-small;">2015-07-12 13:28:08.433 [io-8080-exec-10] x.s.w.c.AsyncDeferredController : Request received</span><br />
<span style="font-size: x-small;">2015-07-12 13:28:08.475 [io-8080-exec-10] x.s.w.c.AsyncDeferredController : Servlet thread released</span><br />
<span style="font-size: x-small;">2015-07-12 13:28:13.469 [onPool-worker-1] x.spring.web.service.TaskServiceImpl : Slow task executed
</span><br />
<br />
<br />
<h3>
<span style="color: #3d85c6;">5- Conclusion</span></h3>
<br />
At a high level view, Callable and DeferredResult do the same exact thing, which is releasing the container thread and processing the long running task asynchronously in another thread. The difference is in who manages the thread executing the task.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com12tag:blogger.com,1999:blog-6026073423954662509.post-74028294386832982982015-04-14T19:12:00.000+02:002017-11-01T19:47:44.287+01:00Configure a Spring JMS application with Spring Boot and annotation support<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
In previous posts we learned how to configure a project using Spring JMS. If you check the article <a href="http://xpadro.blogspot.com/2013/07/introduction-to-messaging-with-spring.html" target="_blank">introduction to messaging with Spring JMS</a>, you will notice that it is configured using XML. This article will take advantage of the <a href="https://spring.io/blog/2014/04/30/spring-4-1-s-upcoming-jms-improvements" target="_blank">improvements</a> introduced in Spring 4.1 version, and configure a JMS project using Java config only.<br />
<br />
In this example we will also see how easy it can be to configure the project by using <a href="http://projects.spring.io/spring-boot/" target="_blank">Spring Boot</a>.<br />
<br />
Before we get started, just note that as usual, you can take a look at the source code of the project used in the examples below.<br />
<br />
<a href="https://github.com/xpadro/spring-integration/tree/master/spring-jms/jms-boot-javaconfig" target="_blank">See the example project at github</a>.<br />
<br />
Sections:<br />
<ol>
<li>Introduction.</li>
<li>The example application.</li>
<li>Setting up the project.</li>
<li>A simple example with JMS listener.</li>
<li>Sending a response to another queue with @SendTo.</li>
<li>Conclusion.</li>
</ol>
<br />
<h3>
<span style="color: #0b5394;">2 The example application</span></h3>
<br />
The application uses a Client service to send orders to a JMS queue, where a JMS listener will be registered and handle these orders. Once received, the listener will store the order through the Store service:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-oT0qxGEvhHE/VUZmxxmsr3I/AAAAAAAAEsA/JcpUAiSjleI/s1600/20150414_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="186" src="https://1.bp.blogspot.com/-oT0qxGEvhHE/VUZmxxmsr3I/AAAAAAAAEsA/JcpUAiSjleI/s1600/20150414_1.png" width="320" /></a></div>
<br />
<br />
We will use the Order class to create orders:<br />
<br />
<script src="https://gist.github.com/xpadro/77f43508282ea2227173.js"></script>
Before moving on to the first example, we will first explore how the project structure is built.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Setting up the project</span></h3>
<br />
<span style="color: #0b5394;">3.1 Configuring pom.xml</span><br />
<br />
The first thing to do is to define the artifact <i>spring-boot-starter-parent</i> as our parent pom.<br />
<br />
<script src="https://gist.github.com/xpadro/2f853d9c449b2143c0b1.js"></script>
This parent basically sets several Maven defaults and provides the dependency management for the main dependencies that we will use, like the Spring version (which is 4.1.6).<br />
<br />
It is important to note that this parent pom defines the version of many libraries but it does not add any dependency to our project. So don’t worry about getting libraries you won’t use.<br />
<br />
The next step is to set the basic dependencies for Spring Boot:<br />
<br />
<script src="https://gist.github.com/xpadro/03b3122181350cd639fd.js"></script>
In addition to the core Spring libraries, this dependency will bring the auto configuration functionality of Spring Boot. This will allow the framework to try to automatically set up the configuration based on the dependencies you add.<br />
<br />
Finally, we will add the Spring JMS dependency and the ActiveMQ message broker, leaving the whole pom.xml as follows:<br />
<br />
<script src="https://gist.github.com/xpadro/a650b61219a62b2c4c4d.js"></script>
<span style="color: #0b5394;">3.2 Spring Configuration with Java Config</span><br />
<br />
We used @SpringBootApplication instead of the usual @Configuration annotation. This Spring Boot annotation is also annotated with @Configuration. In addition, it sets other configuration like Spring Boot auto configuration:<br />
<br />
<script src="https://gist.github.com/xpadro/a646cd893e3bbe41a671.js"></script>
The configuration class does not need to define any bean. All the configuration is automatically set by Spring Boot. Regarding the connection factory, Spring Boot will detect that I included the ActiveMQ dependency on the classpath and will start and configure an embedded broker.
<br />
<br />
<script src="https://gist.github.com/xpadro/19100f0945c5b1fbfc65.js"></script>If you need to specify a different broker url, you can declare it in the properties. Check <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html#boot-features-activemq" target="_blank">ActiveMQ support</a> section for further detail.<br />
<br />
It is all set now. We will see how to configure a JMS listener in the example in the next section, since it is configured with an annotation.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 A simple example with JMS listener</span></h3>
<br />
<span style="color: #0b5394;">4.1 Sending an order to a JMS queue</span><br />
<br />
The ClientService class is responsible for sending a new order to the JMS queue. In order to accomplish this, it uses a JmsTemplate:<br />
<br />
<script src="https://gist.github.com/xpadro/64d630d8707679663893.js"></script>
Here, we use a JmsTemplate to convert our Order instance and send it to the JMS queue. If you prefer to directly send a message through the <i>send</i> message, you can instead use the new <a href="http://docs.spring.io/autorepo/docs/spring/4.1.6.RELEASE/javadoc-api/org/springframework/jms/core/JmsMessagingTemplate.html" target="_blank">JmsMessagingTemplate</a>. This is preferable since it uses the more standardized <a href="http://docs.spring.io/autorepo/docs/spring/4.1.6.RELEASE/javadoc-api/org/springframework/messaging/Message.html" target="_blank">Message</a> class.<br />
<br />
<br />
<span style="color: #0b5394;">4.2 Receiving an order sent to the JMS queue</span><br />
<br />
Registering a JMS listener to a JMS listener container is as simple as adding the @JmsListener annotation to the method we want to use. This will create a JMS listener container under the covers that will receive messages sent to the specified queue and delegate them to our listener class:<br />
<br />
<script src="https://gist.github.com/xpadro/80425360230c3f2637ee.js"></script>
The StoreService receives the order and saves it to a list of received orders:<br />
<br />
<script src="https://gist.github.com/xpadro/32a5f9367f10132e1f91.js"></script>
<span style="color: #0b5394;">4.3 Testing the application</span><br />
<br />
Now let’s add a test to check if we did everything correctly:<br />
<br />
<script src="https://gist.github.com/xpadro/79054efcf353d4d30310.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Sending a response to another queue with @SendTo</span></h3>
<br />
Another addition to Spring JMS is the @SendTo annotation. This annotation allows a listener to send a message to another queue. For example, the following listener receives an order from the “in.queue” and after storing the order, sends a confirmation to the “out.queue”.<br />
<br />
<script src="https://gist.github.com/xpadro/8840d9da13b9a7610fee.js"></script>
There, we have another listener registered that will process this confirmation id:<br />
<br />
<script src="https://gist.github.com/xpadro/33e36dbc050fb1d0e19a.js"></script>
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
With annotation support, it is now much easier to configure a Spring JMS application, taking advantage of asynchronous message retrieval using annotated JMS listeners.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com10tag:blogger.com,1999:blog-6026073423954662509.post-53729039835991611472015-03-08T19:00:00.000+01:002015-05-03T20:18:45.619+02:00Improving performance: non-blocking processing of streams<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
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.<br />
<br />
As shown in the graphic below, the example application will retrieve information about several clients, group them in a list and then process it to calculate the total amount of its purchases:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYgtIzdjaFLzZHk6N-rBXE1grPdPSOF9NW0visOOP88K1BtyVs5qzp0ozlRGxyy5fozkcQ8Mot_EO_naGSI7aDtWu00Hte_WQpqKZcAWOi15UdWsOKZdEHor9tDNWVYxS3tFOR06Nz_fo/s1600/flow.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYgtIzdjaFLzZHk6N-rBXE1grPdPSOF9NW0visOOP88K1BtyVs5qzp0ozlRGxyy5fozkcQ8Mot_EO_naGSI7aDtWu00Hte_WQpqKZcAWOi15UdWsOKZdEHor9tDNWVYxS3tFOR06Nz_fo/s1600/flow.png" height="292" width="320" /></a></div>
<br />
In this post, we will see different ways of gathering the information and which one is the best in terms of performance.<br />
<br />
This is a Java related post. However, we will use the <a href="http://projects.spring.io/spring-framework/" target="_blank">Spring framework</a> to invoke a <a href="http://en.wikipedia.org/wiki/Representational_state_transfer" target="_blank">RESTful</a> web service.<br />
<br />
Sections:<br />
<ol>
<li>Introduction</li>
<li>Explaining the example</li>
<li>First attempt: Sequential stream</li>
<li>Improving performance: Parallel stream</li>
<li>Non-blocking processing with CompletableFuture</li>
<li>Conclusion</li>
</ol>
<br />
The source code can be found at the <a href="https://github.com/xpadro/java8" target="_blank">Java 8 GitHub repository</a>.<br />
<br />
Additionally, you can access the source code of the web application exposing the RESTful web service at <a href="https://github.com/xpadro/spring-samples/tree/master/spring-rest-simple" target="_blank">this</a> repository.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Explaining the example</span></h3>
In our application, we have a list of 20 ids representing clients we want to retrieve from a web service. After retrieving all the clients, we will look up at what did every client purchase and sum them up to compute what is the total amount of money spent by all the clients.<br />
<br />
There is one problem though, this web service only allows to retrieve one client at each invocation, so we will need to invoke the service twenty times. In addition, the web service is a little bit slow, taking at least two seconds to respond to a request.<br />
<br />
If we take a look at the application implementing the web service, we can see that invocations are handled by the ClientController class:
<br />
<br />
<script src="https://gist.github.com/xpadro/ee96ea70becac5272412.js"></script>
A Thread.sleep is used to simulate the slowness in responding.<br />
<br />
The domain class (Client) contains the information we need; how much money has a client spent:
<br />
<br />
<script src="https://gist.github.com/xpadro/82b9ddf014f37e8c6eb6.js"></script>
<br />
<h3>
<span style="color: #0b5394;">3 First attempt: Sequential stream</span></h3>
In this first example we will sequentially invoke the service to get the information of all twenty clients:<br />
<br />
<script src="https://gist.github.com/xpadro/b67ae0132b6a26a17e11.js"></script>
Output:<br />
<span style="font-size: x-small;">Sequential | Total time: 42284 ms</span><br />
<span style="font-size: x-small;">Total purchases: 20.0</span><br />
<br />
The execution of this program takes 42 seconds approximately. This is too much time. Let’s see if we can improve its performance.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Improving performance: Parallel stream</span></h3>
Java 8 allows us to split a stream into chunks and process each one in a separate thread. What we need to do is simply create the stream in the previous example as a parallel stream.<br />
<br />
You should take into account that each chunk will be executed in its thread asynchronously, so the order in which the chunks are processed must not matter. In our case, we are summing the purchases, so we can do it.<br />
<br />
Let’s try this:<br />
<br />
<script src="https://gist.github.com/xpadro/c0a7f0966835d51b1844.js"></script>
Output:<br />
<span style="font-size: x-small;">Parallel | Total time: 6336 ms</span><br />
<span style="font-size: x-small;">Total purchases: 20.0</span><br />
<br />
Wow, that’s a big improvement! But what does this number come from?<br />
<br />
Parallel streams internally use the ForkJoinPool, which is the pool used by the ForkJoin framework introduced in Java 7. By default, the pool uses as many threads as your machine's processors can handle. My laptop is a quad core that can handle 8 threads (you can check this by invoking Runtime.getRuntime.availableProcessors), so it can make 8 invocations to the web service in parallel. Since we need 20 invocations, it will need at least 3 "rounds":<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW6MkL1S5K-OcY-IaX3jKXAp0W8Vt2uZ3g8sIUIvdXPRkwvD9MXwWygkqhDK5b6oFc5kLLlpzspS-aJ-j1P46-UAvt1vkCx5K5sEQ92HTsnmRta0atiJcslcTGlPgNr3yYP_u4mEYjk74/s1600/invocations.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhW6MkL1S5K-OcY-IaX3jKXAp0W8Vt2uZ3g8sIUIvdXPRkwvD9MXwWygkqhDK5b6oFc5kLLlpzspS-aJ-j1P46-UAvt1vkCx5K5sEQ92HTsnmRta0atiJcslcTGlPgNr3yYP_u4mEYjk74/s1600/invocations.png" height="320" width="263" /></a></div>
<br />
Ok, so from 40 seconds to 6 is quite a good improvement but, can we still improve it further? The answer is yes.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Non-blocking processing with CompletableFuture</span></h3>
Let’s analise the previous solution.<br />
<br />
We send 8 threads invoking each one the web service, but while the service is processing the request (two whole seconds), our processors are doing nothing but waiting (this is a IO operation). Until these requests don’t come back, we won’t be able to send more requests.<br />
<br />
The question is, what if we could send all 20 requests asynchronously, freeing our processors and process each response when is available? This is where CompletableFuture comes to the rescue:<br />
<br />
<script src="https://gist.github.com/xpadro/b4d7bd025990046f6a3d.js"></script>
Output:<br />
<span style="font-size: x-small;">Async with executor | Total time: 2192 ms</span><br />
<span style="font-size: x-small;">Total purchases: 20.0</span><br />
<br />
It took a third of the time spent in the previous example.<br />
<br />
We sent all 20 requests at the same time, so the time spent in IO operations is spent only once. As soon as responses come by, we process them quickly.<br />
<br />
It is important the use of the executor service, set as an optional second parameter of the supplyAsync method. We specified a pool of a hundred threads so we could send 100 requests at the same time. If we don’t specify an executor, the ForkJoin pool will be used by default.<br />
<br />
You can try to remove the executor and you will see the same performance as in the parallel example.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
We have seen that when executing operations that do not involve computing (like IO operations) we can use the CompletableFuture class to take advantage of our processors and improve the performance of our applications.
<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.
Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com2tag:blogger.com,1999:blog-6026073423954662509.post-7473862827120663082015-02-15T19:58:00.000+01:002017-11-01T19:48:44.228+01:00Java Concurrency Tutorial - Locking: Explicit locks<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
In many cases, using implicit locking is enough. Other times, we will need more complex functionalities. In such cases, <i>java.util.concurrent.locks</i> 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.<br />
<br />
The main advantages or improvements over implicit synchronization are:<br />
<br />
<ul>
<li>Separation of locks by read or write.</li>
<li>Some locks allow concurrent access to a shared resource (<a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReadWriteLock.html" target="_blank">ReadWriteLock</a>).</li>
<li>Different ways of acquiring a lock:</li>
<ul>
<li>Blocking: lock()</li>
<li>Non-blocking: tryLock()</li>
<li>Interruptible: lockInterruptibly()</li>
</ul>
</ul>
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Classification of lock objects</span></h3>
<br />
Lock objects implement one of the following two interfaces:<br />
<br />
<ul>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html" target="_blank">Lock</a>: Defines the basic functionalities that a lock object must implement. Basically, this means acquiring and releasing the lock. In contrast to implicit locks, this one allows the acquisition of a lock in a non-blocking or interruptible way (additionally to the blocking way). Main implementations:</li>
<ul>
<li>ReentrantLock</li>
<li>ReadLock (used by ReentrantReadWriteLock)</li>
<li>WriteLock (used by ReentrantReadWriteLock)</li>
</ul>
</ul>
<br />
<ul>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReadWriteLock.html" target="_blank">ReadWriteLock</a>: It keeps a pair of locks, one for read-only operations and another one for writing. The read lock can be acquired simultaneously by different reader threads (as long as the resource isn’t already acquired by a write lock), while the write lock is exclusive. In this way, we can have several threads reading the resource concurrently as long as there is not a writing operation. Main implementations:</li>
<ul>
<li>ReentrantReadWriteLock</li>
</ul>
</ul>
<br />
The following class diagram shows the relation among the different lock classes:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB0lvCcr2YJLmZaONPV6EguULwUNOTXc3EpNuxpUIUb6q2Qfkcvg1omyyt3aRxPst3qtPHT9MfT41Vt2Xx21VfCXd_bTNIYJiSCeUEWYMgSeZJkaTNgnJs2woCPfNi3gbao4Vi2nUs3sM/s1600/explicitLocking.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB0lvCcr2YJLmZaONPV6EguULwUNOTXc3EpNuxpUIUb6q2Qfkcvg1omyyt3aRxPst3qtPHT9MfT41Vt2Xx21VfCXd_bTNIYJiSCeUEWYMgSeZJkaTNgnJs2woCPfNi3gbao4Vi2nUs3sM/s1600/explicitLocking.png" width="400" /></a></div>
<br />
<br />
<h3>
<span style="color: #0b5394;">3 ReentrantLock</span></h3>
<br />
This lock works the same way as the synchronized block; one thread acquires the lock as long as it is not already acquired by another thread, and it does not release it until unlock is invoked. If the lock is already acquired by another thread, then the thread trying to acquire it becomes blocked until the other thread releases it.<br />
<br />
We are going to start with a simple example without locking, and then we will add a reentrant lock to see how it works.<br />
<br />
<script src="https://gist.github.com/xpadro/8d7a917054496a42ba88.js"></script>
Since the code above is not synchronized, threads will be interleaved. Let’s see the output:<br />
<br />
<span style="font-size: x-small;">Thread-2 - 1</span><br />
<span style="font-size: x-small;">Thread-1 - 1</span><br />
<span style="font-size: x-small;">Thread-1 - 2</span><br />
<span style="font-size: x-small;">Thread-1 - 3</span><br />
<span style="font-size: x-small;">Thread-2 - 2</span><br />
<span style="font-size: x-small;">Thread-2 - 3</span><br />
<br />
Now, we will add a reentrant lock in order to serialize the access to the run method:<br />
<br />
<script src="https://gist.github.com/xpadro/7084f40084b179b22862.js"></script>
The above code will safely be executed without threads being interleaved. You may realize that we could have used a synchronized block and the effect would be the same. The question that arises now is what advantages does the reentrant lock provides us?<br />
<br />
The main advantages of using this type of lock are described below:<br />
<br />
<ul>
<li>Additional ways of acquiring the lock are provided by implementing Lock interface:</li>
<ul>
<li><b>lockInterruptibly</b>: The current thread will try to acquire de lock and become blocked if another thread owns the lock, like with the lock() method. However, if another thread interrupts the current thread, the acquisition will be cancelled.</li>
<li><b>tryLock</b>: It will try to acquire the lock and return immediately, regardless of the lock status. This will prevent the current thread from being blocked if the lock is already acquired by another thread. You can also set the time the current thread will wait before returning (we will see an example of this).</li>
<li><b>newCondition</b>: Allows the thread which owns the lock to wait for a specified condition.</li>
</ul>
<br />
<li>Additional methods provided by the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html" target="_blank">ReentrantLock</a> class, primarily for monitoring or testing. For example, <i>getHoldCount </i>or <i>isHeldByCurrentThread </i>methods.</li>
</ul>
<br />
<br />
Let’s look at an example using tryLock before moving on to the next lock class.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3.1 Trying lock acquisition</span></h3>
<br />
In the following example, we have got two threads, trying to acquire the same two locks.<br />
<br />
One thread acquires <i>lock2 </i>and then it blocks trying to acquire <i>lock1</i>:<br />
<br />
<script src="https://gist.github.com/xpadro/8628c345e84ce417617d.js"></script>
Another thread, acquires <i>lock1 </i>and then it tries to acquire <i>lock2</i>.<br />
<br />
<script src="https://gist.github.com/xpadro/b42123f4910801bdb33e.js"></script>
Using the standard lock method, this would cause a dead lock, since each thread would be waiting forever for the other to release the lock. However, this time we are trying to acquire it with <i>tryLock </i>specifying a timeout. If it doesn’t succeed after four seconds, it will cancel the action and release the first lock. This will allow the other thread to unblock and acquire both locks.<br />
<br />
Let’s see the <a href="https://github.com/xpadro/concurrency/blob/master/synchronization.explicit/src/main/java/xpadro/java/concurrency/TryLock.java">full</a> example:<br />
<br />
<script src="https://gist.github.com/xpadro/7eaa5ac3187d6de519ba.js"></script>
If we execute the code it will result in the following output:<br />
<br />
<span style="font-size: x-small;">13:06:38,654|Thread-2|Trying to acquire lock2...</span><br />
<span style="font-size: x-small;">13:06:38,654|Thread-1|Trying to acquire lock1...</span><br />
<span style="font-size: x-small;">13:06:38,655|Thread-2|Lock2 acquired. Trying to acquire lock1...</span><br />
<span style="font-size: x-small;">13:06:38,655|Thread-1|Lock1 acquired. Trying to acquire lock2...</span><br />
<span style="font-size: x-small;">13:06:42,658|Thread-1|Failed acquiring lock2. Releasing lock1</span><br />
<span style="font-size: x-small;">13:06:42,658|Thread-2|Both locks acquired</span><br />
<br />
After the fourth line, each thread has acquired one lock and is blocked trying to acquire the other lock. At the next line, you can notice the four second lapse. Since we reached the timeout, the first thread fails to acquire the lock and releases the one it had already acquired, allowing the second thread to continue.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 ReentrantReadWriteLock</span></h3>
<br />
This type of lock keeps a pair of internal locks (a <i>ReadLock </i>and a <i>WriteLock</i>). As explained with the interface, this lock allows several threads to read from the resource concurrently. This is specially convenient when having a resource that has frequent reads but few writes. As long as there isn’t a thread that needs to write, the resource will be concurrently accessed.<br />
<br />
The following example shows three threads concurrently reading from a shared resource. When a fourth thread needs to write, it will exclusively lock the resource, preventing reading threads from accessing it while it is writing. Once the write finishes and the lock is released, all reader threads will continue to access the resource concurrently:<br />
<br />
<script src="https://gist.github.com/xpadro/55f62d8611f2398d8f99.js"></script>
The console output shows the result:<br />
<br />
<span style="font-size: x-small;">11:55:01,632|pool-1-thread-1|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:01,632|pool-1-thread-2|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:01,632|pool-1-thread-3|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:04,633|pool-1-thread-3|Reading data: default value</span><br />
<span style="font-size: x-small;">11:55:04,633|pool-1-thread-1|Reading data: default value</span><br />
<span style="font-size: x-small;">11:55:04,633|pool-1-thread-2|Reading data: default value</span><br />
<span style="font-size: x-small;">11:55:04,634|pool-1-thread-4|Write lock acquired</span><br />
<span style="font-size: x-small;">11:55:07,634|pool-1-thread-4|Writing data: changed value</span><br />
<span style="font-size: x-small;">11:55:07,634|pool-1-thread-3|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:07,635|pool-1-thread-1|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:07,635|pool-1-thread-2|Read lock acquired</span><br />
<span style="font-size: x-small;">11:55:10,636|pool-1-thread-3|Reading data: changed value</span><br />
<span style="font-size: x-small;">11:55:10,636|pool-1-thread-1|Reading data: changed value</span><br />
<span style="font-size: x-small;">11:55:10,636|pool-1-thread-2|Reading data: changed value</span><br />
<br />
As you can see, when writer thread acquires the write lock (thread-4), no other threads can access the resource.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion</span></h3>
<br />
This post shows which are the main implementations of explicit locks and explains some of its improved features with respect to implicit locking.<br />
<br />
This post is part of the Java Concurrency Tutorial series. Check <a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial.html" target="_blank">here</a> to read the rest of the tutorial.<br />
<div>
<br /></div>
You can find the source code at <a href="https://github.com/xpadro/concurrency/tree/master/synchronization.explicit" target="_blank">Github</a>.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-82562223170590752022014-12-22T08:29:00.000+01:002017-11-01T19:49:26.008+01:00Exposing HTTP Restful API with Inbound Adapters. Part 2 (Java DSL)<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
In the <a href="http://xpadro.blogspot.com/2014/12/exposing-http-restful-api-with-inbound.html">previous part</a> of this tutorial, we implemented an application exposing a Restful API using XML configuration. This part will re-implement this application using <a href="https://github.com/spring-projects/spring-integration-java-dsl/wiki/Spring-Integration-Java-DSL-Reference">Spring Integration Java DSL</a>.<br />
<br />
The application is implemented with Java 8, but when Java 8 specific code is used (for example, when using lambdas), I will also show you how to do it in Java 7. Anyway, I shared both versions at Github in case you want to check it out:<br />
<br />
<a href="https://github.com/xpadro/spring-integration/tree/master/http/http-dsl7">Java 7 Java DSL example</a><br />
<br />
<a href="https://github.com/xpadro/spring-integration/tree/master/http/http-dsl">Java 8 Java DSL example</a><br />
<br />
<br />
This post is divided into the following sections<br />
<ol>
<li>Introduction</li>
<li>Application configuration</li>
<li>Get operation</li>
<li>Put and post operations</li>
<li>Delete operation</li>
<li>Conclusion</li>
</ol>
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Application configuration</span></h3>
<br />
In the <a href="https://github.com/xpadro/spring-integration/blob/master/int-http-dsl/src/main/webapp/WEB-INF/web.xml">web.xml</a> file, the dispatcher servlet is configured to use Java Config:<br />
<br />
<script src="https://gist.github.com/xpadro/05c54a0496c065942964.js"></script>
In the <a href="https://github.com/xpadro/spring-integration/blob/master/int-http-dsl/pom.xml">pom.xml</a> file, we include the Spring Integration Java DSL dependency:<br />
<br />
<script src="https://gist.github.com/xpadro/db23da89875da5865672.js"></script>
<u>InfrastructureConfiguration.java</u><br />
<br />
The configuration class contains bean and flow definitions.
<br />
<br />
<script src="https://gist.github.com/xpadro/5fab6470133a535e347b.js"></script>
In order to parse payload expressions, we define a bean parser, using an <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/expression/spel/standard/SpelExpressionParser.html">SpELExpressionParser</a>.<br />
<br />
The header mapper will later be registered as a property of inbound gateways, in order to map HTTP headers from/to message headers.<br />
<br />
The detail of the flows and endpoints defined in this configuration class is explained in each of the following sections.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Get operation</span></h3>
<br />
Our first step is to define the HTTP inbound gateway that will handle GET requests.<br />
<br />
<script src="https://gist.github.com/xpadro/d31966e2176f6ebd3700.js"></script>
The createMapping method is the Java alternative to the request-mapping XML element seen in the previous part of the tutorial. In this case, we can also use it to define the request path and supported methods.<br />
<br />
Now that we have our gateway set, let’s define the flow that will serve GET requests (remember you can check a diagram of the full flow in the <a href="http://xpadro.blogspot.com/2014/12/exposing-http-restful-api-with-inbound.html">previous part</a> of the tutorial):<br />
<br />
<script src="https://gist.github.com/xpadro/2241fcebac32c66b6525.js"></script>
The flow works as follows:
<br />
<ul>
<li><b>from(httpGetGate())</b>: Get messages received by the HTTP Inbound Gateway.</li>
<li><b>channel(“httpGetChannel”)</b>: Register a new <a href="http://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html">DirectChannel </a>bean and send the message received to it.</li>
<li><b>handle(“personEndpoint”, “get”)</b>: Messages sent to the previous channel will be consumed by our personEndpoint bean, invoking its get method.</li>
</ul>
<br />
Since we are using a gateway, the response of the personEndpoint will be sent back to the client.<br />
<br />
I am showing the personEndpoint for convenience, since it’s actually the same as in the XML application:<br />
<br />
<script src="https://gist.github.com/xpadro/ffab36da0a7f56ae863b.js"></script>
GetOperationsTest uses a <a href="http://spring.io/guides/gs/consuming-rest/">RestTemplate </a>to test the exposed HTTP GET integration flow:<br />
<br />
<script src="https://gist.github.com/xpadro/b5f27eb6eabeaf079c8c.js"></script>
I won’t show the full class since it is the same as in the XML example.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Put and post operations</span></h3>
<br />
Continuing with our Restful API application example, we define a bean for the HTTP inbound channel adapter. You may notice that we are creating a new Gateway. The reason is that inbound channel adapter is internally implemented as a gateway that is not expecting a reply.<br />
<br />
<script src="https://gist.github.com/xpadro/2d503f295fcb755c43c6.js"></script>
We are again using the parser to resolve the returned status code expression.<br />
<br />
The former XML attribute request-payload-type of the inbound adapter is now set as a property of the gateway.<br />
<br />
The flow that handles both PUT and POST operations uses a <a href="https://github.com/spring-projects/spring-integration-java-dsl/wiki/Spring-Integration-Java-DSL-Reference#routers">router </a>to send the message to the appropriate endpoint, depending on the HTTP method received:<br />
<br />
<script src="https://gist.github.com/xpadro/5283b67c5b4a936a4a23.js"></script>
The flow is executed the following way:<br />
<ul>
<li><b>from(httpPostPutGate())</b>:Get messages received by the HTTP Inbound adapter.</li>
<li><b>channel(“routeRequest”)</b>: Register a DirectChannel bean and send the message received to it.</li>
<li><b>route(...)</b>: Messages sent to the previous channel will be handled by a router, which will redirect them based on the HTTP method received (http_requestMethod header). The destination channel is resolved applying the prefix and suffix. For example, if the HTTP method is PUT, the resolved channel will be httpPutChannel, which is a bean also defined in this configuration class.</li>
</ul>
<br />
Subflows (httpPutFlow and httpPostFlow) will receive messages from the router and handle them in our personEndpoint.<br />
<br />
<script src="https://gist.github.com/xpadro/9a821142a4ad95428eb6.js"></script>
Since we defined an inbound adapter, no response from the endpoint is expected.<br />
<br />
In the router definition we used Java 8 lambdas. I told you I would show the alternative in Java 7, so a promise is a promise:<br />
<br />
<script src="https://gist.github.com/xpadro/9beae2e36050412b8850.js"></script>
A little bit longer, isn’t it?<br />
<br />
The PUT flow is tested by the PutOperationsTest class:<br />
<br />
<script src="https://gist.github.com/xpadro/08e2cdc90671d64a158a.js"></script>
The POST flow is tested by the PostOperationsTest class:<br />
<br />
<script src="https://gist.github.com/xpadro/28a459c8339093904cf2.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Delete operation</span></h3>
<br />
With this operation we complete our application. The entry point is defined by the following bean:<br />
<br />
<script src="https://gist.github.com/xpadro/74bdf2cccb31f76e85e6.js"></script>
The configuration is pretty similar to the PutPost gateway. I won’t explain it again.<br />
<br />
The delete flow sends the deletion request to the personEndpoint:<br />
<br />
<script src="https://gist.github.com/xpadro/91c5302ddd6406544710.js"></script>
And our bean will request the service to delete the resource:<br />
<br />
<script src="https://gist.github.com/xpadro/e6e74cba6171f5be3da2.js"></script>
The test asserts that the resource no longer exists after deletion:<br />
<br />
<script src="https://gist.github.com/xpadro/05743d0cf0b6de8c67ba.js"></script>
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
This second part of the tutorial has shown us how to implement a Spring Integration application with no XML configuration, using the new Spring Integration Java DSL. Although flow configuration is more readable using Java 8 lambdas, we still have the option to use Java DSL with previous versions of the language.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com1tag:blogger.com,1999:blog-6026073423954662509.post-34714629306490765222014-12-22T08:28:00.000+01:002017-11-01T19:50:02.139+01:00Exposing HTTP Restful API with Inbound Adapters. Part 1 (XML)<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
The purpose of this post is to implement an HTTP Restful API using Spring Integration <a href="http://docs.spring.io/spring-integration/reference/html/http.html" target="_blank">HTTP</a> inbound adapters. This tutorial is divided into two parts:<br />
<br />
<ul>
<li>XML configuration example (this same post).</li>
<li>Java DSL example. This will be explained in the next part of this tutorial, showing how to configure the application using <a href="https://github.com/spring-projects/spring-integration-java-dsl/wiki/Spring-Integration-Java-DSL-Reference" target="_blank">Spring Integration Java DSL</a>, with examples with both Java 7 and Java 8.</li>
</ul>
<br />
Before looking at the code, let’s take a glance at the following diagram, which shows the different services exposed by the application:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiohk3O6IdawVoJZp9_-HJMRdk4egjVUwJJYiyBGTTfCOBsUppRgN_AwsuZ_KG1TiYFTErnLOYW1WJX9KfdCISVyYOcoSHesogM6Imb9Q2jOjPkX7gTrLlbvkR9Xv3HcMDUuM6Ffd3LKU/s1600/diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="498" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiohk3O6IdawVoJZp9_-HJMRdk4egjVUwJJYiyBGTTfCOBsUppRgN_AwsuZ_KG1TiYFTErnLOYW1WJX9KfdCISVyYOcoSHesogM6Imb9Q2jOjPkX7gTrLlbvkR9Xv3HcMDUuM6Ffd3LKU/s1600/diagram.png" width="640" /></a></div>
<br />
GET operations are handled by an HTTP inbound gateway, while the rest (PUT, POST and DELETE) are handled by HTTP inbound channel adapters, since no response body is sent back to the client. Each operation will be explained in the following sections:
<br />
<ol>
<li>Introduction</li>
<li>Application configuration</li>
<li>Get operation</li>
<li>Put and post operations</li>
<li>Delete operation</li>
<li>Conclusion</li>
</ol>
The source code is available at <a href="https://github.com/xpadro/spring-integration/tree/master/http/http-xml" target="_blank">Github</a>.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Application configuration</span></h3>
<br />
The <a href="https://github.com/xpadro/spring-integration/blob/master/int-http-xml/src/main/webapp/WEB-INF/web.xml">web.xml</a> file contains the definition of the Dispatcher Servlet:
<br />
<br />
<script src="https://gist.github.com/xpadro/8d4989dc356432df5831.js"></script>
The <a href="https://github.com/xpadro/spring-integration/blob/master/int-http-xml/src/main/resources/xpadro/spring/integration/configuration/http-inbound-config.xml">http-inbound-config.xml</a> file will be explained in the following sections.<br />
<br />
The <a href="https://github.com/xpadro/spring-integration/blob/master/int-http-xml/pom.xml">pom.xml</a> file is detailed below. It is important to note the jackson libraries. Since we will be using JSON to represent our resources, these libraries must be present in the class path. Otherwise, the framework won’t register the required converter.<br />
<br />
<script src="https://gist.github.com/xpadro/897018ee8125a50247c2.js"></script>
<br />
<h3>
<span style="color: #0b5394;">3 Get operation</span></h3>
<br />
The configuration of the flow is shown below:<br />
<br />
<u>http-inbound-config.xml</u><br />
<br />
The gateway receives requests to this path: /persons/{personId}. Once a request has arrived, a message is created and sent to httpGetChannel channel. The gateway will then wait for a <a href="http://www.eaipatterns.com/MessagingAdapter.html" target="_blank">service activator</a> (personEndpoint) to return a response:
<br />
<br />
<script src="https://gist.github.com/xpadro/a8a556409c316b4dbe97.js"></script>
Now, some points need to be explained:<br />
<br />
<ul>
<li><b>supported-methods</b>: this attribute indicates which methods are supported by the gateway (only GET requests).</li>
<li><b>payload-expression</b>: What we are doing here is getting the value from personId variable in the URI template and putting it in the message’s payload. For example, the request path ‘/persons/3’ will become a Message with a value ‘3’ as its payload.</li>
<li><b>request-mapping</b>: We can include this element to specify several attributes and filter which requests will be mapped to the gateway. In the example, only requests that contain the value ‘application/json’ for Content-Type header (consumes attribute) and Accept header (produces attribute) will be handled by this gateway.</li>
</ul>
<br />
Once a request is mapped to this gateway, a message is built and sent to the service activator. In the example, we defined a simple bean that will get the required information from a service:
<br />
<br />
<script src="https://gist.github.com/xpadro/4c0210f7640423961882.js"></script>
Depending on the response received from the service, we will return the requested person or a status code indicating that no person was found.<br />
<br />
Now we will test that everything works as expected. First, we define a ClientPerson class to which the response will be converted:
<br />
<br />
<script src="https://gist.github.com/xpadro/3b59d355a6cbdaf887d6.js"></script>
Then we implement the test. The buildHeaders method is where we specify Accept and Content-Type headers. Remember that we restricted requests with ‘application/json’ values in those headers.
<br />
<br />
<script src="https://gist.github.com/xpadro/c69979bc5c3e954148c6.js"></script>
Not specifying a correct value in the Content-Type header will result in a 415 Unsupported Media Type error, since the gateway does not support this media type.<br />
<br />
On the other hand, specifying an incorrect value in the Accept header will result in a 406 Not Acceptable error, since the gateway is returning another type of content than the expected.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Put and post operations</span></h3>
<br />
For PUT and POST operations, we are using the same HTTP inbound channel adapter, taking advantage of the possibility to define several paths and methods to it. Once a request arrives, a router will be responsible to delivering the message to the correct endpoint.<br />
<br />
<u>http-inbound-config.xml
</u><br />
<br />
<script src="https://gist.github.com/xpadro/bcbf4e87d95219a5b1f2.js"></script>
This channel adapter includes two new attributes:<br />
<br />
<ul>
<li><b>status-code-expression</b>: By default, the channel adapter acknowledges that the request has been received and returns a 200 status code. If we want to override this behavior, we can specify a different status code in this attribute. Here, we specify that these operations will return a 204 No Content status code.</li>
<li><b>request-payload-type</b>: This attribute specifies what class will the request body be converted to. If we do not define it, it will not be able to convert to the class that the service activator is expecting (ServerPerson).</li>
</ul>
<br />
<br />
When a request is received, the adapter sends it to the routeRequest channel, where a router is expecting it. This router will inspect the message headers and depending on the value of the ‘http_requestMethod’ header, it will deliver it to the appropriate endpoint.<br />
<br />
Both PUT and POST operations are handled by the same bean:
<br />
<br />
<script src="https://gist.github.com/xpadro/8e229d145e8f7f8f306e.js"></script>
Return type is void because no response is expected; the inbound adapter will handle the return of the status code.<br />
<br />
<a href="https://github.com/xpadro/spring-integration/blob/master/int-http-xml/src/test/java/xpadro/spring/integration/test/PutOperationsTest.java">PutOperationsTest</a> validates that the correct status code is returned and that the resource has been updated:
<br />
<br />
<script src="https://gist.github.com/xpadro/87bb11d2e4b542ca618f.js"></script><a href="https://github.com/xpadro/spring-integration/blob/master/int-http-xml/src/test/java/xpadro/spring/integration/test/PostOperationsTest.java">PostOperationsTest</a> validates that the new resource has been added:
<br />
<br />
<script src="https://gist.github.com/xpadro/a1cc0b9e5769bad7b9bb.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Delete operation</span></h3>
<br />
The last operation of our restful API is the delete operation. This time we use a single channel adapter for this purpose:
<br />
<br />
<script src="https://gist.github.com/xpadro/34c71f1246c58595400d.js"></script>
The channel adapter lets us define the returning status code and we are using the payload-expression attribute to map the requested personId to the message body. The configuration is a little bit different from those in previous operations but there’s nothing not already explained here.<br />
<br />
The service activator, our person endpoint, will request the person service to delete this resource.
<br />
<br />
<script src="https://gist.github.com/xpadro/b17fcb662d66f439fb57.js"></script>Finally, the required test:
<br />
<br />
<script src="https://gist.github.com/xpadro/7f76e9faa6deab18b9d8.js"></script>
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
This post has been an introduction to our application in order to understand how it is structured from a known point of view (xml configuration). In the next part of this tutorial, we are going to implement this same application using Java DSL. The application will be configured to run with Java 8, but when lambdas are used, I will also show how it can be done with Java 7.<br />
<br />
You can read the second part of this tutorial <a href="http://xpadro.blogsopt.com/2014/12/exposing-http-restful-api-with-inbound_22.html">here</a>.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-76613703673746339912014-09-01T17:08:00.000+02:002017-11-01T19:51:11.158+01:00Java Concurrency Tutorial - Locking: Intrinsic locksIn previous posts we reviewed some of the main risks of sharing data between different threads (like <a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-atomicity-and.html" target="_blank">atomicity</a> and <a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-visibility.html" target="_blank">visibility</a>) and how to design classes in order to be shared safely (<a href="http://xpadro.blogsopt.com/2014/08/java-concurrency-tutorial-thread-safe.html" target="_blank">thread-safe designs</a>). In many situations though, we will need to share mutable data, where some threads will write and others will act as readers. It may be the case that you only have one field, independent to others, that needs to be shared between different threads. In this case, you may go with atomic variables. For more complex situations you will need synchronization.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">1 The coffee store example</span></h3>
<br />
Let’s start with a simple example like a <a href="https://github.com/xpadro/concurrency/blob/master/synchronization.intrinsic/src/main/java/intrinsic/CoffeeStore.java" target="_blank">CoffeeStore</a>. This class implements a store where clients can buy coffee. When a client buys coffee, a counter is increased in order to keep track of the number of units sold. The store also registers who was the last client to come to the store.<br />
<br />
<script src="https://gist.github.com/xpadro/10c0a0cfde5243378c37.js"></script>
In the following program, four clients decide to come to the store to get their coffee:<br />
<br />
<script src="https://gist.github.com/xpadro/d15d7e1315b3da12a0d6.js"></script>
The main thread will wait for all four client threads to finish, using Thread.join(). Once the clients have left, we should obviously count four coffees sold in our store, but you may get unexpected results like the one above:<br />
<br />
<span style="font-size: x-small;">Mike bought some coffee</span><br />
<span style="font-size: x-small;">Steve bought some coffee</span><br />
<span style="font-size: x-small;">Anna bought some coffee</span><br />
<span style="font-size: x-small;">John bought some coffee</span><br />
<span style="font-size: x-small;">Sold coffee: 3</span><br />
<span style="font-size: x-small;">Last client: Anna</span><br />
<span style="font-size: x-small;">Total time: 3001 ms</span><br />
<br />
We lost one unit of coffee, and also the last client (John) is not the one displayed (Anna). The reason is that since our code is not synchronized, threads interleaved. Our <i>buyCoffee</i> operation should be made atomic.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 How synchronization works</span></h3>
<br />
A synchronized block is an area of code which is guarded by a lock. When a thread enters a synchronized block, it needs to acquire its lock and once acquired, it won’t release it until exiting the block or throwing an exception. In this way, when another thread tries to enter the synchronized block, it won’t be able to acquire its lock until the owner thread releases it. This is the Java mechanism to ensure that only on thread at a given time is executing a synchronized block of code, ensuring the atomicity of all actions within that block.<br />
<br />
Ok, so you use a lock to guard a synchronized block, but what is a lock? The answer is that any Java object can be used as a lock, which is called intrinsic lock. We will now see some examples of these locks when using synchronization.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Synchronized methods</span></h3>
<br />
Synchronized methods are guarded by two types of locks:<br />
<br />
<ul>
<li><b>Synchronized instance methods</b>: The implicit lock is ‘this’, which is the object used to invoke the method. Each instance of this class will use their own lock.</li>
</ul>
<ul>
<li><b>Synchronized static methods</b>: The lock is the Class object. All instances of this class will use the same lock.</li>
</ul>
<br />
As usual, this is better seen with some code.<br />
<br />
First, we are going to synchronize an instance method. This works as follows: We have one instance of the class shared by two threads (Thread-1 and Thread-2), and another instance used by a third thread (Thread-3):<br />
<br />
<script src="https://gist.github.com/xpadro/052bf927bac52730d437.js"></script>
Since <i>doSomeTask</i> method is synchronized, you would expect that only one thread will execute its code at a given time. But that’s wrong, since it is an instance method; different instances will use a different lock as the output demonstrates:<br />
<br />
<span style="font-size: x-small;">Thread-1 | Entering method. Current Time: 0 ms</span><br />
<span style="font-size: x-small;">Thread-3 | Entering method. Current Time: 1 ms</span><br />
<span style="font-size: x-small;">Thread-3 | Exiting method</span><br />
<span style="font-size: x-small;">Thread-1 | Exiting method</span><br />
<span style="font-size: x-small;">Thread-2 | Entering method. Current Time: 3001 ms</span><br />
<span style="font-size: x-small;">Thread-2 | Exiting method</span><br />
<br />
Since Thread-1 and Thread-3 use a different instance (and hence, a different lock), they both enter the block at the same time. On the other hand, Thread-2 uses the same instance (and lock) as Thread-1. Therefore, it has to wait until Thread-1 releases the lock.<br />
<br />
Now let’s change the method signature and use a static method. <a href="https://github.com/xpadro/concurrency/blob/master/synchronization.intrinsic/src/main/java/intrinsic/StaticMethodExample.java" target="_blank">StaticMethodExample</a> has the same code except the following line:<br />
<br />
<script src="https://gist.github.com/xpadro/6a3cb745eae7142fadd7.js"></script>
If we execute the main method we will get the following output:<br />
<br />
<span style="font-size: x-small;">Thread-1 | Entering method. Current Time: 0 ms</span><br />
<span style="font-size: x-small;">Thread-1 | Exiting method</span><br />
<span style="font-size: x-small;">Thread-3 | Entering method. Current Time: 3001 ms</span><br />
<span style="font-size: x-small;">Thread-3 | Exiting method</span><br />
<span style="font-size: x-small;">Thread-2 | Entering method. Current Time: 6001 ms</span><br />
<span style="font-size: x-small;">Thread-2 | Exiting method</span><br />
<br />
Since the synchronized method is static, it is guarded by the Class object lock. Despite using different instances, all threads will need to acquire the same lock. Hence, any thread will have to wait for the previous thread to release the lock.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Back to the coffee store example</span></h3>
<br />
I have now modified the Coffee Store example in order to synchronize its methods. The result is as follows:<br />
<br />
<script src="https://gist.github.com/xpadro/e766ff8d890ba0d09f9c.js"></script>
Now, if we execute the program, we won’t lose any sale:<br />
<br />
<span style="font-size: x-small;">Mike bought some coffee</span><br />
<span style="font-size: x-small;">Steve bought some coffee</span><br />
<span style="font-size: x-small;">Anna bought some coffee</span><br />
<span style="font-size: x-small;">John bought some coffee</span><br />
<span style="font-size: x-small;">Sold coffee: 4</span><br />
<span style="font-size: x-small;">Last client: John</span><br />
<span style="font-size: x-small;">Total time: 12005 ms</span><br />
<br />
Perfect! Well, it really is? Now the program’s execution time is 12 seconds. You sure have noticed a <i>someLongRunningProcess</i> method executing during each sale. It can be an operation which has nothing to do with the sale, but since we synchronized the whole method, now each thread has to wait for it to execute. Could we leave this code out of the synchronized block? Sure! Have a look at synchronized blocks in the next section.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Synchronized blocks</span></h3>
<br />
The previous section showed us that we may not always need to synchronize the whole method. Since all the synchronized code forces a serialization of all thread executions, we should minimize the length of the synchronized block. In our Coffee store example, we could leave the long running process out of it. In this section’s example, we are going to use synchronized blocks:<br />
<br />
In <a href="https://github.com/xpadro/concurrency/blob/master/synchronization.intrinsic/src/main/java/intrinsic/SynchronizedBlockCoffeeStore.java" target="_blank">SynchronizedBlockCoffeeStore</a>, we modify the <i>buyCoffee</i> method to exclude the long running process outside of the synchronized block:<br />
<br />
<script src="https://gist.github.com/xpadro/bfde3a6d626c731e641a.js"></script>
In the previous synchronized block, we use ‘this’ as its lock. It’s the same lock as in synchronized instance methods. Beware of using another lock, since we are using this lock in other methods of this class (<i>countSoldCoffees</i> and <i>getLastClient</i>).<br />
<br />
Let’s see the result of executing the modified program:<br />
<br />
<span style="font-size: x-small;">Mike bought some coffee</span><br />
<span style="font-size: x-small;">John bought some coffee</span><br />
<span style="font-size: x-small;">Anna bought some coffee</span><br />
<span style="font-size: x-small;">Steve bought some coffee</span><br />
<span style="font-size: x-small;">Sold coffee: 4</span><br />
<span style="font-size: x-small;">Last client: Steve</span><br />
<span style="font-size: x-small;">Total time: 3015 ms</span><br />
<br />
We have significantly reduced the duration of the program while keeping the code synchronized.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">6 Using private locks</span></h3>
<br />
The previous section used a lock on the instance object, but you can use any object as its lock. In this section we are going to use a private lock and see what the risk is of using it.<br />
<br />
In <a href="https://github.com/xpadro/concurrency/blob/master/synchronization.intrinsic/src/main/java/intrinsic/PrivateLockExample.java" target="_blank">PrivateLockExample</a>, we have a synchronized block guarded by a private lock (myLock):<br />
<br />
<script src="https://gist.github.com/xpadro/8567d0c299f836c00cdc.js"></script>
If one thread enters <i>executeTask</i> method will acquire <i>myLock</i> lock. Any other thread entering other methods within this class guarded by the same <i>myLock</i> lock, will have to wait in order to acquire it.<br />
<br />
But now, let’s imagine that someone wants to extend this class in order to add its own methods, and these methods also need to be synchronized because need to use the same shared data. Since the lock is private in the base class, the extended class won’t have access to it. If the extended class synchronizes its methods, they will be guarded by ‘this’. In other words, it will use another lock.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/synchronization.intrinsic/src/main/java/intrinsic/MyPrivateLockExample.java" target="_blank">MyPrivateLockExample</a> extends the previous class and adds its own synchronized method <i>executeAnotherTask</i>:<br />
<br />
<script src="https://gist.github.com/xpadro/4da8dd787cfc023437c9.js"></script>
The program uses two worker threads that will execute <i>executeTask</i> and <i>executeAnotherTask</i> respectively. The output shows how threads are interleaved since they are not using the same lock:<br />
<br />
<span style="font-size: x-small;">executeTask - Entering...</span><br />
<span style="font-size: x-small;">executeAnotherTask - Entering...</span><br />
<span style="font-size: x-small;">executeAnotherTask - Exiting...</span><br />
<span style="font-size: x-small;">executeTask - Exiting...</span><br />
<br />
<br />
<h3>
<span style="color: #0b5394;">7 Conclusion</span></h3>
<br />
We have reviewed the use of intrinsic locks by using Java’s built-in locking mechanism. The main concern here is that synchronized blocks that need to use shared data; have to use the same lock.<br />
<br />
This post is part of the Java Concurrency Tutorial series. Check <a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial.html" target="_blank">here</a> to read the rest of the tutorial.<br />
<br />
You can find the source code at <a href="https://github.com/xpadro/concurrency/tree/master/synchronization.intrinsic" target="_blank">Github</a>.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com1tag:blogger.com,1999:blog-6026073423954662509.post-36645549489524188102014-09-01T17:06:00.000+02:002017-11-01T19:52:20.720+01:00Java Concurrency TutorialThis 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-synchronized programs, and it then continues with more specific features.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">Basics</span></h3>
<br />
<b>
<a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-atomicity-and.html" style="color: #0b5394;" target="_blank">Atomicity and race conditions</a></b><br />
Atomicity is one of the main concerns in concurrent programs. This post shows the effects of executing compound actions in non-synchronized code.<br />
<br />
<b>
<a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-visibility.html" style="color: #0b5394;" target="_blank">Visibility between threads</a></b><br />
Another of the risks of executing code concurrently is how values written by one thread can become visible to other threads accessing the same data.<br />
<br />
<b>
<a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-thread-safe.html" style="color: #0b5394;" target="_blank">Thread-safe designs</a></b><br />
After looking at the main risks of sharing data, this post describes several class designs that can be shared safely between different threads.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">Synchronization</span></h3>
<br />
<b>
<a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial-locking.html" style="color: #0b5394;" target="_blank">Locking - Intrinsic locks</a></b><br />
Intrinsic locks are Java's built-in mechanism for locking in order to ensure that compound actions within a synchronized block are atomic and create a happens-before relationship.<br />
<br />
<b><span style="color: #0b5394;"><a href="http://xpadro.blogspot.com/2015/02/java-concurrency-tutorial-locking.html" target="_blank">Locking - Explicit locks</a></span></b><br />
Explicit locks provide additional features to the Java synchronization mechanism. Here, we take a look at the main implementations and how they work.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-24346574501354983442014-08-25T13:47:00.000+02:002017-11-01T19:53:08.282+01:00Java Concurrency Tutorial - Thread-safe designsAfter reviewing what the main risks are when dealing with concurrent programs (like <a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-atomicity-and.html" target="_blank">atomicity</a> or <a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-visibility.html" target="_blank">visibility</a>), 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 objects, allowing us to share them safely between threads. As an example, we will consider immutable and stateless objects. Other designs will prevent different threads from modifying the same data, like thread-local variables.
<br />
<br />
You can see all the source code at <a href="https://github.com/xpadro/concurrency/tree/master/basic.threadsafety" target="_blank">github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">1 Immutable objects</span></h3>
<br />
Immutable objects have a state (have data which represent the object's state), but it is built upon construction, and once the object is instantiated, the state cannot be modified.<br />
<br />
Although threads may interleave, the object has only one possible state. Since all fields are read-only, not a single thread will be able to change object's data. For this reason, an immutable object is inherently thread-safe.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/basic.threadsafety/src/main/java/threadsafety/Product.java" target="_blank">Product</a> shows an example of an immutable class. It builds all its data during construction and none of its fields are modifiable:<br />
<br />
<script src="https://gist.github.com/xpadro/e3514d1465b076c84a25.js"></script>
In some cases, it won't be sufficient to make a field final. For example, <a href="https://github.com/xpadro/concurrency/blob/master/basic.threadsafety/src/main/java/threadsafety/MutableProduct.java" target="_blank">MutableProduct</a> class is not immutable although all fields are final:<br />
<br />
<script src="https://gist.github.com/xpadro/bc5933efaa7c64e1c111.js"></script>
Why is the above class not immutable? The reason is we let a reference to escape from the scope of its class. The field '<i>categories</i>' is a mutable reference, so after returning it, the client could modify it. In order to show this, consider the following program:<br />
<br />
<script src="https://gist.github.com/xpadro/d6af550155a388c2efb4.js"></script>
And the console output:<br />
<br />
<span style="font-size: x-small;">Product categories</span><br />
<span style="font-size: x-small;">A</span><br />
<span style="font-size: x-small;">B</span><br />
<span style="font-size: x-small;">C</span><br />
<span style="font-size: x-small;"><br /></span>
<span style="font-size: x-small;">Modified Product categories</span><br />
<span style="font-size: x-small;">B</span><br />
<span style="font-size: x-small;">C</span><br />
<br />
Since <i>categories </i>field is mutable and it escaped the object's scope, the client has modified the categories list. The product, which was supposed to be immutable, has been modified, leading to a new state.<br />
<br />
If you want to expose the content of the list, you could use an unmodifiable view of the list:<br />
<br />
<script src="https://gist.github.com/xpadro/af9a3c407aedeb1c0a27.js"></script>
<br />
<h3>
<span style="color: #0b5394;">
2 Stateless objects</span></h3>
<br />
Stateless objects are similar to immutable objects but in this case, they do not have a state, not even one. When an object is stateless it does not have to remember any data between invocations.<br />
<br />
Since there is no state to modify, one thread will not be able to affect the result of another thread invoking the object's operations. For this reason, a stateless class is inherently thread-safe.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/basic.threadsafety/src/main/java/threadsafety/ProductHandler.java" target="_blank">ProductHandler</a> is an example of this type of objects. It contains several operations over Product objects and it does not store any data between invocations. The result of an operation does not depend on previous invocations or any stored data:<br />
<br />
<script src="https://gist.github.com/xpadro/2a92602cb27e0d3b87ac.js"></script>
In its <i>sumCart </i>method, the <i>ProductHandler </i>converts the product list to an array since for-each loop uses an iterator internally to iterate through its elements. List iterators are not thread-safe and could throw a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html" target="_blank">ConcurrentModificationException</a> if modified during iteration. Depending on your needs, you might choose a different <a href="http://stackoverflow.com/questions/4517653/thread-safe-iteration-over-a-collection" target="_blank">strategy</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Thread-local variables</span></h3>
<br />
Thread-local variables are those variables defined within the scope of a thread. No other threads will see nor modify them.<br />
<br />
The first type is local variables. In the below example, the <i>total </i>variable is stored in the thread's stack:<br />
<br />
<script src="https://gist.github.com/xpadro/e27d62fd650c6fb99f9b.js"></script>
Just take into account that if instead of a primitive you define a reference and return it, it will escape its scope. You may not know where the returned reference is stored. The code that calls <i>sumCart </i>method could store it in a static field and allow it being shared between different threads.<br />
<br />
The second type is <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html" target="_blank">ThreadLocal</a> class. This class provides a storage independent for each thread. Values stored into an instance of ThreadLocal are accessible from any code within the same thread.<br />
<br />
The <a href="https://github.com/xpadro/concurrency/blob/master/basic.threadsafety/src/main/java/threadsafety/ClientRequestId.java" target="_blank">ClientRequestId</a> class shows an example of ThreadLocal usage:<br />
<br />
<script src="https://gist.github.com/xpadro/460eca52776f264c4d77.js"></script>
The <a href="https://github.com/xpadro/concurrency/blob/master/basic.threadsafety/src/main/java/threadsafety/ProductHandlerThreadLocal.java" target="_blank">ProductHandlerThreadLocal</a> class uses ClientRequestId to return the same generated id within the same thread:<br />
<br />
<script src="https://gist.github.com/xpadro/21b675171a0662a21dad.js"></script>
If you execute the main method, the console output will show different ids for each thread. As an example:<br />
<br />
<span style="font-size: x-small;">T1 - 23dccaa2-8f34-43ec-bbfa-01cec5df3258</span><br />
<span style="font-size: x-small;">T2 - 936d0d9d-b507-46c0-a264-4b51ac3f527d</span><br />
<span style="font-size: x-small;">T2 - 936d0d9d-b507-46c0-a264-4b51ac3f527d</span><br />
<span style="font-size: x-small;">T3 - 126b8359-3bcc-46b9-859a-d305aff22c7e</span><br />
<span style="font-size: x-small;">...</span><br />
<br />
If you are going to use ThreadLocal, you should care about some of the risks of using it when threads are pooled (like in application servers). You could end up with memory leaks or information leaking between requests. I won't extend myself in this subject since the post <a href="https://plumbr.eu/blog/how-to-shoot-yourself-in-foot-with-threadlocals" target="_blank">How to shoot yourself in foot with ThreadLocals</a> explains well how this can happen.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Using synchronization</span></h3>
<br />
Another way of providing thread-safe access to objects is through synchronization. If we synchronize all accesses to a reference, only a single thread will access it at a given time. We will discuss this on further posts.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion</span></h3>
<br />
We have seen several techniques that help us build simpler objects that can be shared safely between threads. It is much harder to prevent concurrent bugs if an object can have multiple states. On the other hand, if an object can have only one state or none, we won't have to worry about different threads accessing it at the same time.<br />
<br />
This post is part of the Java Concurrency Tutorial series. Check <a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial.html" target="_blank">here</a> to read the rest of the tutorial.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-19769794858107020382014-08-14T12:07:00.000+02:002017-11-01T19:53:42.773+01:00Java Concurrency Tutorial - Visibility between threadsWhen sharing an object’s state between different threads, other issues besides <a href="http://xpadro.blogspot.com/2014/08/java-concurrency-tutorial-atomicity-and.html" target="_blank">atomicity</a> come into play. One of them is visibility.<br />
<br />
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, in a multi-threaded program, it is possible that if one thread updates a value, another thread doesn’t see the update when it needs it or doesn’t see it at all.<br />
<br />
In a multi-threaded environment, it is the program’s responsibility to identify when data is shared between different threads and act in consequence (using synchronization).<br />
<br />
The example in <a href="https://github.com/xpadro/concurrency/blob/master/basic.visibility/src/main/java/visibility/NoVisibility.java" target="_blank">NoVisibility</a> consists in two threads that share a flag. The writer thread updates the flag and the reader thread waits until the flag is set:<br />
<br />
<script src="https://gist.github.com/xpadro/1ff29879114fd50b1f47.js"></script>
This program might result in an infinite loop, since the reader thread may not see the updated flag and wait forever.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-VR-Fz9ocqhk/VUZkPPI8FPI/AAAAAAAAEqw/a9wEOZqLcng/s1600/noVisibility.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="66" src="https://3.bp.blogspot.com/-VR-Fz9ocqhk/VUZkPPI8FPI/AAAAAAAAEqw/a9wEOZqLcng/s1600/noVisibility.png" width="640" /></a></div>
<br />
With synchronization we can guarantee that this reordering doesn’t take place, avoiding the infinite loop. To ensure visibility we have two options:<br />
<ul>
<li>Locking: Guarantees visibility and atomicity (as long as it uses the same lock).</li>
<li>Volatile field: Guarantees visibility.</li>
</ul>
<br />
The volatile keyword acts like some sort of synchronized block. Each time the field is accessed, it will be like entering a synchronized block. The main difference is that it doesn’t use locks. For this reason, it may be suitable for examples like the above one (updating a shared flag) but not when using compound actions.<br />
<br />
We will now modify the previous example by adding the volatile keyword to the ready field.<br />
<br />
<script src="https://gist.github.com/xpadro/14c6234a2344292979ea.js"></script><a href="https://github.com/xpadro/concurrency/blob/master/basic.visibility/src/main/java/visibility/Visibility.java" target="_blank">Visibility</a> will not result in an infinite loop anymore. Updates made by the writer thread will be visible to the reader thread:<br />
<br />
<span style="font-size: x-small;">Writer thread - Changing flag...</span><br />
<span style="font-size: x-small;">Reader Thread - Flag change received. Finishing thread.</span><br />
<br />
<br />
<h3>
<span style="color: #0b5394;">Conclusion</span></h3>
<br />
We learned about another risk when sharing data in multi-threaded programs. For a simple example like the one shown here, we can simply use a volatile field. Other situations will require us to use atomic variables or locking.<br />
<br />
This post is part of the Java Concurrency Tutorial series. Check <a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial.html" target="_blank">here</a> to read the rest of the tutorial.<br />
<br />
You can take a look at the source code at <a href="https://github.com/xpadro/concurrency/tree/master/basic.visibility" target="_blank">github</a>.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-36779903562201447652014-08-11T12:00:00.000+02:002017-11-01T19:54:07.016+01:00Java Concurrency Tutorial - Atomicity and race conditionsAtomicity 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 interference, which means that if two threads execute several steps on the same data, they may overlap.<br />
<br />
The following <a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/Interleaving.java" target="_blank">Interleaving</a> example shows two threads executing several actions (prints in a loop) and how they are overlapped:<br />
<br />
<script src="https://gist.github.com/xpadro/db07449c058b71f7e8be.js"></script>
When executed, it will produce unpredictable results. As an example:<br />
<br />
<span style="font-size: x-small;">Thread 2 - Number: 0</span><br />
<span style="font-size: x-small;">Thread 2 - Number: 1</span><br />
<span style="font-size: x-small;">Thread 2 - Number: 2</span><br />
<span style="font-size: x-small;">Thread 1 - Number: 0</span><br />
<span style="font-size: x-small;">Thread 1 - Number: 1</span><br />
<span style="font-size: x-small;">Thread 1 - Number: 2</span><br />
<span style="font-size: x-small;">Thread 1 - Number: 3</span><br />
<span style="font-size: x-small;">Thread 1 - Number: 4</span><br />
<span style="font-size: x-small;">Thread 2 - Number: 3</span><br />
<span style="font-size: x-small;">Thread 2 - Number: 4</span><br />
<br />
In this case, nothing wrong happens since they are just printing numbers. However, when you need to share the state of an object (its data) without synchronization, this leads to the presence of race conditions.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">Race condition</span></h3>
<br />
Your code will have a race condition if there’s a possibility to produce incorrect results due to thread interleaving. This section describes two types of race conditions:<br />
<br />
<ol>
<li>Check-then-act</li>
<li>Read-modify-write</li>
</ol>
To remove race conditions and enforce thread safety, we must make these actions atomic by using synchronization. Examples in the following sections will show what the effects of these race conditions are.
<br />
<br />
<br />
<h4>
Check-then-act race condition</h4>
<br />
This race condition appears when you have a shared field and expect to serially execute the following steps:<br />
<br />
<ol>
<li>Get a value from a field.</li>
<li>Do something based on the result of the previous check.</li>
</ol>
<br />
<br />
The problem here is that when the first thread is going to act after the previous check, another thread may have interleaved and changed the value of the field. Now, the first thread will act based on a value that is no longer valid. This is easier seen with an example.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/UnsafeCheckThenAct.java" target="_blank">UnsafeCheckThenAct</a> is expected to change the field <i>number</i> once. Following calls to <i>changeNumber </i>method, should result in the execution of the else condition:
<br />
<br />
<script src="https://gist.github.com/xpadro/cdcd8810efa3a13467ef.js"></script>
But since this code is not synchronized, it may (there's no guarantee) result in several modifications of the field:<br />
<br />
<span style="font-size: x-small;">T13 | Changed</span><br />
<span style="font-size: x-small;">T17 | Changed</span><br />
<span style="font-size: x-small;">T35 | Not changed</span><br />
<span style="font-size: x-small;">T10 | Changed</span><br />
<span style="font-size: x-small;">T48 | Not changed</span><br />
<span style="font-size: x-small;">T14 | Changed</span><br />
<span style="font-size: x-small;">T60 | Not changed</span><br />
<span style="font-size: x-small;">T6 | Changed</span><br />
<span style="font-size: x-small;">T5 | Changed</span><br />
<span style="font-size: x-small;">T63 | Not changed</span><br />
<span style="font-size: x-small;">T18 | Not changed</span><br />
<br />
Another example of this race condition is <a href="http://stackoverflow.com/questions/6782660/incorrect-lazy-initialization" target="_blank">lazy initialization</a>.<br />
<br />
A simple way to correct this is to use synchronization.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/SafeCheckThenAct.java" target="_blank">SafeCheckThenAct</a> is thread-safe because it has removed the race condition by synchronizing all accesses to the shared field.
<br />
<br />
<script src="https://gist.github.com/xpadro/44492494525c44a54aa7.js"></script>
Now, executing this code will always produce the same expected result; only a single thread will change the field:<br />
<br />
<span style="font-size: x-small;">T0 | Changed</span><br />
<span style="font-size: x-small;">T54 | Not changed</span><br />
<span style="font-size: x-small;">T53 | Not changed</span><br />
<span style="font-size: x-small;">T62 | Not changed</span><br />
<span style="font-size: x-small;">T52 | Not changed</span><br />
<span style="font-size: x-small;">T51 | Not changed</span><br />
<span style="font-size: x-small;">...</span><br />
<br />
In some cases, there will be other mechanisms which perform better than synchronizing the whole method but I won’t discuss them in this post.
<br />
<br />
<br />
<h4>
Read-modify-write race condition</h4>
<br />
Here we have another type of race condition which appears when executing the following set of actions:<br />
<br />
<ol>
<li>Fetch a value from a field.</li>
<li>Modify the value.</li>
<li>Store the new value to the field.</li>
</ol>
<br />
<br />
In this case, there’s another dangerous possibility which consists in the loss of some updates to the field. One possible outcome is:<br />
<br />
<span style="font-size: x-small;">Field’s value is 1.</span><br />
<span style="font-size: x-small;">Thread 1 gets the value from the field (1).</span><br />
<span style="font-size: x-small;">Thread 1 modifies the value (5).</span><br />
<span style="font-size: x-small;">Thread 2 reads the value from the field (1).</span><br />
<span style="font-size: x-small;">Thread 2 modifies the value (7).</span><br />
<span style="font-size: x-small;">Thread 1 stores the value to the field (5).</span><br />
<span style="font-size: x-small;">Thread 2 stores the value to the field (7).</span><br />
<br />
As you can see, update with the value 5 has been lost.<br />
<br />
Let’s see a code sample. <a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/UnsafeReadModifyWrite.java" target="_blank">UnsafeReadModifyWrite</a> shares a numeric field which is incremented each time:
<br />
<br />
<script src="https://gist.github.com/xpadro/34bd3af734bf331dbe40.js"></script>
Can you spot the compound action
which causes the race condition?<br />
<br />
I’m sure you did, but for completeness, I will explain it anyway. The problem is in the increment (<i>number++</i>). This may appear to be a single action but in fact, it is a sequence of three actions (get-increment-write).<br />
<br />
When executing this code, we may see that we have lost some updates:<br />
<br />
<span style="font-size: x-small;">2014-08-08 09:59:18,859|UnsafeReadModifyWrite|Final number (should be 10_000): 9996</span><br />
<br />
Depending on your computer it will be very difficult to reproduce this update loss, since there’s no guarantee on how threads will interleave. If you can’t reproduce the above example, try <a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/UnsafeReadModifyWriteWithLatch.java" target="_blank">UnsafeReadModifyWriteWithLatch</a>, which uses a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html" target="_blank">CountDownLatch</a> to synchronize thread’s start, and repeats the test a hundred times. You should probably see some invalid values among all the results:<br />
<br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 997</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 999</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<span style="font-size: x-small;">Final number (should be 1_000): 1000</span><br />
<br />
This example can be solved by making all three actions atomic.<br />
<br />
<a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/SafeReadModifyWriteSynchronized.java" target="_blank">SafeReadModifyWriteSynchronized</a> uses synchronization in all accesses to the shared field:
<br />
<br />
<script src="https://gist.github.com/xpadro/ca07d72596484af71bb7.js"></script>
Let’s see another example to remove this race condition. In this specific case, and since the field number is independent to other variables, we can make use of atomic variables. <a href="https://github.com/xpadro/concurrency/blob/master/basic.atomicity/src/main/java/atomicity/SafeReadModifyWriteAtomic.java" target="_blank">SafeReadModifyWriteAtomic</a> uses atomic variables to store the value of the field:
<br />
<br />
<script src="https://gist.github.com/xpadro/ff14adb4970be7448086.js"></script>
Following posts will further explain mechanisms like locking or atomic variables.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">Conclusion</span></h3>
<br />
This post explained some of the risks implied when executing compound actions in non-synchronized multi-threaded programs. To enforce atomicity and prevent thread interleaving, one must use some type of synchronization.<br />
<br />
This post is part of the Java Concurrency Tutorial series. Check <a href="http://xpadro.blogspot.com/2014/09/java-concurrency-tutorial.html" target="_blank">here</a> to read the rest of the tutorial.<br />
<br />
You can take a look at the source code at <a href="https://github.com/xpadro/concurrency/tree/master/basic.atomicity" target="_blank">github</a>.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-62819674222712413942014-05-05T11:51:00.000+02:002016-06-06T09:33:24.101+02:00Spring Integration 4.0: A complete XML-free example<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<div>
<br /></div>
<div>
Spring Integration 4.0 is finally <a href="https://spring.io/blog/2014/04/30/spring-integration-4-0-released" target="_blank">here</a>, and this release comes with very nice features. The one covered in this article is the possibility to configure an integration flow without using XML at all. Those people that don’t like XML will be able to develop an integration application with just using JavaConfig.</div>
<div>
<br /></div>
<div>
This article is divided in the following sections:</div>
<div>
<ol>
<li>Introduction.</li>
<li>An overview of the flow.</li>
<li>Spring configuration.</li>
<li>Detail of the endpoints.</li>
<li>Testing the entire flow.</li>
<li>Conclusion.</li>
</ol>
</div>
<br />
The source code can be found at <a href="https://github.com/xpadro/spring-integration/tree/master/generic/int-v4-full" target="_blank">github</a>.
<br />
<br />
The source code of the web service invoked in this example can be found at the spring-samples repository at <a href="https://github.com/xpadro/spring-samples/tree/master/spring-ws-courses" target="_blank">github</a>.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 An overview of the flow</span></h3>
<br />
The example application shows how to configure several messaging and integration endpoints. The user asks for a course by specifying the course Id. The flow will invoke a web service and return the response to the user. Additionally, some type of courses will be stored to a database.
<br />
<br />
The flow is as follows:
<br />
<ul>
<li>An integration <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/messaging-endpoints-chapter.html#gateway" target="_blank">gateway </a>(course service) serves as the entry to the messaging system.</li>
<li>A <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/messaging-transformation-chapter.html#transformer" target="_blank">transformer </a>builds the request message from the user specified course Id.</li>
<li>A web service <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/ws.html#webservices-outbound" target="_blank">outbound gateway</a> sends the request to a web service and waits for a response.</li>
<li>A <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/messaging-endpoints-chapter.html#service-activator" target="_blank">service activator</a> is subscribed to the response channel in order to return the course name to the user.</li>
<li>A <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/messaging-routing-chapter.html#filter" target="_blank">filter </a>is also subscribed to the response channel. This filter will send some types of courses to a mongodb <a href="http://docs.spring.io/spring-integration/docs/4.0.0.RELEASE/reference/html/mongodb.html#mongodb-outbound-channel-adapter" target="_blank">channel adapter</a> in order to store the response to a database.</li>
</ul>
<br />
The following diagram better shows how the flow is structured:
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik1U1wHEMFxRaVmtPPWFuJze5bZUmjSDw8X0UBDhGasyIqyC1l4GQAJ_7qd8mJ8T0QWklKMBSIzTvPIk4JFGw_v-PxJg3s6IdU1Ulpwu6y1L8CSTeShnsF22pLnOKN4ZF-ute9DC_R-vk/s1600/int-v4-full-flow_v2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="428" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik1U1wHEMFxRaVmtPPWFuJze5bZUmjSDw8X0UBDhGasyIqyC1l4GQAJ_7qd8mJ8T0QWklKMBSIzTvPIk4JFGw_v-PxJg3s6IdU1Ulpwu6y1L8CSTeShnsF22pLnOKN4ZF-ute9DC_R-vk/s1600/int-v4-full-flow_v2.png" width="640" /></a></div>
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Spring configuration</span></h3>
<br />
As discussed in the introduction section, the entire configuration is defined with JavaConfig. This configuration is split into three files: infrastructure, web service and database configuration. Let’s check it out:
<br />
<br />
<h3>
<span style="color: #0b5394;">3.1 Infrastructure configuration</span></h3>
<br />
This configuration file only contains the definition of message channels. The messaging endpoints (transformer, filter, etc...) are configured with annotations.
<br />
<br />
<b>InfrastructureConfiguration.java</b>
<br />
<script src="https://gist.github.com/xpadro/c699e43840766d956cd6.js"></script>
The @ComponentScan annotation searches for @Component annotated classes, which are our defined messaging endpoints; the filter, the transformer and the service activator.
<br />
<br />
The @IntegrationComponentScan annotation searches for specific integration annotations. In our example, it will scan the entry gateway which is annotated with @MessagingGateway.
<br />
<br />
The @EnableIntegration annotation enables integration configuration. For example, method level annotations like @Transformer or @Filter.
<br />
<br />
<h3>
<span style="color: #0b5394;">3.2 Web service configuration</span></h3>
<br />
This configuration file configures the web service outbound gateway and its required marshaller.
<br />
<br />
<b>WebServiceConfiguration.java</b>
<br />
<script src="https://gist.github.com/xpadro/b55aef2bb6a48774ff6c.js"></script>
The gateway allows us to define its output channel but not the input channel. We need to annotate the adapter with @ServiceActivator in order to subscribe it to the invocation channel and avoid having to autowire it in the message channel bean definition.
<br />
<br />
<h3>
<span style="color: #0b5394;">3.3 Database configuration</span></h3>
<br />
This configuration file defines all necessary beans to set up <a href="https://www.mongodb.org/" target="_blank">mongoDB</a>. It also defines the mongoDB outbound channel adapter.
<br />
<br />
<b>MongoDBConfiguration.java</b>
<br />
<script src="https://gist.github.com/xpadro/bb64f9dc85c9352700de.js"></script>
Like the web service gateway, we can’t set the input channel to the adapter. I also have done that by specifying the input channel in the @ServiceActivator annotation.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Detail of the endpoints</span></h3>
<br />
The first endpoint of the flow is the integration gateway, which will put the argument (courseId) into the payload of a message and send it to the request channel.
<br />
<br />
<script src="https://gist.github.com/xpadro/64e0151438ae133671bf.js"></script>
The message containing the course id will reach the transformer. This endpoint will build the request object that the web service is expecting:
<br />
<br />
<script src="https://gist.github.com/xpadro/cb383419382bfc4346a4.js"></script>
Subscribed to the response channel, which is the channel where the web service reply will be sent, there’s a service activator that will receive the response message and deliver the course name to the client:
<br />
<br />
<script src="https://gist.github.com/xpadro/4f9e7be35677d805454d.js"></script>
Also subscribed to the response channel, a filter will decide based on its type, if the course is required to be stored to a database:
<br />
<br />
<script src="https://gist.github.com/xpadro/767bee913191d7cc8e5d.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Testing the entire flow</span></h3>
<br />
The following client will send two requests; a BC type course request that will be stored to the database and a DF type course that will be finally filtered:
<br />
<br />
<script src="https://gist.github.com/xpadro/7a486789cd4d6859bcf6.js"></script>
This will result in the following console output:
<br />
<br />
<div>
<span style="font-size: x-small;">CourseRequestBuilder|Building request for course [BC-45]</span></div>
<div>
<span style="font-size: x-small;">CourseResponseHandler|Course with ID [BC-45] received: Introduction to Java</span></div>
<div>
<span style="font-size: x-small;">StoredCoursesFilter|Course [BC-45] validated. Storing to database</span></div>
<div>
<span style="font-size: x-small;">CourseRequestBuilder|Building request for course [DF-21]</span></div>
<div>
<span style="font-size: x-small;">CourseResponseHandler|Course with ID [DF-21] received: Functional Programming Principles in Scala</span></div>
<div>
<span style="font-size: x-small;">StoredCoursesFilter|Course [DF-21] filtered. Not a BF course</span></div>
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
We have learnt how to set up and test an application powered with Spring Integration using no XML configuration. Stay tuned, because Spring Integration Java DSL with Spring Integration <a href="https://github.com/spring-projects/spring-integration-extensions/tree/master/spring-integration-java-dsl" target="_blank">extensions </a>is on its way!
<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.
Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com14tag:blogger.com,1999:blog-6026073423954662509.post-62199136519011145072014-04-28T14:23:00.000+02:002016-06-06T08:18:00.757+02:00Spring Integration - Configure web service client timeout<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
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, thus 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 of operations. This article will show how to set a client timeout and integrate it with the gateway.<br />
<br />
This article is divided in the following sections:<br />
<ol>
<li>Introduction.</li>
<li>Web service invocation overview.</li>
<li>Configuring a message sender.</li>
<li>The sample application.</li>
<li>Conclusion.</li>
</ol>
<br />
The source code can be found at <a href="https://github.com/xpadro/spring-integration/tree/master/webservices/ws-timeout" target="_blank">github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Web service invocation overview</span></h3>
<br />
The web service outbound gateway delegates the web service invocation to the <a href="http://projects.spring.io/spring-ws/" target="_blank">Spring Web Services</a> <a href="http://docs.spring.io/spring-ws/sites/2.0/apidocs/org/springframework/ws/client/core/WebServiceTemplate.html" target="_blank">WebServiceTemplate</a>. When a message arrives to the outbound gateway, this template uses a message sender in order to create a new connection. The diagram below shows an overview of the flow:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg04BTM1Xs-czzCaiOdnKG41hawBqtmRMlkBFWuPbTe9jd7Kwc4FaVbAoZiyc3BKhhLRh-6GdHwMaCURc9UnGyMCZt7h9X7FKa6Dcx3xd9rBGy9kD9u6QksFO1aM5XwOPMTyV_mEUOqDJ8/s1600/int-timeout-sequence.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="248" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg04BTM1Xs-czzCaiOdnKG41hawBqtmRMlkBFWuPbTe9jd7Kwc4FaVbAoZiyc3BKhhLRh-6GdHwMaCURc9UnGyMCZt7h9X7FKa6Dcx3xd9rBGy9kD9u6QksFO1aM5XwOPMTyV_mEUOqDJ8/s1600/int-timeout-sequence.png" width="640" /></a></div>
<br />
By default, the web service template sets an <a href="http://docs.spring.io/spring-ws/sites/2.0/apidocs/org/springframework/ws/transport/http/HttpUrlConnectionMessageSender.html" target="_blank">HttpUrlConnectionMessageSender</a> as its message sender, which is a basic implementation without support for configuration options. This behavior though, can be overridden by setting a more advanced message sender with the capability of setting both read and connection timeouts.<br />
<br />
We are going to configure the message sender in the next section.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Configuring a message sender</span></h3>
<br />
We are going to configure a message sender to the outbound gateway. This way, the gateway will set the template’s message sender with the one provided.<br />
<br />
The implementation we are providing in the example is the <a href="http://docs.spring.io/spring-ws/sites/2.0/apidocs/org/springframework/ws/transport/http/HttpComponentsMessageSender.html" target="_blank">HttpComponentsMessageSender</a> class, also from the Spring Web Services project. This message sender allows us to define the following timeouts:<br />
<ul>
<li><b>connectionTimeout</b>: Sets the timeout until the connection is established.</li>
<li><b>readTimeout</b>: Sets the socket timeout for the underlying HttpClient. This is the time required for the service to reply.</li>
</ul>
<br />
Configuration:<br />
<br />
<script src="https://gist.github.com/xpadro/11318693.js"></script>
The properties file contains the values, which are both set to two seconds:<br />
<br />
<span style="font-size: x-small;">timeout.connection=2000</span><br />
<span style="font-size: x-small;">timeout.read=2000</span><br />
<br />
Once configured, we add it to the web service outbound gateway configuration:<br />
<br />
<script src="https://gist.github.com/xpadro/11318714.js"></script>
To use this message sender, you will need to add the following dependency:<br />
<br />
<script src="https://gist.github.com/xpadro/11318727.js"></script>
And that’s it; the next section will show the sample application to see how it works.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 The sample application</span></h3>
<br />
The flow is simple; it consists in an application that sends a request to a web service and receives a response. The web service source code can be found at <a href="https://github.com/xpadro/spring-samples/tree/master/spring-ws-courses" target="_blank">github</a>.<br />
<br />
<script src="https://gist.github.com/xpadro/11318736.js"></script>
The gateway contains the method through which we will enter the messaging system:<br />
<br />
<script src="https://gist.github.com/xpadro/11318750.js"></script>
Finally, the test:<br />
<br />
<script src="https://gist.github.com/xpadro/11318776.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion</span></h3>
<br />
We have learnt how to set additional options to the web service outbound gateway in order to establish a timeout.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com0tag:blogger.com,1999:blog-6026073423954662509.post-27860016484648804662014-02-24T14:04:00.000+01:002017-11-01T19:55:03.326+01:00Thymeleaf integration with Spring (Part 2)<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
This is the second part of the Thymeleaf integration with Spring tutorial. You can read the first part <a href="http://xpadro.blogspot.com/2014/02/thymeleaf-integration-with-spring-part-1.html" target="_blank">here</a>, where you will learn how to configure this project.<br />
<br />
As explained at the beginning of the first part of this tutorial, the web application will send two types of requests:<br />
<ul>
<li>Insert a new guest: Sends a synchronous request to the server for adding a new guest. This will demonstrate how Thymeleaf is integrated with Spring’s form backing beans.</li>
</ul>
<ul>
<li>List guests: Sends an AJAX request that will update a region (<a href="http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#including-template-fragments" target="_blank">fragment</a>) of the page in order to show the guest list returned from the server.</li>
</ul>
<br />
Let's see how we will accomplish this.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Handling forms</span></h3>
<br />
In this section we are going to see how a form is submitted with Thymeleaf. We will basically need three attributes:<br />
<br />
th:action<br />
th:object<br />
th:field<br />
<br />
The first two are defined in the form element:<br />
<br />
<script src="https://gist.github.com/xpadro/9124388.js"></script>
The <i>th:action</i> attribute rewrites the action url, prefixing the application context to it.<br />
<br />
The <i>th:object</i> attribute in the form element is the object selection. It can then be referenced within the form. What we do here is bind the form backing bean to the model attribute, which we defined in the controller before rendering the view:<br />
<br />
<script src="https://gist.github.com/xpadro/9124403.js"></script>
As we see, <i>th:object</i> refers to the Guest form backing bean, while <i>th:field</i> will refer to its properties. Take a look at the form body:<br />
<br />
<script src="https://gist.github.com/xpadro/9124427.js"></script>
What <i>th:field</i> will do is assign the
value of its input element to the backing bean property. So, when the user
submits the form, all these <i>th:field</i> will set the form backing bean
properties.
<br />
<br />
At the controller, we will receive
the Guest instance:
<br />
<br />
<script src="https://gist.github.com/xpadro/9124541.js"></script>
Now the guest can be inserted into the database.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Sending AJAX requests</span></h3>
<br />
When trying to find a simple example of sending an AJAX request with Thymeleaf, I have found examples with Spring Webflow (<a href="https://github.com/spring-projects/spring-webflow-samples/blob/master/booking-mvc/src/main/webapp/WEB-INF/hotels/booking/booking-flow.xml" target="_blank">render fragments</a>). I also <a href="http://stackoverflow.com/questions/15739584/ajax-in-the-spring-mvc-thymeleaf-application" target="_blank">read </a>others saying that you need Tiles in order to accomplish that.<br />
<br />
I didn't want to use those frameworks so in this section I'm using jQuery to send an AJAX request to the server, wait for the response and partially update the view (fragment rendering).<br />
<br />
<u>The form</u><br />
<br />
<script src="https://gist.github.com/xpadro/9124579.js"></script>
This form contains an input text with a search string (searchSurname) that will be sent to the server. There's also a region (resultsBlock div) which will be updated with the response received from the server.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHdggbniUpbxxWflrNR2BvAKzgVc9s9CtmPQoY79Rsgr889zEpqk5wXM-_0FBJeTTr1Qa0jf0hJTbWx8k-JdsowwsedZguE3Pdll74JICJwnPWtV9obWJQGoiF8DrLDc4QNU3e5xPkm40/s1600/searchForm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHdggbniUpbxxWflrNR2BvAKzgVc9s9CtmPQoY79Rsgr889zEpqk5wXM-_0FBJeTTr1Qa0jf0hJTbWx8k-JdsowwsedZguE3Pdll74JICJwnPWtV9obWJQGoiF8DrLDc4QNU3e5xPkm40/s1600/searchForm.png" /></a></div>
<br />
<br />
When the user clicks the button, the retrieveGuests() function will be invoked.<br />
<br />
<script src="https://gist.github.com/xpadro/9124664.js"></script>
The jQuery <a href="https://api.jquery.com/load/" target="_blank">load</a> function makes a request to the server at the specified url and places the returned HTML into the specified element (resultsBlock div).<br />
<br />
If the user enters a search string, it will search for all guests with the specified surname. Otherwise, it will return the complete guest list. These two requests will reach the following controller request mappings:<br />
<br />
<script src="https://gist.github.com/xpadro/9124677.js"></script>
Since Spring is integrated with Thymeleaf, it will now be able to return fragments of HTML. In the above example, the return string "results :: resultsList" is referring to a fragment named resultsList which is located in the results page. Let's take a look at this results page:<br />
<br />
<script src="https://gist.github.com/xpadro/9124703.js"></script>
The fragment, which is a table with registered guests, will be inserted in the results block:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNnKNfnPsfAJ_lmjiDSnijLsCM4xZ0McGU2AQ7dxzCedqCICdGELnbXvLO7I_PURzndhTn8DNwwQZino6XoQoHeKy0F_6NR3nYfRG4836ef38SYMtsUGMwH8TyW8eHkv4oy4yAhm6YcqY/s1600/resultsBlock.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="170" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNnKNfnPsfAJ_lmjiDSnijLsCM4xZ0McGU2AQ7dxzCedqCICdGELnbXvLO7I_PURzndhTn8DNwwQZino6XoQoHeKy0F_6NR3nYfRG4836ef38SYMtsUGMwH8TyW8eHkv4oy4yAhm6YcqY/s1600/resultsBlock.png" width="400" /></a></div>
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Conclusion</span></h3>
<br />
After integrating both frameworks, we learnt how forms are linked to the Spring MVC model. We also learnt how to send AJAX requests and partially update the view.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com22tag:blogger.com,1999:blog-6026073423954662509.post-72216053358594437402014-02-17T16:07:00.000+01:002017-11-01T19:55:31.794+01:00Thymeleaf integration with Spring (Part 1)<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
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.<br />
<br />
The example consists in a Hotel's single page web application from where we can send two different requests:<br />
<br />
<ul>
<li>Insert a new guest: A synchronous request that shows how Thymeleaf is integrated with Spring's form backing beans.</li>
<li>List guests: An asynchronous request that shows how to handle fragment rendering with AJAX.</li>
</ul>
<br />
This tutorial expects you to know the basics of Thymeleaf. If not, you should first read <a href="http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html" target="_blank">this article</a>.<br />
<br />
Here's an example of the application flow:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-Escg7ZAil6Q/VUZZguKWzQI/AAAAAAAAEpc/spoeIGUy-24/s1600/hotelFlow.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="316" src="https://4.bp.blogspot.com/-Escg7ZAil6Q/VUZZguKWzQI/AAAAAAAAEpc/spoeIGUy-24/s1600/hotelFlow.png" width="320" /></a></div>
<br />
This example is based on Thymeleaf <a href="http://www.thymeleaf.org/whatsnew21.html" target="_blank">2.1</a> and Spring 4 versions.<br />
<br />
The source code can be found at <a href="https://github.com/xpadro/thymeleaf/tree/master/th-spring-integration" target="_blank">github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Configuration</span></h3>
<br />
This tutorial takes the <a href="http://docs.spring.io/spring/docs/4.0.1.RELEASE/spring-framework-reference/htmlsingle/#beans-java" target="_blank">JavaConfig </a>approach to configure the required beans. This means xml configuration files are no longer necessary.<br />
<br />
<u>web.xml</u><br />
Since we want to use JavaConfig, we need to specify <a href="http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.html" target="_blank">AnnotationConfigWebApplicationContext </a>as the class that will configure the Spring container. If we don't specify it, it will use <a href="http://docs.spring.io/spring/docs/4.0.x/javadoc-api/org/springframework/web/context/support/XmlWebApplicationContext.html" target="_blank">XmlWebApplicationContext </a>by default.<br />
<br />
When defining where the configuration files are located, we can specify classes or packages. Here, I'm indicating my configuration class.<br />
<br />
<script src="https://gist.github.com/xpadro/9051206.js"></script>
<u>Spring Configuration</u><br />
My configuration is split in two classes: thymeleaf-spring integration (<a href="https://github.com/xpadro/thymeleaf/blob/master/th-spring-integration/src/main/java/xpadro/thymeleaf/configuration/WebAppConfiguration.java" target="_blank">WebAppConfiguration </a>class) and mongoDB configuration (<a href="https://github.com/xpadro/thymeleaf/blob/master/th-spring-integration/src/main/java/xpadro/thymeleaf/configuration/MongoDBConfiguration.java" target="_blank">MongoDBConfiguration </a>class).<br />
<br />
WebAppConfiguration.java<br />
<br />
<script src="https://gist.github.com/xpadro/9051269.js"></script>
Things to highlight from looking at the above code:<br />
<ul>
<li><a href="http://docs.spring.io/spring/docs/4.0.1.RELEASE/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html" target="_blank">@EnableWebMvc</a>: This enables Spring MVC annotations like @RequestMapping. This would be the same as the xml namespace <mvc:annotation-driven /></li>
<li><a href="http://docs.spring.io/spring/docs/4.0.1.RELEASE/javadoc-api/org/springframework/context/annotation/ComponentScan.html" target="_blank">@ComponentScan(“xpadro.thymeleaf”)</a>: Activates component scanning in the xpadro.thymeleaf package and subpackages. Classes annotated with @Component and related annotations will be registered as beans.</li>
<li>We are registering three beans which are necessary to configure Thymeleaf and integrate it with the Spring framework.</li>
<ul>
<li>template resolver: Resolves template names and delegates them to a servlet context resource resolver.</li>
<li>template engine: Integrates with Spring framework, establishing the Spring specific dialect as the default dialect.</li>
<li>view resolver: Thymeleaf implementation of the Spring MVC view resolver interface in order to resolve Thymeleaf views.</li>
</ul>
</ul>
<br />
MongoDBConfiguration.java<br />
<br />
<script src="https://gist.github.com/xpadro/9051290.js"></script>
This class extends <a href="http://docs.spring.io/spring-data/data-mongodb/docs/1.3.3.RELEASE/api/org/springframework/data/mongodb/config/AbstractMongoConfiguration.html" target="_blank">AbstracMongoConfiguration</a>, which defines mongoFactory and mongoTemplate beans.
<br />
The @EnableMongoRepositories will scan the specified package in order to find interfaces extending MongoRepository. Then, it will create a bean for each one. We will see this later, at the data access layer section.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Thymeleaf – Spring MVC Integration</span></h3>
<br />
<u>HotelController
</u><br />
The controller is responsible for accessing the service layer, construct the view model from the result and return a view. With the configuration that we set in the previous section, now MVC Controllers will be able to return a view Id that will be resolved as a Thymeleaf view.
<br />
<br />
Below we can see a fragment of the controller where it handles the initial request (http://localhost:8080/th-spring-integration/spring/home):
<br />
<br />
<script src="https://gist.github.com/xpadro/9051349.js"></script>
A typical MVC Controller that returns a "home" view id. Thymeleaf template resolver will look for a template named "home.html" which is located in /WEB-INF/html/ folder, as indicated in the configuration. Additionally, a view attribute named "hotelData" will be exposed to the Thymeleaf view, containing hotel information that needs to be displayed on the initial view.
<br />
<br />
This fragment of the home view shows how it accesses some of the properties of the view attribute by using Spring Expression Language (Spring EL):
<br />
<br />
<script src="https://gist.github.com/xpadro/9051366.js"></script>
Another nice feature is that Thymeleaf will be able to resolve Spring managed message properties, which have been configured through the MessageSource interface.
<br />
<br />
<script src="https://gist.github.com/xpadro/9051379.js"></script>
<br />
<u>Error handling
</u><br />
Trying to add a new user will raise an exception if a user with the same id already exists. The exception will be handled and the home view will be rendered with an error message.
<br />
<br />
Since we only have one controller, there's no need to use <a href="http://xpadro.blogspot.com/2013/03/centralize-validation-and-exception.html" target="_blank">@ControllerAdvice</a>. We will instead use a @ExceptionHandler annotated method. You can notice that we are returning an internationalized message as the error message:
<br />
<br />
<script src="https://gist.github.com/xpadro/9051398.js"></script>
Thymeleaf will resolve the view attribute with ${} and then it will resolve the message #{}:
<br />
<br />
<script src="https://gist.github.com/xpadro/9051406.js"></script>
The th:unless Thymeleaf attribute will only render the span element if an error message has been returned.
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjA3kHl6DC5zrdDjvS_lWpjq0-2YHRNL8cfjOok764Z54aHnwRMyEmXtwNUEJYiTi7AWVXOxPqqa78YoidUABKxJDoH_udCOMvEF-o4GSHzzKdaRQhdlkYrxju0m2i0VuoYur2JqDPqMnc/s1600/hotelError.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="158" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjA3kHl6DC5zrdDjvS_lWpjq0-2YHRNL8cfjOok764Z54aHnwRMyEmXtwNUEJYiTi7AWVXOxPqqa78YoidUABKxJDoH_udCOMvEF-o4GSHzzKdaRQhdlkYrxju0m2i0VuoYur2JqDPqMnc/s1600/hotelError.png" width="640" /></a></div>
<br />
<h3>
<span style="color: #0b5394;">4 The Service layer</span></h3>
<br />
The service layer accesses the data access layer and adds some business logic.
<br />
<br />
<script src="https://gist.github.com/xpadro/9051424.js"></script>
<br />
<h3>
<span style="color: #0b5394;">5 The Data Access layer</span></h3>
<br />
The HotelRepository extends the Spring Data class <a href="http://docs.spring.io/spring-data/mongodb/docs/1.3.3.RELEASE/api/org/springframework/data/mongodb/repository/MongoRepository.html" target="_blank">MongoRepository</a>.
<br />
<br />
<script src="https://gist.github.com/xpadro/9051449.js"></script>
This is just an interface, we won't implement it. If you remember the configuration class, we added the following annotation:
<br />
<br />
<script src="https://gist.github.com/xpadro/9051456.js"></script>
Since this is the package where the repository is located, Spring will create a bean and inject a mongoTemplate to it. Extending this interface provides us with generic CRUD operations. If you need additional operations, you can add them with the @Query annotation (see code above).
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
We have configured Thymeleaf to resolve views in a Spring managed web application. This allows the view to access to Spring Expression Language and message resolving. The next part of this tutorial is going to show how forms are linked to Spring form backing beans and how we can reload fragments by sending an AJAX request.
<br />
<br />
Read the next part of this tutorial: <a href="http://xpadro.blogspot.com/2014/02/thymeleaf-integration-with-spring-part-2.html" target="_blank">Thymeleaf integration with Spring (Part 2)</a><br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.
Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com7tag:blogger.com,1999:blog-6026073423954662509.post-51693428423468049742014-01-20T08:23:00.000+01:002015-05-03T19:22:31.065+02:00Migrating Spring MVC RESTful web services to Spring 4<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
Spring 4 brings several <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#_general_web_improvements" target="_blank">improvements</a> for MVC applications. In this post I will focus on restful web services and try these improvements by taking a project implemented with Spring 3.2 and upgrading it to Spring 4. The following points sum up the content of this post:<br />
<ul>
<li>Migration from Spring 3.2 to Spring 4.0</li>
<li>Changes in <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html" target="_blank">@ResponseBody</a> and inclusion of <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/web/bind/annotation/RestController.html" target="_blank">@RestController</a></li>
<li>Synchronous and Asynchronous calls</li>
</ul>
<br />
The source code of the following projects can be found at github:<br />
<br />
<a href="https://github.com/xpadro/spring-rest/tree/master/spring-rest-api-v32" target="_blank">Original project (spring 3.2)</a><br />
<br />
<a href="https://github.com/xpadro/spring-rest/tree/master/spring-rest-api-v4" target="_blank">Migration to Spring 4</a><br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 The Spring 3.2 RESTful sample</span></h3>
<br />
The starting project is implemented with Spring 3.2 (<a href="https://github.com/xpadro/spring-rest/blob/master/spring-rest-api-v32/pom.xml">pom.xml</a>) . It consists in a Spring MVC application that access a database to retrieve data about TV series. Let's have a look at its REST API to see it clearer:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0IAjNV6C40dZfyX3Qm-fBd2xJToj_eBVZ0x5rSXlZ_dL5wZNsl6S8wrMiLQVocQRT-xFdK_HGenAIRd-ydyWp7kGbZUpoW7kn9SmIskJ1N5osdZSvMx2HHpYEpCEwZr9-rzTaGF4Sxk/s1600/blog-rest-api.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX0IAjNV6C40dZfyX3Qm-fBd2xJToj_eBVZ0x5rSXlZ_dL5wZNsl6S8wrMiLQVocQRT-xFdK_HGenAIRd-ydyWp7kGbZUpoW7kn9SmIskJ1N5osdZSvMx2HHpYEpCEwZr9-rzTaGF4Sxk/s1600/blog-rest-api.png" height="232" width="320" /></a></div>
<br />
<br />
<b>Spring configuration</b><br />
<br />
root-context.xml<br />
<script src="https://gist.github.com/xpadro/8491920.js"></script>
<br />
db-context.xml<br />
<script src="https://gist.github.com/xpadro/8491942.js"></script>
<br />
<b>Service implementation</b><br />
This class is responsible of retrieving the data from a mongoDB database:<br />
<br />
<script src="https://gist.github.com/xpadro/8491966.js"></script>
<br />
<b>Controller implementation</b><br />
This controller will handle requests and interact with the service in order to retrieve series data:<br />
<br />
<script src="https://gist.github.com/xpadro/8491990.js"></script>
<br />
<b>Integration testing</b><br />
These integration tests will test our controller within a mock Spring MVC environment. In this way, we will be able to also test the mappings of our handler methods. For this purpose, the <a href="http://docs.spring.io/spring-framework/docs/3.2.x/javadoc-api/org/springframework/test/web/servlet/MockMvc.html">MockMvc</a> class becomes very useful. If you want to learn how to write tests of Spring MVC controllers I highly recommend the <a href="http://www.petrikainulainen.net/spring-mvc-test-tutorial/">Spring MVC Test Tutorial</a> series by Petri Kainulainen.<br />
<br />
<script src="https://gist.github.com/xpadro/8492123.js"></script>
I'm showing some of the tests implemented. Check <a href="https://github.com/xpadro/spring-rest/blob/master/spring-rest-api-v32/src/test/java/xpadro/spring/web/test/SeriesIntegrationTesting.java">SeriesIntegrationTesting</a> for complete implementation.<br />
<br />
<b>Functional testing</b><br />
The application contains some functional testing by using the <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/client/RestTemplate.html">RestTemplate</a> class. You need the webapp deployed in order to test this.<br />
<br />
<script src="https://gist.github.com/xpadro/8492152.js"></script>
That's all, the web application is tested and running. Now is time to migrate to Spring 4.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Migrating to Spring 4</span></h3>
<br />
Check <a href="https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework">this page</a> to read information about migrating from earlier versions of the Spring framework<br />
<br />
<h3>
<span style="color: #0b5394;">3.1 Changing maven dependencies</span></h3>
<br />
This section explains which dependencies should be modified. You can take a look at the complete pom.xml <a href="https://github.com/xpadro/spring-rest/blob/master/spring-rest-api-v4/pom.xml">here</a>.<br />
<br />
The first step is to change Spring dependencies version from 3.2.3.RELEASE to 4.0.0.RELEASE:<br />
<br />
<script src="https://gist.github.com/xpadro/8492201.js"></script>
<br />
The next step is to update to Servlet 3.0 specification. This step is important since some of the Spring features are based on Servlet 3.0 and won't be available. In fact, trying to execute SeriesIntegrationTesting will result in a ClassNotFoundException due to this reason, which is also explained <a href="https://jira.springsource.org/browse/SPR-11049">here</a>.
<br />
<br />
<script src="https://gist.github.com/xpadro/8492282.js"></script>
<h3>
<span style="color: #0b5394;">3.2 Updating of Spring namespace</span></h3>
<br />
Don't forget to change the namespace of your spring configuration files:<br />
<script src="https://gist.github.com/xpadro/8492295.js"></script>
Review the information page linked in section 2 since there are some changes regarding mvc namespace.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3.3 Deprecation of jackson libraries</span></h3>
<br />
If you check SeriesFunctionalTesting (setup method) again you will notice that the Jackson converter is now deprecated. If you try to run the test it will throw a NoSuchMethodError due to method change in Jackson libraries:<br />
<br />
<span style="font-size: x-small;">java.lang.NoSuchMethodError: org.codehaus.jackson.map.ObjectMapper.getTypeFactory()Lorg/codehaus/jackson/map/type/TypeFactory</span><br />
<br />
In Spring 4, support to Jackson 1.x has been deprecated in favor of Jackson v2. Let's change the old dependency:<br />
<br />
<script src="https://gist.github.com/xpadro/8492301.js"></script>
For these:<br />
<script src="https://gist.github.com/xpadro/8492308.js"></script>
Finally, if you are explicitly registering message converters you will need to change the deprecated class for the new version:
<br />
<script src="https://gist.github.com/xpadro/8492344.js"></script>
<br />
<br />
<h3>
<span style="color: #0b5394;">3.4 Migration complete</span></h3>
<br />
The migration is done. Now you can run the application and execute its tests. The next section will review some of the improvements I mentioned at the beginning of this post.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Spring 4 Web improvements
</span></h3>
<br />
<h3>
<span style="color: #0b5394;">4.1 @ResponseBody and @RestController
</span></h3>
<br />
If your REST API serves content in JSON or XML format, some of the API methods (annotated with @RequestMapping) will have its return type annotated with @ResponseBody. With this annotation present, the return type will be included into the response body. In Spring 4 we can simplify this in two ways:
<br />
<br />
<b>Annotate the controller with @ResponseBody
</b><br />
This annotation can now be added on type level. In this way, the annotation is inherited and we are not forced to put this annotation in every method.
<br />
<br />
<script src="https://gist.github.com/xpadro/8492436.js"></script>
<br />
<b>Annotate the controller with @RestController</b><br />
<br />
<script src="https://gist.github.com/xpadro/8492448.js"></script>
This annotation simplifies the controller even more. If we check this annotation we will see that it is itself annotated with @Controller and @ResponseBody:<br />
<br />
<script src="https://gist.github.com/xpadro/8492473.js"></script>
Including this annotation won't affect methods annotated with @ResponseEntity. The handler adapter looks up into a list of return value handlers in order to resolve who is capable of handling the response. The <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.html">handler</a> responsible of handling the ResponseEntity return type is asked before the ResponseBody type, so it will be used if ResponseEntity annotation is present at the method.
<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4.2 Asynchronous calls
</span></h3>
<br />
Using the utility class RestTemplate for calling a RESTful service will block the thread until it receives a response. Spring 4 includes <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#rest-async-resttemplate">AsyncRestTemplate</a> in order to execute asynchronous calls. Now you can make the call, continue doing other calculations and retrieve the response later.
<br />
<br />
<script src="https://gist.github.com/xpadro/8492531.js"></script>
<br />
<b>Asynchronous calls with callback
</b><br />
Although the previous example makes an asynchronous call, the thread will block if we try to retrieve the response with futureEntity.get() if the response hasn't already been sent.
<br />
AsyncRestTemplate returns <a href="http://docs.spring.io/spring/docs/4.0.0.RELEASE/javadoc-api/org/springframework/util/concurrent/ListenableFuture.html">ListenableFuture</a>, which extends Future and allows us to register a callback. The following example makes an asynchronous call and keeps going with its own tasks. When the service returns a response, it will be handled by the callback:
<br />
<br />
<script src="https://gist.github.com/xpadro/8492562.js"></script>
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Conclusion
</span></h3>
<br />
We took a Spring 3.2.x web application and migrated it to the new release of Spring 4.0.0. We also reviewed some of the improvements that can be applied to a Spring 4 web application.
<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.
Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com20tag:blogger.com,1999:blog-6026073423954662509.post-36136010744298116362013-12-31T16:31:00.000+01:002016-06-06T08:16:59.757+02:00Retry web service operations with RequestHandlerRetryAdvice<h3>
<span style="color: #0b5394;">1 Introduction</span></h3>
<br />
Sometimes when invoking a web service, we may be interested in retrying the operation in case an error occurs. When using Spring Integration, we can achieve this functionality with <a href="http://docs.spring.io/spring-integration/docs/3.0.0.RELEASE/api/org/springframework/integration/handler/advice/RequestHandlerRetryAdvice.html" target="_blank">RequestHandlerRetryAdvice</a> class. This class will allow us to retry the operation for a specified number of times before giving up and throwing an exception. This post will show you how to accomplish this.<br />
<br />
The test application will invoke a web service and if it fails to respond, it will wait for a specified time and try it again until it receives a response or it reaches a retry limit. If the limit is reached, the failed request will be stored into a database. Mainly, this post shows an example of the following:<br />
<br />
<ul>
<li>Using an<a href="http://docs.spring.io/spring-integration/docs/3.0.0.RELEASE/reference/html/ws.html#webservices-outbound" target="_blank"> outbound gateway</a> to invoke a web service</li>
<li>Configuring a <a href="https://spring.io/blog/2012/10/09/what-s-new-in-spring-integration-2-2-part-4-retry-and-more" target="_blank">retry advice</a> to retry the operation if necessary</li>
<li><a href="http://docs.spring.io/spring-integration/docs/3.0.0.RELEASE/reference/html/mongodb.html" target="_blank">MongoDB</a> integration.</li>
</ul>
<br />
<br />
The source code of the application can be found at <a href="https://github.com/xpadro/spring-integration/tree/master/webservices/ws-retry-adv" target="_blank">github</a>.<br />
You can also get the source code of the web service project that is called by the application at <a href="https://github.com/xpadro/spring-integration/tree/master/webservices/spring-ws" target="_blank">github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 Web service invocation</span></h3>
<br />
<b>Use case</b>: The client invokes the web service and receives a response.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_1KLSrcWfhl_-DQCYQkjz-csFbBov1VY3rmd-9GWO9pvVe8iVGPFzfxeSCDCZIesOXakIVRlzo1p674D6nl6EosduVD6XF3DeA_DSzYUsR3leXiSMw07OvXS4sGYNOeX7UvZDn4T_gHQ/s1600/grafic1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="330" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_1KLSrcWfhl_-DQCYQkjz-csFbBov1VY3rmd-9GWO9pvVe8iVGPFzfxeSCDCZIesOXakIVRlzo1p674D6nl6EosduVD6XF3DeA_DSzYUsR3leXiSMw07OvXS4sGYNOeX7UvZDn4T_gHQ/s1600/grafic1.png" width="640" /></a></div>
<br />
The request enters the messaging system through the "system entry" gateway. It then reaches the outbound gateway, invokes the web service and waits for the response. Once received, the response is sent to the response channel.<br />
<br />
The above image is the result of this configuration:<br />
<br />
<script src="https://gist.github.com/xpadro/8182683.js"></script>
Mapped to the response channel there's a service activator which just logs the result.<br />
<br />
<br />
<u>TestInvocation.java: Sends the request to the entry gateway</u><br />
<br />
<script src="https://gist.github.com/xpadro/8182750.js"></script>
With this configuration, if the service invocation fails, a <a href="http://docs.spring.io/spring-integration/api/org/springframework/integration/MessagingException.html" target="_blank">MessagingException</a> will be raised and sent to the error channel. In the next section, we will add the retry configuration.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Adding the retry advice</span></h3>
<br />
<b>Use case</b>: The initial request failed because the service is not active. We will retry the operation until a response is received from the service.<br />
<br />
In this case, we need to add the retry advice to the web service outbound gateway:<br />
<br />
<script src="https://gist.github.com/xpadro/8182861.js"></script>
<br />
Now the web service outbound gateway will delegate the invocation to the retry advice, which will try the operation as many times as specified until it gets a response from the service.<br />
<br />
Let's define the retry advice:<br />
<br />
<script src="https://gist.github.com/xpadro/8182914.js"></script>
<br />
To accomplish its objective, the advice uses a RetryTemplate, which is provided by the <a href="https://github.com/spring-projects/spring-retry" target="_blank">Spring Retry</a> project. We can customize its behavior by defining <i>backoff </i>and <i>retry </i>policies.<br />
<br />
<b>Backoff policy</b>: Establishes a period of time between each retry. The more interesting types are:<br />
<br />
<ul>
<li><i>FixedBackOffPolicy</i>: Used in our example. It will wait for the same specified amount of time between each retry.</li>
<li><i>ExponentialBackOffPolicy</i>: Starting with a determined amount of time, it will double the time on each retry. You can change the default behavior by establishing a multiplier.</li>
</ul>
<br />
<br />
<b>Retry policy</b>: Establishes how many times will retry the failed operation. Some of the types:<br />
<br />
<ul>
<li><i>SimpleRetryPolicy</i>: Used in our example. Specifies a retry attempts limit.</li>
<li><i>ExceptionClassifierRetryPolicy</i>: Allows us to establish a different maxAttempts depending on the exception raised.</li>
<li><i>TimeoutRetryPolicy</i>: Will keep retrying until a timeout is reached.</li>
</ul>
<br />
<br />
<h3>
<span style="color: #0b5394;">4 No luck, logging the failed request</span></h3>
<br />
<b>Use case</b>: The service won't recover, storing the request to the database.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEBNwzzDi7IGzhYbMAkFZpq7a42iOs6MfyRM7xSpklY67nR6nUvC_sCKaGegX9W16VQfTC3VJM4BiAgjGFLbM9J8Xxbl5I-vrcTJFKGh6owgwdHGAP46Yv2gMz46CpfHLGHWPze6ltDvE/s1600/pic2..png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="378" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEBNwzzDi7IGzhYbMAkFZpq7a42iOs6MfyRM7xSpklY67nR6nUvC_sCKaGegX9W16VQfTC3VJM4BiAgjGFLbM9J8Xxbl5I-vrcTJFKGh6owgwdHGAP46Yv2gMz46CpfHLGHWPze6ltDvE/s1600/pic2..png" width="640" /></a></div>
<br />
<br />
The final part of the configuration is the following:<br />
<br />
<script src="https://gist.github.com/xpadro/8182972.js"></script>
The service activator subscribed to the error channel will retrieve the failed message and send it to the outbound adapter, which will insert it to a mongoDB database.<br />
<br />
The service activator:<br />
<br />
<script src="https://gist.github.com/xpadro/8183101.js"></script>
<br />
If we not succeed in obtaining a response from the service, the request will be stored into the database:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjA98Xehs9cKasFuhOfIWaxpl4lmdwCs06X4p_7tRkdEBPBcnJ_x9pYqLzDzvLJNcmpjVoNETD36S4ESTdY182kAHQW6ht9r4GSNtGsO2naH_wJOZ2aJjzreT4L3SZSCMkI9XRhpV_uhyphenhyphenE/s1600/mongodb.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="126" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjA98Xehs9cKasFuhOfIWaxpl4lmdwCs06X4p_7tRkdEBPBcnJ_x9pYqLzDzvLJNcmpjVoNETD36S4ESTdY182kAHQW6ht9r4GSNtGsO2naH_wJOZ2aJjzreT4L3SZSCMkI9XRhpV_uhyphenhyphenE/s1600/mongodb.png" width="640" /></a></div>
<br />
<br />
<div>
<h3>
</h3>
</div>
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
We've learnt how Spring Integration gets support from the Spring Retry project in order to achieve retry of operations. We've used the <i>int-ws:request-handler-advice-chain</i>, but the 'int' namespace also supports this element to add this functionality to other types of endpoints.<br />
<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com1tag:blogger.com,1999:blog-6026073423954662509.post-49722278490726647672013-11-30T17:12:00.000+01:002016-06-06T09:33:55.876+02:00How error handling works in Spring Integration<h3>
<span style="color: #0b5394;">1 Introduction
</span></h3>
<br />
The target of this post is to show you how errors are handled when using the messaging system with Spring Integration. 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.<br />
<br />
You can get the source code at <a href="https://github.com/xpadro/spring-integration/tree/master/generic/int-error" target="_blank">github</a>.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">2 The sample application
</span></h3>
<br />
I will use a basic example, since I want to focus on exception handling. The application consists in an order service, which receives an order, processes it and returns a confirmation.<br />
<br />
Below we can see how the messaging system is configured:<br />
<br />
<u>int-config.xml</u><br />
<script src="https://gist.github.com/xpadro/7720132.js"></script>
The gateway is the entry point of the messaging system. It will receive the order and send it to the direct channel "requestChannel" where a router will redirect it to the appropriate channel based on the order id:<br />
<br />
<ul>
<li>syncChannel: A <a href="http://docs.spring.io/spring-integration/docs/2.2.6.RELEASE/api/org/springframework/integration/channel/DirectChannel.html" target="_blank">direct channel</a> that will send the order to an order processor subscribed to this channel.</li>
<li>asyncChannel: A <a href="http://docs.spring.io/spring-integration/docs/2.2.6.RELEASE/api/org/springframework/integration/channel/QueueChannel.html" target="_blank">queue channel</a> from which the order processor will actively retrieve the order.</li>
</ul>
<br />
Once the order is processed, an order confirmation will be sent back to the gateway. Here is a graphic representing this:<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinBPnm1g40AGhjMAaHisPh0MEl_hz9ePRiYq6-mFmYpfnczeC29gT9ec7xxpjSAH7ZUVWlHVcuZQ-OOL_QZPX_-JQohJnf3zlOMo4OszTztskvJmIaBrVIzrSndAwut9RfsDmkLvOJgYQ/s1600/grafic.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinBPnm1g40AGhjMAaHisPh0MEl_hz9ePRiYq6-mFmYpfnczeC29gT9ec7xxpjSAH7ZUVWlHVcuZQ-OOL_QZPX_-JQohJnf3zlOMo4OszTztskvJmIaBrVIzrSndAwut9RfsDmkLvOJgYQ/s1600/grafic.png" width="378" /></a></div>
<br />
Ok, let's start with the simplest case, synchronous sending using a Direct Channel.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">3 Synchronous sending with Direct channel
</span></h3>
<br />
The order processor is subscribed to the "syncChannel" Direct Channel. The "processOrder" method will be invoked in the sender's thread.<br />
<br />
<script src="https://gist.github.com/xpadro/7720179.js"></script>
Now, we will implement a test that will provoke an exception by sending an invalid order. This test will send an order to the gateway:<br />
<br />
<script src="https://gist.github.com/xpadro/7720215.js"></script>
<br />
The test:<br />
<br />
<u>TestSyncErrorHandling.java</u><br />
<script src="https://gist.github.com/xpadro/7720241.js"></script>
<br />
We run the test and see how an exception is raised in the order processor and reaches the test. That's fine; we wanted to validate that sending an invalid order raised an exception. This happened because the test sent the order and blocked waiting for the order to be processed in the same thread. But, what happens when we use an asynchronous channel? Let's continue to the next section.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">4 Asynchronous sending with Queue Channel
</span></h3>
<br />
This section's test sends an order that will be redirected by the router to the queue channel. The gateway is shown below:
<br />
<br />
<script src="https://gist.github.com/xpadro/7720268.js"></script>
Notice that this time the gateway is returning a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html" target="_blank">Future</a>. If we didn't return this, the gateway would block the test thread. By returning Future, the gateway becomes asynchronous and doesn't block the sender's thread.<br />
<br />
The test:<br />
<br />
<u>TestKoAsyncErrorHandling.java</u><br />
<script src="https://gist.github.com/xpadro/7720305.js"></script>
Ok, so now we are going to launch the test and see the exception raising...<br />
<br />
<span style="font-size: x-small;">java.lang.AssertionError: Expected exception: org.springframework.integration.MessageHandlingException</span><br />
<br />
Oops the test failed because no exception reached the test! What happened? Well, the explanation is below:<br />
<br />
<script src="https://gist.github.com/xpadro/7720335.js"></script>
Since we are using an asynchronous channel (a queue), the sender sends the order and moves on. Then, the receiver polls the order from a different thread. For this reason, it won't be possible to throw the Exception back to the sender. Let's act like nothing happened then? Well you better not, there are other options.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5 Asynchronous error handling
</span></h3>
<br />
When using asynchronous messaging, Spring Integration handles exceptions by publishing them to message channels. The exception thrown will be wrapped into a <a href="http://docs.spring.io/spring-integration/docs/2.2.6.RELEASE/api/org/springframework/integration/MessagingException.html" target="_blank">MessagingException</a> and become the payload of the message.<br />
<br />
What channel is the error message sent to? First, it will check if the request message contains a header called "errorChannel". If found, the error message will be sent there. Otherwise, the message will be sent to a so-called global error channel.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5.1 Global error channel
</span></h3>
<br />
By default, a global error channel called "errorChannel" is created by Spring Integration. This channel is a publish-subscribe channel. This means we can subscribe several endpoints to this channel. In fact, there's already an endpoint subscribed to it: a <a href="http://docs.spring.io/spring-integration/docs/2.2.6.RELEASE/api/org/springframework/integration/handler/LoggingHandler.html" target="_blank">logging handler</a>.This handler will log the payload of messages arriving to the channel, though it can be configured to behave differently.<br />
<br />
We will now subscribe a new handler to this global channel and test that it receives the exception message by storing it into a database.<br />
<br />
First of all, we will need to change a few things in our configuration. I've created a new file so it doesn't interfere with our previous tests:<br />
<br />
<u>int-async-config.xml</u><br />
<script src="https://gist.github.com/xpadro/7720381.js"></script><b>
The gateway</b>: I've added an error channel. If the invocation fails, the error message will be sent to this channel. If I hadn't defined an error channel, the gateway would have propagated the exception to the caller, but in this case it wouldn't have worked since this is an asynchronous gateway.<br />
<br />
<b>The error handler</b>: I've defined a new endpoint that is subscribed to the global error channel. Now, any error message sent to the global error channel will be delivered to our handler.<br />
<br />
I've also added a configuration file in order to configure the database. Our error handler will insert received errors to this database:<br />
<br />
<u>db-config.xml</u><br />
<script src="https://gist.github.com/xpadro/7720415.js"></script>
The error handler is pretty simple; it receives the error message and inserts its information to the database:<br />
<br />
<script src="https://gist.github.com/xpadro/7720435.js"></script>
Ok, now is all set. Let's implement a new test:<br />
<br />
<u>TestOkAsyncErrorHandlingTest.java</u><br />
<script src="https://gist.github.com/xpadro/7720463.js"></script>
This time the test is successful, the error message has been stored to the database.<br />
<br />
<br />
<h3>
<span style="color: #0b5394;">5.2 Other mechanisms</span></h3>
<br />
<b>Custom error channel</b>: You can define your error channel and define it as a queue channel instead of the default publish-subscribe channel:<br />
<br />
<script src="https://gist.github.com/xpadro/7720483.js"></script>
<b>ErrorMessageExceptionTypeRouter</b>: This Spring Integration specialized router will resolve the channel where the error message will be sent. It bases its decision on the most specific cause of the error:<br />
<br />
<script src="https://gist.github.com/xpadro/7720503.js"></script>
<br />
<br />
<h3>
<span style="color: #0b5394;">6 Conclusion</span></h3>
<br />
We have learnt what are the different mechanisms for error handling when using Spring Integration. With this base, you will be able to extend it and configure your error handling by implementing transformers to extract the information from the error message, using header enrichers for setting error channel or implementing your own router among other things.<br />
<br />
I'm publishing my new posts on Google plus and Twitter. Follow me if you want to be updated with new content.<br />
<br />Anonymoushttp://www.blogger.com/profile/05830415740156192526noreply@blogger.com8