Skip to Content

Implement a Notification Application for Monitoring Java Apps

test
0 %
Implement a Notification Application for Monitoring Java Apps
Details

Implement a Notification Application for Monitoring Java Apps

December 9, 2020
Created by
December 1, 2020
Create an application that notifies you of critical metrics via e-mail or SMS, using the SAP Cloud Platform Monitoring service.

You will learn

  • How to configure a custom application to notify you of critical metrics of Java applications via e-mail or SMS.
  • How to use the custom application to take self-healing actions by restarting the specified Java applications running on SAP Cloud Platform.
QR code

Prerequisites

In this tutorial, you implement a notification application that requests the metrics of the following Java applications (running on SAP Cloud Platform):

  • app1 located in a1 subaccount and Europe (Rot/Germany) region

  • app2 located in a2 subaccount and US East region

Since the requests are sent to only two applications, the Maven project that you import in Eclipse only spawns two threads. However, you can change this number in the MetricsWatcher class, where the ScheduledThreadPoolExecutor(2) method is called. Furthermore, if you decide to change the list of applications, you also need to correct the list in the Demo class of the imported project.

When the notification application receives the Java application metrics, it checks for critical metrics. The application then sends an e-mail or SMS, depending on whether the metrics are received as critical once or three times. In addition, the notification application restarts the Java application when the metrics are detected as critical three times.

To learn more about the whole scenario, read the Notification Scenario blog post.

If a Java application isn’t started yet, the notification application automatically triggers the start process.


Step 1: Download the project

Download the cloud-metricswatcher project as a ZIP file from https://github.com/SAP/cloud-metricswatcher.

Log on to answer question
Step 2: Import into Eclipse

Extract the files into a local folder and import the folder in Eclipse as an existing Maven project.

You can also upload your project by copying the URL from GitHub and pasting it as a Git repository path or URI after you switch to the Git perspective. Remember to switch back to a Java perspective afterward.

How did you import the folder in Eclipse?
×
Step 3: Update Demo.java

Open the Demo.java class and update the following information:

  • Your e-mail

  • Your SMS

  • Your logon credentials

  • Your Java applications, and their subaccounts and regions

String mail_to = "my_email@email.com";
String mail_to_sms = "my_email@sms-service.com";


private final String auth_user = "my_user";
private final String auth_pass = "my_password";


String landscapeFqdn1 = "api.hana.ondemand.com";
String account1 = "a1";
String application1 = "app1";

String landscapeFqdn2 = "api.us1.hana.ondemand.com";
String account2 = "a2";
String application2 = "app2";
What parameters have you set the values for in the `Demo.java` class?
×
Step 4: Update `Mailsender.java`

Open the Mailsender.java class and update your e-mail account settings.

private static final String FROM = "my_email_account@email.com";
final String userName = "my_email_account";
final String password = "my_email_password";

public static void sendEmail(String to, String subject, String body) throws AddressException, MessagingException {
            // Set up the mail server
            Properties properties = new Properties();
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.starttls.enable", "true");
            properties.setProperty("mail.smtp.port", "587");
            properties.setProperty("mail.smtp.host", "smtp.email.com");
            properties.setProperty("mail.smtp.host", "mail.email.com");

}
What parameters have you set the values for in the `Mailsender.java` class?
×
Step 5: Test your scenario
  1. Open the SAP Cloud Platform cockpit and find your Java applications.

  2. Configure your Java applications to return critical metrics.

    To do this, you can create a JMX check with a low critical threshold for HeapMemoryUsage so that the check is always received in a critical state.

    For more information, see create-jmx-check.

    Example:

    neo create-jmx-check -a mysubaccount -b demo -u p1234567 -n "JMX Check Test - Heap Memory" -O java.lang:type=Memory -A HeapMemoryUsage -K used -U B -C 20000000 -h hana.ondemand.com
    

    To use the console commands, you need to set up the console client. For more information, see Set Up the Console Client.

  3. Run your notification application in Eclipse and check the following:

    • You receive an e-mail with subject A metric has reached a critical state. and body Metric HeapMemoryUsage for application app1 has reached а critical state. when a critical metric is received.

    • You receive an SMS with text Metric HeapMemoryUsage for application app1 has reached critical state 3 times. The application will be restarted. when a critical metric is received three times.

    • Your Java application is restarted when its critical metric is received three times.

You can check the status of your Java applications in the SAP Cloud Platform cockpit.

What have you verified for the dashboard application?
×

Next Steps

Back to top