AEMaaCS - AEM launcher is not working because it is getting initiated for the first replication and it is not sending any notification on next times.
So I tried using Event handler (EVENT.TOPIC), it is sending the notification to the business. But problem here is it is triggering email multiple times(2-3 times).
Onevent it is triggering the workflow where it has Messagegatewayservice with all hardcoded input(straight forward htmlemail value passing to the message service)
Can you please help me on this? Thanks in Advance
Event Handler:
Workflow:
Solved! Go to Solution.
Views
Replies
Total Likes
Hi, the workflow model you will create on page publish will get trigger every time you publish the page,
So to differentiate the first page publish, you can use an identifier. I have summarized the steps below.
1. Create a custom workflow model that gets triggered on page publish.
2. Create a workflow process class as you have created above
(
service = WorkflowProcess.class,
property = {
"process.label=New Page Publish Email Notification"
}
)
public class NewPagePublishEmailWorkflowProcess implements WorkflowProcess {
private MessageGatewayService messageGatewayService;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
String pagePath = workItem.getWorkflowData().getPayload().toString();
ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
Page page = resolver.getResource(pagePath).adaptTo(Page.class);
// Check if the page is being published for the first time
if(!page.getProperties().containsKey("mailTriggered")){
sendEmail();
page.getProperties().put("mailTriggered", false);
resolver.commit();
}
}
private void sendEmail() {
try {
Email email = new SimpleEmail();
email.addTo("business@example.com");
email.setSubject("A new page has been published");
email.setMsg("A new page has been published on the website.");
MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
messageGateway.send(email);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Configure the workflow model with above Workflow process class.
@dmanibabu You can try below code snippet in case of page publish. But, launcher has one problem as it gets call multiple times. In place launcher we can call event listener to listen particular event on property value modified. It will also allow us to get topic as replication with resource.
From listener we can call our send email workflow model.
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
jcr:primaryType="cq:WorkflowLauncher"
condition="cq:lastReplicated"
conditions="[]"
description="\0"
disabledFeatures="[]"
enabled="{Boolean}true"
eventType="{Long}16"
excludeList="\0"
features="[]"
glob="/content/practice"
nodetype="cq:PageContent"
runModes="author,publish"
workflow="/var/workflow/models/send-mial-workflow"/>
@Imran__Khan As you said launcher is triggering the workflow only for the first time of page publish. it is not looking for second time when page is published.
Our requirement is whenever page is published under specific path, we have to send mail to business.
Event handler I am using it to look for event . For event listener, do you have any reference code snippet. I am not sure listener wont trigger workflow multiple times.
Please follow below links on the same. It talks about event listeners and email template step by step. Code snippet is also available to consume.
https://javadoubts.com/aem-event-listener/
Replication Event : https://github.com/arunpatidar02/aem63app-repo/blob/master/java/ReplicationListener.java
Email Template : https://aemlab.blogspot.com/2019/06/aem-custom-templated-email.html
@arunpatidar Tried this but still I am receiving same email more than 3times. Not sure why Mail is triggering multiple times.
Hi,
Can you please check how many events are triggered when you publish the page?
http://localhost:4502/system/console/events
@dmanibabu Agree, it will get call multiple times. Please apply check resource path and resource type in Java class for which we want to send email. This will make sure email get send one time.
Checking property and node type will not help here as while page publishing it will also publish other references content.
@Imran__Khan yes I do have the resource path after the replication action, but still it is getting multiple times. please refer screenshot
@arunpatidar I tried in local but problem here is I don't have mail setup in my local. So I am trying it in development instance. In local it is triggering multiple times because of missing mail config.
Also the job is keep in retrying it, till it get success.
can we make this to execute only once?
Hi @dmanibabu
Technically it is possible to retry only once but is it not good to retry until succeed?
Hi, the workflow model you will create on page publish will get trigger every time you publish the page,
So to differentiate the first page publish, you can use an identifier. I have summarized the steps below.
1. Create a custom workflow model that gets triggered on page publish.
2. Create a workflow process class as you have created above
(
service = WorkflowProcess.class,
property = {
"process.label=New Page Publish Email Notification"
}
)
public class NewPagePublishEmailWorkflowProcess implements WorkflowProcess {
private MessageGatewayService messageGatewayService;
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
String pagePath = workItem.getWorkflowData().getPayload().toString();
ResourceResolver resolver = workflowSession.adaptTo(ResourceResolver.class);
Page page = resolver.getResource(pagePath).adaptTo(Page.class);
// Check if the page is being published for the first time
if(!page.getProperties().containsKey("mailTriggered")){
sendEmail();
page.getProperties().put("mailTriggered", false);
resolver.commit();
}
}
private void sendEmail() {
try {
Email email = new SimpleEmail();
email.addTo("business@example.com");
email.setSubject("A new page has been published");
email.setMsg("A new page has been published on the website.");
MessageGateway<Email> messageGateway = messageGatewayService.getGateway(Email.class);
messageGateway.send(email);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. Configure the workflow model with above Workflow process class.
@dmanibabu Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies