#1
After setting up your centralized monitoring dashboard, the next step is to make it proactive.
You want to receive alerts automatically whenever a scheduled job fails, instead of checking the dashboard manually.
This tutorial shows how to send email alerts and Slack notifications from Spring Boot when Firebird maintenance jobs fail.

1. Add Email Configuration

First, configure the email sender in application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your.email@gmail.com
spring.mail.password=your-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
alert.email.to=admin@yourcompany.com
Make sure to use an App Password if you’re using Gmail.

2. Create Email Service

Implement a simple email alert service:
@Service
public class EmailAlertService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${alert.email.to}")
    private String alertRecipient;

    public void sendFailureAlert(String jobName, String errorMessage) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(alertRecipient);
        message.setSubject("🚨 Job Failed: " + jobName);
        message.setText("The job '" + jobName + "' failed with error:\n" + errorMessage);
        mailSender.send(message);
    }
}
This utility class sends out a quick alert whenever a job throws an exception.

3. Update Maintenance Job Logic

Now modify your job scheduler to trigger the alert when a failure occurs.
@Component
public class MaintenanceScheduler {

    @Autowired
    private MaintenanceLogRepository logRepo;

    @Autowired
    private AuditLogRepository auditRepo;

    @Autowired
    private EmailAlertService alertService;

    @Scheduled(cron = "0 0 2 * * SUN")
    public void cleanAndArchiveLogs() {
        try {
            auditRepo.archiveOldLogs();
            auditRepo.deleteOldLogs();

            logRepo.save(new MaintenanceLog("Cleanup Job", "SUCCESS",
                    LocalDateTime.now(), "Logs archived and deleted."));
        } catch (Exception e) {
            logRepo.save(new MaintenanceLog("Cleanup Job", "FAILED",
                    LocalDateTime.now(), e.getMessage()));

            // Trigger alert
            alertService.sendFailureAlert("Cleanup Job", e.getMessage());
        }
    }
}
Now, whenever a maintenance job fails, an email will be sent automatically to the admin.

4. (Optional) Add Slack Integration

If your team uses Slack, you can push alerts there too.
Create a Slack webhook in your workspace and add this property:
alert.slack.webhook=https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX
Then add a quick Slack alert service:
@Service
public class SlackAlertService {

    @Value("${alert.slack.webhook}")
    private String webhookUrl;

    public void sendSlackMessage(String jobName, String errorMessage) {
        String payload = "{\"text\": \"🚨 Job Failed: " + jobName + "\\nError: " + errorMessage + "\"}";
        try {
            HttpClient.newHttpClient().send(
                HttpRequest.newBuilder(URI.create(webhookUrl))
                .POST(HttpRequest.BodyPublishers.ofString(payload))
                .header("Content-Type", "application/json")
                .build(),
                HttpResponse.BodyHandlers.ofString()
            );
        } catch (Exception ignored) {}
    }
}
You can combine this with the email alert for maximum visibility.

5. Add Alert History

To track alerts, extend the MAINTENANCE_LOG table with an alert column:
ALTER TABLE MAINTENANCE_LOG ADD COLUMN ALERT_SENT BOOLEAN DEFAULT FALSE;
Update your code to mark alerts that were already sent, preventing duplicates.
#ads

image quote pre code