{"id":58711,"date":"2023-09-30T10:29:10","date_gmt":"2023-09-30T04:59:10","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58711"},"modified":"2024-06-10T15:36:00","modified_gmt":"2024-06-10T10:06:00","slug":"aem-service-with-multiple-implementation","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aem-service-with-multiple-implementation\/","title":{"rendered":"AEM Service With Multiple Implementation"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">AEM services are modular components in the AEM framework that encapsulate specific functionality, promoting a highly modular and extensible architecture. They allow multiple implementations, enabling developers to create tailored solutions for different use cases, user roles, devices, or locations. This customization enhances the user experience and increases customer engagement on AEM-based platforms.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">AEM services offer various use case examples, such as an Email Delivery Service for sending transactional emails with multiple implementations, allowing the platform to choose the most suitable email delivery provider. Another use case is the Personalization Service, which enables a retail website to offer personalized product recommendations by experimenting with different algorithms based on user preferences and behavior. These examples showcase how AEM&#8217;s modularity and flexibility enhance functionality and user experience.<\/span><\/p>\n<h2><strong>Prerequisites:<\/strong><\/h2>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Ensure you have an OSGi container or framework set up to deploy and manage your OSGi components and services<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Your project should be configured with Maven to handle dependencies and build your OSGi bundles.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Make sure you have the necessary dependencies to use OSGi Declarative Services (DS) annotations(R8 in my case) in your project.<\/span><\/li>\n<\/ol>\n<h2><strong>Why Multiple Implementations?<\/strong><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><b>Modular Components:<\/b><span style=\"font-weight: 400;\"> AEM services act as modular components, organizing business logic effectively.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Extensibility:<\/b><span style=\"font-weight: 400;\"> Multiple implementations allow addressing specific use cases and adapting to diverse requirements.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Customization:<\/b><span style=\"font-weight: 400;\"> Businesses can customize services for personalized user experiences.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Data-Driven Decisions:<\/b><span style=\"font-weight: 400;\"> A\/B testing with different implementations helps make informed decisions.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Scalability &amp; Maintenance:<\/b><span style=\"font-weight: 400;\"> Modular architecture enhances scalability and simplifies maintenance.<\/span><\/li>\n<\/ul>\n<h2><strong>Accessing a specific service<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Let&#8217;s say we have multiple implementations of a service, and we want to call a particular implementation of the service dynamically. So, let&#8217;s look at an example where we will be making a servlet and trying to access the specific implementation by the search parameter.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">I have created a <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files#diff-215e573451f1fa9ef3fd4c89fe22a90013b1d9a506eb942139b3f291dd29711b\"><span style=\"font-weight: 400;\">SearchImageService<\/span><\/a>,<span style=\"font-weight: 400;\"> which is basically an interface that provides functionality related to searching for images.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Then, we implemented this service with two concrete classes, <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files#diff-7a5de3c2908a72d6b3ea090bc67e675dc0f18ad368cff9a21ad081c80f34fa70\"><span style=\"font-weight: 400;\">SearchDogImageServiceImpl<\/span><\/a><span style=\"font-weight: 400;\"> and <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files#diff-71f85abeef9676c4413451c54f715489bce638a91e5c65291483b31d7307fb67\"><span style=\"font-weight: 400;\">SearchFoxImageServiceImpl<\/span><\/a><span style=\"font-weight: 400;\">, each responsible for fetching random images of dogs and foxes, respectively.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files#diff-4ec70f0bf5b3993f2af8b07c5a597494cad9f07c60c057f13891b51754d0de34\"><span style=\"font-weight: 400;\">SearchImageServlet<\/span><\/a><span style=\"font-weight: 400;\"> uses these service implementations based on user input to fetch and display the appropriate images. This structure allows for modular and extensible image retrieval.<\/span><\/li>\n<\/ul>\n<h2><span style=\"font-weight: 400;\">Codebase:<\/span><\/h2>\n<p><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files\"><span style=\"font-weight: 400;\">Here<\/span><\/a>,<span style=\"font-weight: 400;\"> you will find the full code for the service implementation and other required changes; Please go through the <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1#issuecomment-1732553273\"><span style=\"font-weight: 400;\">conversation<\/span><\/a><span style=\"font-weight: 400;\"> for more details regarding the implementation.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To access a specific service, we use @Reference annotation, which<\/span><span style=\"font-weight: 400;\"> is an annotation in Java that is used to inject OSGi service references into a component. The <\/span><span style=\"font-weight: 400;\">@Reference<\/span><span style=\"font-weight: 400;\"> annotation is used to define a reference to a service.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">So, in my servlet, you can see the Reference annotation somewhat like this.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">@Reference(<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 service = SearchImageService<\/span><span style=\"font-weight: 400;\">.class<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 cardinality = ReferenceCardinality<\/span><span style=\"font-weight: 400;\">.MULTIPLE<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 policy = ReferencePolicy<\/span><span style=\"font-weight: 400;\">.DYNAMIC<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 bind = <\/span><span style=\"font-weight: 400;\">\"bindService\"<\/span><span style=\"font-weight: 400;\">,<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0 \u00a0 \u00a0 unbind = <\/span><span style=\"font-weight: 400;\">\"unbindService\"<\/span>\r\n<span style=\"font-weight: 400;\">)<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Now let us understand one by one what the parameters mean here in layman\u2019s terms.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">let\u2019s imagine you\u2019re a construction manager and the <\/span><span style=\"font-weight: 400;\">@Reference<\/span><span style=\"font-weight: 400;\"> annotation is like a request for tools and workers. Here\u2019s how it translates:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>@Reference<\/b><span style=\"font-weight: 400;\"> = This annotation is used to define a reference to a service. This is like you saying, \u201cI need some tools and workers for a job.\u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>service <\/b><span style=\"font-weight: 400;\">= <em>SearchImageService.class<\/em><\/span><span style=\"font-weight: 400;\"><em>:<\/em> This is the class or interface of the service that you want to reference. This is specifying the type of worker you need. It\u2019s like saying, \u201cI need workers who can use a hammer.\u201d<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>cardinality <\/b><span style=\"font-weight: 400;\">= <em>ReferenceCardinality.MULTIPLE<\/em><\/span><span style=\"font-weight: 400;\"><em>:<\/em> This defines how many instances of the service the component requires. This is like saying, \u201cI might need more than one worker.\u201d It means your construction project can use multiple workers who know how to use a hammer.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>policy <\/b><span style=\"font-weight: 400;\">= <\/span><span style=\"font-weight: 400;\">This defines when the service will be bound. It can be <em>STATIC<\/em> (the service is bound before the component is activated and unbound after it is deactivated) or <em>DYNAMIC<\/em> (the service can be bound or unbound at any time). <\/span><em><span style=\"font-weight: 400;\">ReferencePolicy.DYNAMIC<\/span><\/em><span style=\"font-weight: 400;\"> This is like saying, \u201cWorkers can come and go as they please.\u201d It means that workers can join or leave the project at any time.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>bind <\/b><span style=\"font-weight: 400;\">= <\/span><span style=\"font-weight: 400;\">\u00a0This is the name of the method that will be called when a service is bound to the component.<\/span><span style=\"font-weight: 400;\">&#8220;<em>bindService<\/em>&#8220;<\/span><span style=\"font-weight: 400;\">: This is like saying, \u201cWhen a worker comes to help, they should start by doing the \u2018<em>bindService<\/em>\u2019 task.\u201d It specifies the task to be performed when a worker joins the project.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>unbind <\/b><span style=\"font-weight: 400;\">= <\/span><span style=\"font-weight: 400;\">This is the name of the method that will be called when a service is unbound from the component. <\/span><span style=\"font-weight: 400;\">&#8220;<em>unbindService<\/em>&#8220;<\/span><span style=\"font-weight: 400;\">: This is like saying, \u201cWhen a worker finishes their shift, they should finish by doing the \u2018<em>unbindService<\/em>\u2019 task.\u201d It specifies the task to be performed when a worker leaves the project.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">So in layman\u2019s terms, this line of code is like putting up a sign at a construction site that says, \u201cHelp Wanted: Looking for one or more workers who know how to use a hammer. You can come and go as you please. When you start, do bindService When you finish, do unbindService\u201d And then your construction project waits for workers to come and help out as needed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In my case I\u2019m referencing a service of type <\/span><a href=\"https:\/\/github.com\/abhishekanandttn\/ttndemo\/pull\/1\/files#diff-215e573451f1fa9ef3fd4c89fe22a90013b1d9a506eb942139b3f291dd29711b\"><span style=\"font-weight: 400;\">SearchImageService.class<\/span><\/a><span style=\"font-weight: 400;\">, allowing for multiple instances (MULTIPLE), with a dynamic binding policy (DYNAMIC). The methods for binding and unbinding are \u201c<\/span><span style=\"font-weight: 400;\">bindService<\/span><span style=\"font-weight: 400;\">\u201d and \u201c<\/span><span style=\"font-weight: 400;\">unbindService<\/span><span style=\"font-weight: 400;\">\u201d respectively.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Conclusion\u00a0<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Multiple implementations of a service offer flexibility in software design. They allow for different strategies to accomplish the same task, optimizing for various scenarios. This promotes modularity and loose coupling, as components interact with the service interface without knowing the underlying implementation details. This makes the system more maintainable and scalable.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">References<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">https:\/\/mvnrepository.com\/artifact\/org.osgi\/osgi.annotation\/8.0.0<\/span><\/p>\n<p>https:\/\/experienceleague.adobe.com\/docs\/experience-manager-learn\/cloud-service\/underlying-technology\/introduction-osgi.html?lang=en<\/p>\n<p><span style=\"font-weight: 400;\">https:\/\/mvnrepository.com\/artifact\/org.osgi\/osgi.annotation\/8.0.0<\/span><\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"ryo9HuLV0c\"><p><a href=\"https:\/\/www.tothenew.com\/blog\/access-multiple-implementation-of-an-osgi-service-from-a-sling-servlet-dynamically\/\">Access multiple implementation of an OSGi service from a Sling servlet dynamically<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Access multiple implementation of an OSGi service from a Sling servlet dynamically&#8221; &#8212; TO THE NEW Blog\" src=\"https:\/\/www.tothenew.com\/blog\/access-multiple-implementation-of-an-osgi-service-from-a-sling-servlet-dynamically\/embed\/#?secret=ryo9HuLV0c\" data-secret=\"ryo9HuLV0c\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>AEM services are modular components in the AEM framework that encapsulate specific functionality, promoting a highly modular and extensible architecture. They allow multiple implementations, enabling developers to create tailored solutions for different use cases, user roles, devices, or locations. This customization enhances the user experience and increases customer engagement on AEM-based platforms. AEM services offer [&hellip;]<\/p>\n","protected":false},"author":1600,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":297},"categories":[5868],"tags":[5471],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58711"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1600"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58711"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58711\/revisions"}],"predecessor-version":[{"id":59066,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58711\/revisions\/59066"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}