backbone-queue 2.7.36

Backbone Framework Queue Module - Redis, SQS, and RabbitMQ support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! RabbitMQ Queue Examples
//!
//! This example demonstrates comprehensive RabbitMQ usage patterns including:
//! - Basic message publishing and consuming
//! - Different exchange types (Direct, Fanout, Topic, Headers)
//! - Message routing and filtering
//! - Publisher confirms and acknowledgments
//! - Dead letter exchanges and error handling
//! - Performance tuning and configuration options
//!
//! Run with: cargo run --example rabbitmq_examples
//!

use backbone_queue::{
    rabbitmq_simple::{RabbitMQQueueSimple, RabbitMQConfig, ExchangeType},
    traits::QueueService,
    types::{QueueMessage, QueuePriority},
    utils::rabbitmq_simple::*,
};
use std::collections::HashMap;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    env_logger::init();

    println!("🐰 RabbitMQ Queue Examples\n");
    println!("========================\n");

    // Example 1: Basic Direct Exchange
    println!("đŸ“Ŧ Example 1: Basic Direct Exchange");
    basic_direct_exchange_example().await?;
    println!();

    // Example 2: Fanout Exchange Broadcasting
    println!("đŸ“ĸ Example 2: Fanout Exchange Broadcasting");
    fanout_exchange_example().await?;
    println!();

    // Example 3: Topic Exchange Pattern Matching
    println!("đŸŽ¯ Example 3: Topic Exchange Pattern Matching");
    topic_exchange_example().await?;
    println!();

    // Example 4: High Priority Messages
    println!("⚡ Example 4: Priority Message Handling");
    priority_message_example().await?;
    println!();

    // Example 5: Batch Operations
    println!("đŸ“Ļ Example 5: Batch Operations");
    batch_operations_example().await?;
    println!();

    // Example 6: Message with Headers
    println!("đŸˇī¸ Example 6: Messages with Custom Headers");
    message_with_headers_example().await?;
    println!();

    // Example 7: Error Handling and Validation
    println!("đŸ›Ąī¸ Example 7: Configuration Validation");
    validation_example().await?;
    println!();

    // Example 8: Health Monitoring
    println!("💓 Example 8: Queue Health Monitoring");
    health_monitoring_example().await?;
    println!();

    println!("✅ All examples completed successfully!");
    Ok(())
}

/// Example 1: Basic Direct Exchange
/// Demonstrates simple point-to-point messaging using direct exchange
async fn basic_direct_exchange_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = dev_config("user_notifications", "notifications_direct");
    let queue = RabbitMQQueueSimple::new(config).await?;

    // Create a simple notification message
    let message = QueueMessage::builder()
        .payload(serde_json::json!({
            "type": "email_notification",
            "user_id": 12345,
            "email": "user@example.com",
            "subject": "Welcome to our service!",
            "body": "Thank you for signing up. Here's how to get started..."
        }))
        .expect("Failed to serialize payload")
        .priority(QueuePriority::Normal)
        .routing_key("user.email")
        .build();

    // Enqueue the message
    let message_id = queue.enqueue(message).await?;
    println!("   ✓ Enqueued notification message: {}", message_id);

    // Try to dequeue (in real scenario, a consumer would be running)
    let dequeued = queue.dequeue().await?;
    if let Some(msg) = dequeued {
        println!("   ✓ Dequeued message: {}", msg.id);
        queue.ack(&msg.id).await?;
        println!("   ✓ Acknowledged message");
    } else {
        println!("   â„šī¸  No messages available (simulated)");
    }

    Ok(())
}

/// Example 2: Fanout Exchange Broadcasting
/// Demonstrates broadcasting the same message to multiple consumers
async fn fanout_exchange_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = RabbitMQConfig {
        connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
        queue_name: "system_events".to_string(),
        exchange_name: "system_events_fanout".to_string(),
        exchange_type: ExchangeType::Fanout,
        routing_key: None, // Fanout ignores routing keys
    };

    let queue = RabbitMQQueueSimple::new(config).await?;

    // Create a system event that should be broadcast to all services
    let system_event = QueueMessage::builder()
        .payload(serde_json::json!({
            "event_type": "system_maintenance",
            "timestamp": chrono::Utc::now().to_rfc3339(),
            "message": "System maintenance scheduled for 2:00 AM UTC",
            "affected_services": ["api", "database", "cache"],
            "duration_minutes": 30
        }))
        .expect("Failed to serialize payload")
        .priority(QueuePriority::High)
        .build();

    let event_id = queue.enqueue(system_event).await?;
    println!("   ✓ Broadcast system event: {}", event_id);

    // Simulate multiple services receiving the same event
    let services = ["logging_service", "monitoring_service", "alerting_service"];
    for service in services {
        println!("   📡 Service '{}' would receive the broadcast event", service);
    }

    Ok(())
}

