How to send email based on listener outputs in AEM?
I've got this listener which listens to any changes including addition, modification and deletion for nodes under content directory. A mail has to be send to some mail address whenever the listener logs a change. How to work around that?
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventListener;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import com.day.cq.mailer.MessageGatewayService;
import org.osgi.service.component.ComponentContext;
import javax.jcr.observation.EventIterator ;
@Component(immediate=true,
service= EventListener.class)
public class ContentChangeListener implements EventListener{
private Session adminSession;
@Reference
org.apache.sling.jcr.api.SlingRepository repository;
@Reference
private MessageGatewayService messageGatewayService;
@Activate
public void activate(ComponentContext context) throws Exception {
System.out.println("Started Listener");
try {
adminSession = repository.loginService("testing",null);
adminSession.getWorkspace().getObservationManager().addEventListener(
this, Event.PROPERTY_ADDED|Event.NODE_ADDED|Event.NODE_MOVED|Event.NODE_REMOVED|Event.PROPERTY_CHANGED|Event.PROPERTY_REMOVED,
"/content/silversea/en/homepage", true, null, null, false);
} catch (RepositoryException e){
System.out.println("unable to register session"+e);
}
}
@Deactivate
public void deactivate(){
if (adminSession != null){
adminSession.logout();
}
}
public void onEvent(EventIterator eventIterator) {
try {
while (eventIterator.hasNext()){
System.out.println("Something has been added : {}"+ eventIterator.nextEvent().getPath());
}
} catch(RepositoryException e){
System.out.println("Error while treating events"+e);
}
}
}