Tuesday, February 4, 2020

EVENT LISTNER & HANDLERS:


When click on a page sub-pages will open --->internal servlet is called by granite.js-- Approchable for 3rd party.

What if i want some functionalities internally??

Eg: When  a page is created i want to set few properties automatically.


When to use??
To assign some default functionalities when some event occurred.


UseCase::

-->When  a node is created felix console throws some event (Internally :: 'A node created' event is created by event manager)


-->Whiteboard pattern is a memory in aem

When we load  a bundle  it goes into active state. What happen  in this process??
All components in the bundle will get activated. At time of Activation.
Anything with @Component is a singleton, So Object is created  for that component.

While creating that Object.
If it is a servlet, in memory it created  a list of servlets.
If it is a Event listener, in memory it created  a list of Event listener.
If it is a Event handler, in memory it created  a list of Event handler.
If it is a Workflow, in memory it created  a list of Workflow.

It will separate/Categorize --> This is called whiteboard pattern.

Whiteboard pattern is a design pattern implemented by apache felix.

In a nutshell: Whiteboard pattern brings the objects into the  memory & Segregate them.

Eg:  In Jcr  any small changes then triggers an event like -->craete node+save triggers an event  & felix console catches the event. Now felix will go to whiteboard  & check for the eventListener.

Event-- Event Listener binding/registering/tagging is done by ObservationManager.





If it is registered then it executes that class.


Case2:
:: @Reference -->While code execution it will search for that singleton object -->goes to the registered service object  & executes it.

-->
All this happens  in runtime
1. Whiteboard pattern
2.EventListener pattern.
3.Singleton Pattern



Q) What happens when  a singleton object is requested by multiple events??
A) for Each call a thread is created.




EVENT LISTENER:


Adv:

Its very fast in repository level events & single level nodes.
Eg:tag remove or add.

Dis Adv:


1. When a node is created with automatic child nodes also created then event listener will applied child nodes also(eg: page is created -> event is triggered 2 times).

2. When dealing with more nodes say 5000 nodes then 5000 event will be triggered which consumes a lot  of time & memory . System will hang.

3. Its In-memory, When restared there  might be a data loss so UN RELIABLE.









-->EventListener class should implement EventListener.
















--> get ListenerSession when active is called.

--> From Session-->get workspace -->get observation manager (in repository i.e in JCR  level).
For this observation manager  add an EventListener.





























When create a tag it is stored as cq:tags string[] namespace:tag













Here comes in Event Handler

Event Handler::

-->Based on node type event will be triggered.

For page -->event will be triggered single time.

For Asset -->event will be triggered single time.

--> Better than Event Listener but still expensive.






--> Doesn't have observation manager but works on EVENT_TOPIC

-->EventConstansts: We can manually  register our own events.

--> EVENT_TOPIC : implemented by sling-->to bind/register  to events---> Here  Dam & page events



--> One event can be converted in to another by formEvent()--->take the details  in one event & converts into another



















There are some functionalities which should be executed even if system hangs out (renditions etc)then the savior is:

Thursday, January 30, 2020

Integrating SOLR with Adobe Experience Manager


How to integrate AEM and SOLR so that an AEM component can use SOLR to perform searches, as shown in the following illustration. 


















The following list compares developing an external search platform using Solr to Oak indexing. The following are benefits:

• Full control over the Solr document model
• Control over boosting specific fields in Solr document
• Real time indexing is within your control
• Comes handy when multiple heterogeneous systems are contributing for indexing.


Create project from scratch

·         Project creation using eclipse plugin.
·         Project creation using maven – archetype.
·         Deployment in AEM instance.

Create project using eclipse:

Download the eclipse from below location.

https://github.com/Adobe-Marketing-Cloud/aem-project-archetype

Properties to be remember while creating AEM Projects.


Create Project using maven archetype:

Archetype creates a minimal Adobe Experience Manager project as a starting point for your own projects. The properties that must be provided when using this archetype allow to name as desired all parts of this project.





Maven command to run the project:
mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeGroupId=com.adobe.granite.archetypes -DarchetypeArtifactId=aem-project-archetype -DarchetypeVersion=13 -DarchetypeCatalog=https://repo.adobe.com/nexus/content/groups/public/


Deployment in AEM instance

Provided Maven profiles

autoInstallBundle: Install core bundle with the maven-sling-plugin to the felix console.

autoInstallPackage: Install the ui.content and ui.apps content package with the content-package-maven-plugin to the package manager to default author instance on localhost, port 4502. Hostname and port can be changed with the aem.host and aem.port user defined properties.

autoInstallPackagePublish: Install the ui.content and ui.apps content package with the content-package-maven-plugin to the package manager to default publish instance on localhost, port 4503. Hostname and port can be changed with the aem.host and aem.port user defined properties.

Go to the project source parent folder, and execute one of the below command on command prompt:
1.      >mvn clean install

It will compile the full project but would not deploy on your instances.

Below files should be manually upload to package manager and install.
/ui.apps/target/***.zip
/ui.content/target/***.zip

2.      >mvn clean install -PautoInstallPackage -Padobe-public
Note: Check the pom.xml file for author port.

3.      >mvn clean install -PautoInstallBundle
Auto install the bundle only.

4.      >mvn clean install -PautoInstallPackagePublish -Padobe-public
Note: Check the pom.xml file for author port.

Archetype provides the following modules –>

Core : Core bundle (java code goes here)
it.launcher - Baseline bundle to support Integration Tests with AEM
it.test - Integrations tests
ui.apps - Module for your components,template etc code.
ui.content - Project sample/test content or may be actual content (actual content in codebase is not a good practice)

Notes:
In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.
POM is fundamental unit of maven which resides in the root directory of your project and contains the information about the project and various configuration details used by maven to build the project. So before creating a project , we should first decide the project group (groupId), artifactId and its version as these attributes help in uniquely identifying the project in maven repository.



Create Solr_search component.




Create Java files and what is the use each one for implementing solr-facet search in aem.



go to core.

SolrSearchService (a Java interface)



SOLRSEARCHSERVICE INTERFACE


The SolrSearchService interface describes the operations exposed by this service. The following Java code represents this interface.

SOLRSERVERCONFIGURATION INTERFACE


The SolorServerConfiguration interface descibes operations exposed by this configuration service. The following Java code represents this interface. 

Creating a workflow for indexing.








After activating the page then Custom process step calls the index .



Build the OSGi bundle using Maven



To build the OSGi bundle by using Maven, perform these steps:
  1. Open the command prompt and go to aem-solr-article.
  2. Run the following maven command: mvn clean install.
  3. The OSGi component can be found in the following folder: aem-solr-article\core\target. The file name of the OSGi component is solr.core-1.0-SNAPSHOT.jar.
  4. Install the OSGi using the Felix console.

Setup the Solr Server


Download and install the Solr server (solr-6.2.0.zip ) from the following URL:
then create a new core.


Configure AEM to use Solr Server


Configure AEM to use Solr server. Go to the following URL:
Search for AEM Solr Search - Solr Configuration Service and enter the following values:
  • Protocol - http
  • Solr Server name - localhost
  • Solr Server Port: 8983
  • Solr Core Name - collection (references the collection you created)


Select core from the drop-down control and select Query. Then click the Execute button. If successful, you will see the result set.



View Solr results in an AEM component


In CRXDE lite, open the following HTML file:
/apps/solr/components/content/solrsearch/solrsearch.html
and add the following script at the top of the page:
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script>  
To access the component that display Solr values, enter the following URL: 





Indexing JSON data


Let’s see how we can index the JSON data in Solr




We need to follow 2 steps

1)Define the description of the fields of the new JSON data
2)Publish the data to Solr.

Let’s use books.json file provided by solr itself for indexing the JSON data.


books.json is available inside solr-6.2.0\example\exampledocs

Let’s add below fields description in the schema.xml file(solr-6.2.0\server\solr\MyCore\conf) after the < uniqueKey>id tag

<!-- Fields added for indexing books.json file-->
 <field name="cat" type="text_general" indexed="true" stored="true"/>
 <field name="name" type="text_general" indexed="true" stored="true"/>
 <field name="price" type="tdouble" indexed="true" stored="true"/>
 <field name="inStock" type="boolean" indexed="true" stored="true"/>
 <field name="author" type="text_general" indexed="true" stored="true"/>

If we observe the fields in the books.json file, we can see that 10 fields are available inside this file.But we have provided only 5 fields description in the schema.xml file.
What happens to other fields? Will they be indexed?
Yes, the other fields will also be indexed but how?
The id field in the books.json file will be taken care by the uniqueKey element of schema.xml file for indexing < uniqueKey>id< /uniqueKey>
The other 4 fields will also be indexed using the dynamicField tag in the schema.xml.

Now let’s post the data to the Solr to index it

Lets navigate to the below path in command prompt solr-6.2.0\example\exampledocs
run the below command
java -Dtype=text/json -Durl=http://localhost:8983/solr/MyCore/update -jar post.jar  books.json
Since it’s a java command, we can pass run time arguments using –D We are passing 2 java run time arguments here

–Dtype – Specifyies the type of the file like CSV,XML,JSON etc, we are passing JSON as our publishing data is of JSON type.
-Durl -> URL of the Core under which indexing has to happen
We can see that Solr server has indexed the file and committed the indexed data in MyCore and displayed the following output in command prompt.
Access the below url now and check the statistics of indexed data
http://localhost:8983/solr/#/MyCore
We can observe that Num Docs displays no of records which are indexed.
Since we have 4 records in books.json file, all these records are indexed and hence Num Docs displays 4.
Access indexed data We can access the indexed data directly in the Admin console of Solr without any condition.
Access the below url now http://localhost:8983/solr/#/MyCore
Select MyCore and click on query option
Now click on execute query