/// Example 3: Topic Exchange Pattern Matching
/// Demonstrates flexible routing using topic exchange patterns
async fn topic_exchange_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = RabbitMQConfig {
        connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
        queue_name: "application_logs".to_string(),
        exchange_name: "logs_topic".to_string(),
        exchange_type: ExchangeType::Topic,
        routing_key: Some("logs.*".to_string()), // Subscribe to all logs
    };

    let queue = RabbitMQQueueSimple::new(config).await?;

    // Different log messages with different routing keys
    let log_messages = vec![
        (
            "logs.auth.success",
            serde_json::json!({
                "level": "INFO",
                "service": "auth",
                "event": "login_success",
                "user_id": 12345,
                "ip": "192.168.1.100"
            }),
        ),
        (
            "logs.api.error",
            serde_json::json!({
                "level": "ERROR",
                "service": "api",
                "event": "validation_failed",
                "error_code": "INVALID_INPUT",
                "endpoint": "/api/v1/users"
            }),
        ),
        (
            "logs.db.warning",
            serde_json::json!({
                "level": "WARNING",
                "service": "database",
                "event": "slow_query",
                "query_time_ms": 2500,
                "table": "users"
            }),
        ),
        (
            "logs.cache.info",
            serde_json::json!({
                "level": "INFO",
                "service": "cache",
                "event": "cache_hit",
                "key": "user_profile_12345",
                "hit_rate": 0.85
            }),
        ),
    ];

    for (routing_key, payload) in log_messages {
        let log_message = QueueMessage::builder()
            .payload(payload)
            .expect("Failed to serialize log payload")
            .routing_key(routing_key)
            .build();

        let message_id = queue.enqueue(log_message).await?;
        println!("   ✓ Log message: {} -> {}", routing_key, message_id);
    }

    println!("   📊 Total log messages enqueued: {}", log_messages.len());
    Ok(())
}

/// Example 4: Priority Message Handling
/// Demonstrates different message priorities and their processing
async fn priority_message_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = dev_config("priority_queue", "priority_exchange");
    let queue = RabbitMQQueueSimple::new(config).await?;

    // Messages with different priorities
    let priority_messages = vec![
        (
            QueuePriority::Critical,
            serde_json::json!({
                "alert": "SYSTEM_DOWN",
                "severity": "critical",
                "action_required": "immediate"
            }),
        ),
        (
            QueuePriority::High,
            serde_json::json!({
                "alert": "HIGH_CPU_USAGE",
                "severity": "high",
                "cpu_percent": 95.5,
                "action_required": "monitor"
            }),
        ),
        (
            QueuePriority::Normal,
            serde_json::json!({
                "alert": "USER_LOGIN",
                "severity": "info",
                "user_id": 67890
            }),
        ),
        (
            QueuePriority::Low,
            serde_json::json!({
                "alert": "CLEANUP_TASK",
                "severity": "low",
                "task": "log_rotation"
            }),
        ),
    ];

    for (priority, payload) in priority_messages {
        let message = QueueMessage::builder()
            .payload(payload)
            .expect("Failed to serialize priority payload")
            .priority(priority)
            .routing_key(format!("alerts.{}", priority.name().to_lowercase()))
            .build();

        let message_id = queue.enqueue(message).await?;
        println!("   ⚡ {} priority message: {}", priority.name(), message_id);
    }

    Ok(())
}

/// Example 5: Batch Operations
/// Demonstrates efficient batch processing of messages
async fn batch_operations_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = dev_config("batch_processing", "batch_exchange");
    let queue = RabbitMQQueueSimple::new(config).await?;

    // Create a batch of user profile updates
    let mut batch_messages = Vec::new();

    for i in 1..=100 {
        let profile_update = QueueMessage::builder()
            .payload(serde_json::json!({
                "user_id": i,
                "update_type": "profile_refresh",
                "fields_updated": ["last_seen", "status"],
                "timestamp": chrono::Utc::now().to_rfc3339()
            }))
            .expect("Failed to serialize profile update")
            .priority(QueuePriority::Normal)
            .routing_key("user.profile.update")
            .build();

        batch_messages.push(profile_update);
    }

    println!("   đŸ“Ļ Preparing batch of {} profile updates", batch_messages.len());

    // Process the batch
    let start_time = std::time::Instant::now();
    let message_ids = queue.enqueue_batch(batch_messages).await?;
    let processing_time = start_time.elapsed();

    println!("   ✓ Batch processed in {:?}", processing_time);
    println!("   ✓ Messages per second: {:.2}", message_ids.len() as f64 / processing_time.as_secs_f64());
    println!("   ✓ Generated {} message IDs", message_ids.len());

    // Demonstrate batch dequeue
    let batch_result = queue.dequeue_batch(10).await?;
    println!("   ✓ Retrieved batch: {} messages out of {} requested",
             batch_result.messages.len(), batch_result.requested);

    Ok(())
}

