Configuring notifications

October 8, 2024

This topic shows how to use Apache Camel to route notifications and call them in actions, and send mail to users when an action is performed.

About notifications

For notifications Jahia now uses Apache Camel.

Camel uses routes to define how your messages should be handled across your network. Jahia provides you two ways to register your routes from Spring inside the applicationcontext-camel.xml file or using the API through the CamelNotificationService.

By default Jahia provides a source to notify users through mails named seda:users?multipleConsumers=true you need to use the full URI to refer to it.

The CamelNotificationService allows you to call any route and send a message to this route.

Map<String, Object> map = new HashMap<String, Object>();
map.put("To", MailService.getInstance().getSettings().getTo());
map.put("From", MailService.getInstance().getSettings().getFrom());
map.put("Subject", "Camel rocks");

String body = "Hello.\nYes it does.\n\nRegards.";

camelNotificationService.sendMessagesWithBodyAndHeaders("seda:users?multipleConsumers=true",body,map);

There is also a shortcut to send a mail directly :

camelNotificationService.sendMail("seda:users?multipleConsumers=true","Camel Rocks",
  "<html><body><h1>Camel Rocks</h1><p>Camel Complex Mail Test</p></body></html>",
  "Camel Complex Mail Test", MailService.getInstance().getSettings().getFrom(),
  MailService.getInstance().getSettings().getTo(),null,null);

This way the notification system is more like a message system, now in your modules you can use this service to register your own routes and call them in your action or just use the default ones to send mails to users when an action is performed.

We provide consequences for the rules system that allows to send mails.

rule "notify user (create)"
    when
       A new node is created
                - the node has the type jnt:user
                - its name is not guest
                - its name is not root
    then
        Notify new user with mail template "/notifications/templates/mail/newUser.vm"
end

Sending Mail with templates

You can use a template written with your preferred script language to design your mails, each mail will use two templates. One for the body and one for the subject.

For example in the Jahia Form Builder modules we define a MailAction that will send the submitted form by mail. We have defined a mail template in /action/mail/body.vm for the body and jahia will assumed the subject template to be named /action/mail/body.subject.vm (same name, same extension).

Here how to send the mail from you action (extract from MailAction):

//Define where to send the mail
String toMail = UserPreferencesHelper.getEmailAddress(to);

//Define objects to be bound with the script engine to evaluate the scripts
//Same bindings for body and subject
Map<String,Object> bindings = new HashMap<String,Object>();
bindings.put("formDatas",formDatas);
bindings.put("formNode",node.getParent());
bindings.put("submitter",renderContext.getUser());
bindings.put("date",new DateTool());
bindings.put("submissionDate", Calendar.getInstance());
bindings.put("locale", resource.getLocale());

// Send the mail to the specified address from the account defined in the Jahia Settings
camelNotificationService.sendMailWithTemplate(mailTemplatePath,bindings,toMail, MailService.getInstance().getSettings().getFrom(),
                                              null,null,resource.getLocale(), "Jahia Form Builder");