<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tharindu Dhananjaya on Medium]]></title>
        <description><![CDATA[Stories by Tharindu Dhananjaya on Medium]]></description>
        <link>https://medium.com/@tharindu.dhananjaya?source=rss-316a0e909f70------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MYxMig_ibqPr6jT5FjS7Lw.jpeg</url>
            <title>Stories by Tharindu Dhananjaya on Medium</title>
            <link>https://medium.com/@tharindu.dhananjaya?source=rss-316a0e909f70------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 21:06:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tharindu.dhananjaya/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ActiveMQ vs RabbitMQ | Key Features and Differences | What is best?]]></title>
            <link>https://medium.com/@tharindu.dhananjaya/activemq-vs-rabbitmq-key-features-and-differences-6a911509d8cb?source=rss-316a0e909f70------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a911509d8cb</guid>
            <category><![CDATA[activemq]]></category>
            <category><![CDATA[rabbitmq]]></category>
            <dc:creator><![CDATA[Tharindu Dhananjaya]]></dc:creator>
            <pubDate>Thu, 16 Nov 2023 14:29:54 GMT</pubDate>
            <atom:updated>2023-11-16T14:41:51.191Z</atom:updated>
            <content:encoded><![CDATA[<p>ActiveMQ and RabbitMQ are both popular open-source message brokers used in distributed systems to facilitate communication between different components or microservices. They serve as intermediaries for passing messages between applications, providing reliable messaging mechanisms. However, they have some differences in terms of features, protocols, and architecture.</p><h3>ActiveMQ:</h3><p>ActiveMQ is an open-source message broker that supports several messaging protocols, including MQTT, AMQP, and Stomp, among others. It is written in Java and is part of the Apache Software Foundation.</p><p><strong>Key features of ActiveMQ:</strong></p><ol><li><strong>Protocols:</strong> Supports various protocols for messaging, making it versatile for different types of applications.</li><li><strong>JMS (Java Message Service) compliance:</strong> Offers full support for the JMS API, allowing Java applications to interact with the message broker using standard JMS functionality.</li><li><strong>Persistence:</strong> Provides different persistence options, allowing messages to be stored in different ways, such as in-memory, file-based, or using a database.</li><li><strong>Clustering:</strong> Supports clustering to achieve high availability and scalability.</li></ol><h3>RabbitMQ:</h3><p>RabbitMQ is another open-source message broker, written in Erlang. It implements the AMQP (Advanced Message Queuing Protocol) standard and has become popular due to its ease of use, reliability, and robustness.</p><p><strong>Key features of RabbitMQ:</strong></p><ol><li><strong>AMQP Support:</strong> Built specifically for AMQP, ensuring strong support and adherence to this messaging protocol standard.</li><li><strong>Plugins and Extensions:</strong> Offers a range of plugins and extensions that can extend its functionality, such as management plugins, MQTT support, etc.</li><li><strong>Message Durability:</strong> Emphasizes message durability and reliability, providing different features to ensure messages are not lost.</li><li><strong>Clustering:</strong> Supports clustering and high availability configurations.</li></ol><h3>Differences and Considerations:</h3><ol><li><strong>Protocols:</strong> ActiveMQ supports various messaging protocols, whereas RabbitMQ is primarily focused on the AMQP protocol.</li><li><strong>Technology Stack:</strong> ActiveMQ is written in Java, while RabbitMQ is implemented in Erlang. This can impact the choice based on the technology stack and preferences within an organization.</li><li><strong>JMS Compliance:</strong> ActiveMQ fully supports JMS, making it a suitable choice for Java-based applications looking for a standard-compliant messaging system. RabbitMQ follows the AMQP standard.</li><li><strong>Community and Ecosystem:</strong> Both ActiveMQ and RabbitMQ have active communities and support ecosystems, but the specific features, extensions, and community support might differ based on individual needs.</li></ol><p>Choosing between ActiveMQ and RabbitMQ often depends on specific use cases, existing technology stacks, protocol preferences, and desired features. Both are robust solutions and can cater well to different messaging requirements in distributed systems.</p><h3>What is best?</h3><p>Choosing between ActiveMQ and RabbitMQ depends on various factors such as specific project requirements, the technology stack in use, desired features, performance considerations, and the familiarity of the development team with each solution. Both ActiveMQ and RabbitMQ are robust message brokers, but they have differences that might influence the decision-making process. Here are some considerations:</p><h3>Choosing the Best:</h3><ul><li><strong>Use Case:</strong> Consider the specific requirements of your project. If your project heavily relies on JMS or requires support for various protocols, ActiveMQ might be a better fit. If you prioritize AMQP or need strong message durability, RabbitMQ could be more suitable.</li><li><strong>Technology Stack:</strong> Consider your existing technology stack and the compatibility of the message broker with your stack. For Java-centric environments, ActiveMQ’s JMS compliance might be advantageous.</li><li><strong>Community and Support:</strong> Assess the available community support, documentation, and ease of finding resources or help for troubleshooting or scaling.</li><li><strong>Performance and Scalability:</strong> Evaluate performance benchmarks, scalability options, and how each broker handles the anticipated message load and throughput.</li></ul><p>Ultimately, there is no one-size-fits-all answer. Both ActiveMQ and RabbitMQ are reputable solutions, and the best choice depends on the specific needs and context of your project, alongside factors like protocol preference, technology stack, required features, and performance considerations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a911509d8cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why constructor injection better than property injection in Spring Framework (Java)?]]></title>
            <link>https://medium.com/@tharindu.dhananjaya/why-constructor-injection-better-than-property-injection-in-spring-framework-java-3dc2dab3071e?source=rss-316a0e909f70------2</link>
            <guid isPermaLink="false">https://medium.com/p/3dc2dab3071e</guid>
            <category><![CDATA[setter-injection]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[property-injection]]></category>
            <category><![CDATA[constructor-injection]]></category>
            <dc:creator><![CDATA[Tharindu Dhananjaya]]></dc:creator>
            <pubDate>Wed, 15 Nov 2023 14:30:15 GMT</pubDate>
            <atom:updated>2023-11-15T14:30:15.635Z</atom:updated>
            <content:encoded><![CDATA[<p>In the context of Spring Framework (a popular Java framework used for building enterprise-level applications), both constructor injection and property injection are ways to perform dependency injection, which is a key feature of the framework.</p><p>Constructor Injection and Property Injection are two different approaches to inject dependencies into a class:</p><ol><li>Constructor Injection: In this approach, dependencies are injected through the class constructor. When the object is created, all required dependencies are passed to the constructor, making them an essential part of the object’s state. The class cannot be instantiated without providing these dependencies. Example;</li></ol><pre>public class MyClass {<br>    <br>    private final Dependency dependency;<br><br>    public MyClass(Dependency dependency) {<br>        this.dependency = dependency;<br>    }<br>}</pre><p>2. Property Injection: Property injection involves setting the dependencies through setter methods or directly assigning them to public fields. Dependencies are set after the object is constructed. Example;</p><pre>public class MyClass {<br>    <br>    private Dependency dependency;<br><br>    public void setDependency(Dependency dependency) {<br>        this.dependency = dependency;<br>    }<br>}</pre><p>Now, regarding why constructor injection is often preferred over property injection:</p><ol><li><strong>Guaranteed Dependencies</strong>: With constructor injection, dependencies are required at the time of object creation. This ensures that the object is always in a valid state, as all required dependencies are provided. It helps in avoiding situations where an object might be in an inconsistent state due to missing dependencies.</li><li><strong>Immutability</strong>: When dependencies are injected through the constructor and marked as final (as in the example above), the class becomes immutable once instantiated. This can lead to better thread safety and easier reasoning about the object’s state.</li><li><strong>Clearer Object Initialization</strong>: Constructor injection makes it explicit which dependencies are necessary for the object to function properly. It’s visible in the constructor’s signature, making the class requirements clearer and more self-documenting.</li><li><strong>Testing and Mocking</strong>: Constructor injection simplifies testing by allowing easier injection of mock dependencies for unit testing. It’s more straightforward to provide mock dependencies through the constructor than through setter methods.</li></ol><p>However, it’s essential to note that both approaches have their use cases, and the choice between them might depend on specific requirements and design preferences. In some scenarios, property injection might be more suitable, especially for optional dependencies or cases where circular dependencies exist.</p><p>In general, for better maintainability, test-ability, and clearer code, many developers prefer constructor injection as it encourages encapsulation, immutability, and clearly defined dependencies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3dc2dab3071e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Async REST APIs using SpringBoot]]></title>
            <link>https://medium.com/@tharindu.dhananjaya/introduction-to-async-rest-apis-using-springboot-36c0a39570ea?source=rss-316a0e909f70------2</link>
            <guid isPermaLink="false">https://medium.com/p/36c0a39570ea</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[response-time]]></category>
            <dc:creator><![CDATA[Tharindu Dhananjaya]]></dc:creator>
            <pubDate>Sat, 13 Nov 2021 14:01:59 GMT</pubDate>
            <atom:updated>2021-11-13T14:01:59.149Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, you will learn how to execute methods asynchronously by using @Async which will impact to reduce API response time.</p><p>Spring comes with @<a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableAsync.html">EnableAsync </a>annotation and can be applied on Spring application classes for asynchronous behavior. This annotation will look for methods marked with @<a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Async.html">Async </a>annotation and run in background thread pools. The @Async annotated methods can return CompletableFuture to hold the result of an asynchronous computation.</p><p>Consider the below scenario;</p><p>Need to expose a rest API, that API will make response by invoking independent multiple services. Which means, this API will invoke multiple remote services internally to make the response with proper results. Those services are independent, and the response of API is a collection of results from those services.</p><p>Basically, what you are doing to fulfill this requirement is;</p><p>· In the service layer, call all the remote services one by one. Fetch the results and generate response.</p><p>· In this approach, your application must wait until one service completes its request-response cycle before going for next remote service. Therefore, your application response time can be higher.</p><h3>Can you reduce the application response time?</h3><p>Let’s see how to reduce the response time with the help of @Async spring boot.</p><p>In here, your application will invoke remote services asynchronously at once and generate the response once all are done. There is no waiting till complete services one by one. So, the response time is low than previous approach.</p><ol><li><strong>Create three APIs to access asynchronous way</strong></li></ol><p>In this demo, I have created a REST API, which gather data from three remote services asynchronously. When responses come from these three services, then aggregate the final response.</p><p>As the following flow chart main application is going to access EmployeeName, EmployeeAddress, EmployeePhone APIs asynchronously.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*SQQLJiAZUsbCsm9ITNvOeQ.png" /></figure><p>See the following code example for these APIs;</p><blockquote>@GetMapping(“/addresses”)<br> public EmployeeAddresses getAddresses() {<br> logger.info(“get addresses Start”);</blockquote><blockquote>EmployeeAddresses employeeAddressesList = new EmployeeAddresses();</blockquote><blockquote>EmployeeAddress employeeAddress1 = new EmployeeAddress();<br> EmployeeAddress employeeAddress2 = new EmployeeAddress();</blockquote><blockquote>List&lt;EmployeeAddress&gt; addressList = new ArrayList&lt;&gt;();<br> employeeAddress1.setHouseNo(“1111”);<br> employeeAddress1.setStreetNo(“111”);<br> employeeAddress1.setZipCode(“111111”);</blockquote><blockquote>employeeAddress2.setHouseNo(“222”);<br> employeeAddress2.setStreetNo(“222”);<br> employeeAddress2.setZipCode(“222222”);</blockquote><blockquote>addressList.add(employeeAddress1);<br> addressList.add(employeeAddress2);</blockquote><blockquote>employeeAddressesList.setEmployeeAddressesList(addressList);<br> <br> return employeeAddressesList;<br> }</blockquote><blockquote>@GetMapping(“/phones”)<br> public EmployeePhone getPhoneNumbers() {<br> logger.info(“get phones Start”);</blockquote><blockquote>EmployeePhone employeePhone = new EmployeePhone();<br> ArrayList&lt;String&gt; phoneNumberList = new ArrayList&lt;String&gt;();</blockquote><blockquote>phoneNumberList.add(“+94712343987”);<br> phoneNumberList.add(“+94772343987”);</blockquote><blockquote>employeePhone.setPhoneNumberList(phoneNumberList);</blockquote><blockquote>return employeePhone;<br> }</blockquote><blockquote>@GetMapping(“/names”)<br> public EmployeeNames getEmployeeName() {<br> logger.info(“get names Start”);</blockquote><blockquote>EmployeeNames employeeNamesList = new EmployeeNames();</blockquote><blockquote>EmployeeName employeeName1 = new EmployeeName();<br> EmployeeName employeeName2 = new EmployeeName();</blockquote><blockquote>List&lt;EmployeeName&gt; employeeList = new ArrayList&lt;EmployeeName&gt;();</blockquote><blockquote>employeeName1.setFirstName(“Kamal”);<br> employeeName1.setLastName(“Vithana”);</blockquote><blockquote>employeeName2.setFirstName(“Namal”);<br> employeeName2.setLastName(“Perera”);</blockquote><blockquote>employeeList.add(employeeName1);<br> employeeList.add(employeeName2);</blockquote><blockquote>employeeNamesList.setEmployeeList(employeeList);</blockquote><blockquote>return employeeNamesList;<br> }</blockquote><p>2. Configure async thread pool</p><p>Need to create Async configuration class, refer following code example for the Async configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*9nnrEWQ6k9CVayH9HViB1Q.png" /><figcaption>AsynchConfiguration.java</figcaption></figure><p>3. Spring @Async controller methods</p><p>The methods that need to run asynchronously need to define with @Async annotation and return type should be <strong>CompletableFuture&lt;T&gt;</strong></p><blockquote><a href="http://twitter.com/Async">@Async</a>(“asyncExecutor”)<br>public CompletableFuture&lt;T&gt; methodOne() {<br> //code<br>}</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/747/1*quClBIPideu8iKb2-rAktA.png" /><figcaption>Asynchronous methods</figcaption></figure><p>4. Call asynchronous methods and aggregate results</p><p>Main application can call these three methods asynchronously. Final response will be sent after retrieve three methods responses.</p><p><strong>CompletableFuture.allOf()</strong> method is used to gather all synchronize methods responses.</p><p>Refer following code example;</p><blockquote>CompletableFuture.allOf(methodOne, methodTwo, methodThree).join();</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*ePu_DKow8yhlz5vhy5NbkA.png" /></figure><p>Try this demo within and without @asnync. You can see significant improvement of response time with @asnync.</p><p>Outputs;</p><ol><li>Without Async</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*c9eM9CkneD2USRH-Rl-URg.png" /><figcaption>without async enable</figcaption></figure><p>2. With Async</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*2tV_gBgbmx0bEnmCfXo53A.png" /><figcaption>with async enable</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36c0a39570ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>