/// Example 6: Messages with Custom Headers
/// Demonstrates advanced message routing using custom headers
async fn message_with_headers_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = dev_config("routing_by_headers", "headers_exchange");
    let queue = RabbitMQQueueSimple::new(config).await?;

    // Create a message with rich metadata headers
    let mut headers = HashMap::new();
    headers.insert("source_service".to_string(), serde_json::Value::String("payment_service".to_string()));
    headers.insert("correlation_id".to_string(), serde_json::Value::String("req_123456".to_string()));
    headers.insert("retry_count".to_string(), serde_json::Value::Number(serde_json::Number::from(0)));
    headers.insert("requires_ack".to_string(), serde_json::Value::Bool(true));
    headers.insert("timeout_ms".to_string(), serde_json::Value::Number(serde_json::Number::from(5000)));

    let mut message = QueueMessage::builder()
        .payload(serde_json::json!({
            "transaction_id": "txn_789012",
            "amount": 99.99,
            "currency": "USD",
            "merchant_id": "merchant_456",
            "payment_method": "credit_card"
        }))
        .expect("Failed to serialize payment payload")
        .priority(QueuePriority::High)
        .routing_key("payment.process");

    // Note: In a real implementation, we'd need to add headers to the message structure
    // For this example, we'll simulate it
    let final_message = message.build();

    let message_id = queue.enqueue(final_message).await?;
    println!("   ✓ Payment message with headers: {}", message_id);

    // Display the headers that would be sent
    println!("   📋 Headers:");
    for (key, value) in &headers {
        println!("      {}: {}", key, value);
    }

    Ok(())
}

/// Example 7: Configuration Validation and Error Handling
/// Demonstrates proper configuration and error handling
async fn validation_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("   đŸ›Ąī¸ Testing configuration validation...");

    // Test 1: Valid configuration
    let valid_config = dev_config("valid_queue", "valid_exchange");
    match RabbitMQQueueSimple::new(valid_config).await {
        Ok(queue) => println!("   ✓ Valid configuration accepted"),
        Err(e) => println!("   ❌ Unexpected error with valid config: {}", e),
    }

    // Test 2: Invalid connection URL
    let invalid_config = RabbitMQConfig {
        connection_url: "invalid://not-a-real-protocol".to_string(),
        queue_name: "test".to_string(),
        exchange_name: "test".to_string(),
        exchange_type: ExchangeType::Direct,
        routing_key: None,
    };

    match RabbitMQQueueSimple::new(invalid_config).await {
        Ok(_) => println!("   ❌ Invalid configuration was incorrectly accepted"),
        Err(e) => println!("   ✓ Invalid configuration correctly rejected: {}", e),
    }

    // Test 3: Empty connection URL
    let empty_config = RabbitMQConfig {
        connection_url: "".to_string(),
        queue_name: "test".to_string(),
        exchange_name: "test".to_string(),
        exchange_type: ExchangeType::Direct,
        routing_key: None,
    };

    match RabbitMQQueueSimple::new(empty_config).await {
        Ok(_) => println!("   ❌ Empty URL was incorrectly accepted"),
        Err(e) => println!("   ✓ Empty URL correctly rejected: {}", e),
    }

    // Test 4: Production configuration with TLS
    let prod_config = prod_config(
        "amqps://user:pass@rabbitmq.example.com:5671/%2f",
        "production_queue",
        "production_exchange",
        ExchangeType::Topic,
    );

    match RabbitMQQueueSimple::new(prod_config).await {
        Ok(queue) => println!("   ✓ Production TLS configuration accepted"),
        Err(e) => println!("   âš ī¸  Production config validation (connection test would fail): {}", e),
    }

    Ok(())
}

