-
Notifications
You must be signed in to change notification settings - Fork 44
E mail Scriptlet
This example shows how to implement a flexible email notification system based on the scriptlet processor (gsn.processor.ScriptletProcessor). Thanks to the script, we can easily set and update the condition for sending an email as well as the email content itself. Prior to use this example, please configure the access to your SMTP account, as shown below and then set the recipient address in the script.
The SMTP configuration is set in the file
conf/emails.properties
An example of configuration, using a GMAIL SMTP account is shown below:
# This file contains the properties for configuring the acces to a SMTP server in order to send emails.
# See the JavaMail API for constants for details -- http://java.sun.com/products/javamail/javadocs/index.html
# Configuration example for using the GMAIL SMTP server.
[email protected]
mail.smtp.password=**************
mail.smtp.host=smtp.gmail.com
[email protected]
mail.smtp.starttls.enable=true
mail.smtp.auth=true
Send an email to one or many recipients. Return true if the message has been succesfully sent, false otherwise.
boolean sendEmail(ArrayList<String> to, String object, String message);
This virtual sensor gets its data from the MemoryMonitorVS and sends a new email each time the MemoryMonitorVS.HEAP field reached a new maxima.
<virtual-sensor name="EmailScriptlet" priority="10">
<processing-class>
<class-name>gsn.processor.ScriptletProcessor</class-name>
<init-params>
<param name="persistant">false</param>
<param name="scriptlet">
<![CDATA[
// This script sends an email to alert that the heap value (from the local wrapper)
// has reached a new maxima.
// Please set your SMTP configuration (in the conf/emails.properties) prior to use this script.
// Initialize the stateful variable maxima
if ( ! isdef('maxima'))
maxima = 0;
// Comparing the new heap size with the maxima
if (maxima < HEAP) {
maxima = HEAP;
// Notify by email
def emailTitle = "GSN Notification - New HEAP maxima reached: " + HEAP;
def emailContent = "Warning, the HEAP maxima has been reached: " + HEAP;
def recipients = ["[email protected]", "[email protected]", "[email protected]"]; // Define one or more recipients
sendEmail(recipients, emailTitle, emailContent); // note that the sendMail method is, by default, imported statically to the script.
}
]]>
</param>
</init-params>
<output-structure/>
</processing-class>
<description>
This Virtual Sensor demonstrates the use of the scriplet processor (gsn.processor.ScriptletProcessor) for email
notifications. It gets its data from the MemoryMonitorVS and sends a new email each time the MemoryMonitorVS.HEAP field reached a new maxima.
</description>
<addressing/>
<storage history-size="1"/>
<streams>
<stream name="stream1">
<source alias="source1" storage-size="1" sampling-rate="1">
<address wrapper="local">
<predicate key="name">MemoryMonitorVS</predicate>
</address>
<query>select * from wrapper</query>
</source>
<query>select * from source1</query>
</stream>
</streams>
</virtual-sensor>