Adding a Service Monitor
Implement SQL-based monitoring queries to detect transit service disruptions
This module implements a monitoring system that uses SQL queries to detect transit service disruptions such as delayed vehicles, route bunching, and offline vehicles.
Transit Service Monitoring
An effective transit monitoring system detects potential issues by analyzing the current state of the fleet. The monitoring service checks for several key conditions:
Delayed vehicles: Vehicles that have been stopped for too long
Vehicle bunching: Multiple vehicles on the same route too close together
Low route coverage: Routes with fewer than the minimum required vehicles
Offline vehicles: Vehicles that haven't reported their position recently
These conditions help operators identify service disruptions before they significantly impact passengers.
Understanding the Monitor Service
Open the MonitorService class:
open src/main/java/com/example/transit/service/MonitorService.javaThe class contains configuration and tracking components:
// Thresholds for monitoring conditions
private static final int STOPPED_THRESHOLD_MINUTES = 5;
private static final int BUNCHING_DISTANCE_KM = 1;
private static final int MINIMUM_VEHICLES_PER_ROUTE = 2;
private static final int OFFLINE_THRESHOLD_MINUTES = 15;
// Monitoring infrastructure
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final IgniteClient client;
// Statistics tracking
private final Map<String, AtomicInteger> alertCounts = new HashMap<>();
private final List<ServiceAlert> recentAlerts = new ArrayList<>();The service starts with startMonitoring():
The performMonitoring method runs each check:
Monitoring Delayed Vehicles
The delayed vehicles check:
This method executes an SQL query that finds vehicles with a "STOPPED_AT" status that have been stopped longer than the threshold time, then creates and records alerts for each match.
Understanding the Query Structure
The query uses a subquery with a JOIN to find the latest position for each vehicle:
The subquery finds the latest timestamp for each vehicle. The JOIN retrieves only the most recent position data. The WHERE clause filters for stopped vehicles exceeding the threshold. TIMESTAMPDIFF calculates the duration in minutes between the vehicle's last report and the current time.
Service Alerts
The monitoring service creates ServiceAlert objects to represent detected issues. Open the class:
Each alert captures the alert type (e.g., "DELAYED_VEHICLE"), a human-readable message, related route and vehicle IDs, geographic coordinates, a severity value, and the timestamp when the alert was created.
Running the Service Monitor Example
Run the example:
Open the example:
The example connects to the cluster, verifies data exists, creates and starts the monitoring service, schedules periodic statistics output, and waits for user input to stop.
The core part:
Expected output:
Other Monitoring Conditions
The MonitorService class implements several other monitoring conditions, each with a specialized SQL query:
Vehicle Bunching
This query finds the latest position for each in-transit vehicle, self-joins to find pairs on the same route, calculates the distance between them in kilometers, and filters for pairs closer than the threshold distance.
Low Route Coverage
This query finds recent positions, groups by route, counts vehicles per route, and filters for routes below the minimum vehicle threshold.
Offline Vehicles
This query identifies routes with some active vehicles, then finds vehicles on those routes that haven't reported in longer than the threshold. Checking only active routes avoids false positives for routes that are simply not running.
Next Steps
This module implemented a monitoring system that continuously checks for service disruptions including delayed vehicles, vehicle bunching, low route coverage, and offline vehicles. The system tracks alert statistics and provides a foundation for operator notifications.
The monitoring system transforms the transit application from a passive data collection system into an active monitoring tool.
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