/// Example 8: Health Monitoring and Statistics
/// Demonstrates queue health checking and performance monitoring
async fn health_monitoring_example() -> Result<(), Box<dyn std::error::Error>> {
    let config = dev_config("health_check_queue", "health_exchange");
    let queue = RabbitMQQueueSimple::new(config).await?;

    // Test queue health
    println!("   💓 Performing health check...");
    let health = queue.health_check().await?;
    println!("   ✓ Health Status: {:?}", health.status);
    println!("   ✓ Queue Size: {}", health.queue_size);
    println!("   ✓ Error Rate: {:.2}%", health.error_rate * 100.0);
    println!("   ✓ Last Check: {}", health.checked_at.format("%Y-%m-%d %H:%M:%S UTC"));

    // Get queue statistics
    println!("\n   📊 Queue Statistics:");
    let stats = queue.get_stats().await?;
    println!("   ✓ Total Messages: {}", stats.total_messages);
    println!("   ✓ Visible Messages: {}", stats.visible_messages);
    println!("   ✓ Invisible Messages: {}", stats.invisible_messages);
    println!("   ✓ Total Processed: {}", stats.total_processed);
    println!("   ✓ Total Failed: {}", stats.total_failed);
    println!("   ✓ Success Rate: {:.2}%", stats.success_rate() * 100.0);

    // Test connection
    println!("\n   🔌 Testing connection...");
    let connection_ok = queue.test_connection().await?;
    println!("   ✓ Connection Test: {}", if connection_ok { "✅ PASS" } else { "❌ FAIL" });

    // Validate configuration
    println!("\n   âš™ī¸  Validating configuration...");
    let config_ok = queue.validate_config().await?;
    println!("   ✓ Configuration Validation: {}", if config_ok { "✅ PASS" } else { "❌ FAIL" });

    // Check queue size and emptiness
    println!("\n   📏 Queue Size Information:");
    let size = queue.size().await?;
    let is_empty = queue.is_empty().await?;
    println!("   ✓ Current Size: {} messages", size);
    println!("   ✓ Is Empty: {}", if is_empty { "✅ YES" } else { "❌ NO" });

    Ok(())
}

/// Utility function to create a basic consumer simulation
async fn simulate_consumer(queue_name: &str) -> Result<(), Box<dyn std::error::Error>> {
    println!("   🤖 Simulating consumer for queue: {}", queue_name);

    let config = dev_config(queue_name, "consumer_exchange");
    let queue = RabbitMQQueueSimple::new(config).await?;

    let mut processed_count = 0;
    let max_messages = 5;

    while processed_count < max_messages {
        match queue.dequeue().await {
            Ok(Some(message)) => {
                println!("   ✓ Processed message: {} - {}", message.id, message.payload);
                queue.ack(&message.id).await?;
                processed_count += 1;
            }
            Ok(None) => {
                println!("   â„šī¸  No more messages in queue");
                break;
            }
            Err(e) => {
                println!("   ❌ Error dequeuing message: {}", e);
                break;
            }
        }

        // Small delay between processing
        tokio::time::sleep(Duration::from_millis(100)).await;
    }

    println!("   📊 Consumer processed {} messages", processed_count);
    Ok(())
}

/// Example of setting up a complete microservices communication pattern
async fn microservices_communication_example() -> Result<(), Box<dyn std::error::Error>> {
    println!("đŸ—ī¸  Microservices Communication Pattern");
    println!("=====================================");

    // Service 1: User Service publishes user events
    let user_service = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "user_events".to_string(),
            exchange_name: "domain_events".to_string(),
            exchange_type: ExchangeType::Topic,
            routing_key: Some("user.created".to_string()),
        }
    ).await?;

    // Publish a user created event
    let user_event = QueueMessage::builder()
        .payload(serde_json::json!({
            "event_type": "user.created",
            "user_id": 98765,
            "email": "newuser@example.com",
            "timestamp": chrono::Utc::now().to_rfc3339(),
            "metadata": {
                "source": "user_service",
                "version": "1.0"
            }
        }))
        .expect("Failed to serialize user event")
        .routing_key("user.created")
        .build();

    let event_id = user_service.enqueue(user_event).await?;
    println!("   👤 User Service published event: {}", event_id);

    // Service 2: Notification Service listens for user events
    let notification_service = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "notifications_queue".to_string(),
            exchange_name: "domain_events".to_string(),
            exchange_type: ExchangeType::Topic,
            routing_key: Some("user.*".to_string()), // Listen to all user events
        }
    ).await?;

    // Service 3: Analytics Service listens for all events
    let analytics_service = RabbitMQQueueSimple::new(
        RabbitMQConfig {
            connection_url: "amqp://guest:guest@localhost:5672/%2f".to_string(),
            queue_name: "analytics_queue".to_string(),
            exchange_name: "domain_events".to_string(),
            exchange_type: ExchangeType::Fanout,
            routing_key: None, // Get all events
        }
    ).await?;

    println!("   📧 Notification Service: Ready to process user events");
    println!("   📊 Analytics Service: Ready to process all events");
    println!("   🔄 Event-driven architecture established!");

    Ok(())
}