-
Notifications
You must be signed in to change notification settings - Fork 21
improving filtering examples + others #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Receives just Messages where filter California is applied. | ||
This is assured by having added to the vector filter_values of FilterConfiguration the value California | ||
and by the post_filter function to skip false positives | ||
*/ | ||
|
||
use futures::StreamExt; | ||
use rabbitmq_stream_client::error::StreamCreateError; | ||
use rabbitmq_stream_client::types::ResponseCode; | ||
use rabbitmq_stream_client::types::{ByteCapacity, OffsetSpecification}; | ||
use rabbitmq_stream_client::{Environment, FilterConfiguration}; | ||
use std::convert::TryInto; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let stream = "test_stream_filtering"; | ||
let environment = Environment::builder() | ||
.host("localhost") | ||
.port(5552) | ||
.build() | ||
.await?; | ||
|
||
let create_response = environment | ||
.stream_creator() | ||
.max_length(ByteCapacity::GB(5)) | ||
.create(stream) | ||
.await; | ||
|
||
if let Err(e) = create_response { | ||
if let StreamCreateError::Create { stream, status } = e { | ||
match status { | ||
// we can ignore this error because the stream already exists | ||
ResponseCode::StreamAlreadyExists => {} | ||
err => { | ||
println!("Error creating stream: {:?} {:?}", stream, err); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// filter configuration: https://www.rabbitmq.com/blog/2023/10/16/stream-filtering | ||
// We are telling the Consumer to ask the server just messages with filter California | ||
// The post_filler is Optional and needed to skip false positives | ||
let filter_configuration = FilterConfiguration::new(vec!["California".to_string()], false) | ||
.post_filter(|message| { | ||
let region: String = message | ||
.application_properties() | ||
.unwrap() | ||
.get("region") | ||
.unwrap() | ||
.clone() | ||
.try_into() | ||
.unwrap(); | ||
|
||
region == "California".to_string() | ||
}); | ||
|
||
let mut consumer = environment | ||
.consumer() | ||
.offset(OffsetSpecification::First) | ||
.filter_input(Some(filter_configuration)) | ||
.build(stream) | ||
.await | ||
.unwrap(); | ||
|
||
// Just Messages with filter California will appear | ||
while let Some(delivery) = consumer.next().await { | ||
let d = delivery.unwrap(); | ||
println!( | ||
"Got message : {:?} with offset {}", | ||
d.message() | ||
.data() | ||
.map(|data| String::from_utf8(data.to_vec())), | ||
d.offset() | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* Send 100 messages with filter of Region "California" and 100 messages with filter "Texas" | ||
Filters are specified in the application_properties of the messages using the custom field "Region" | ||
in the filter_value_extractor callback | ||
The main thread wait on a condition variable until all the messages have been confirmed */ | ||
|
||
use rabbitmq_stream_client::error::StreamCreateError; | ||
use rabbitmq_stream_client::types::ResponseCode; | ||
use rabbitmq_stream_client::types::{ByteCapacity, Message}; | ||
use rabbitmq_stream_client::Environment; | ||
use std::convert::TryInto; | ||
use std::sync::atomic::{AtomicU32, Ordering}; | ||
use std::sync::Arc; | ||
use tokio::sync::Notify; | ||
|
||
// This callback instruct the Producer on what filter we want to apply | ||
// In this case we are returning the value of the application_property "region" value | ||
fn filter_value_extractor(message: &Message) -> String { | ||
message | ||
.application_properties() | ||
.unwrap() | ||
.get("region") | ||
.unwrap() | ||
.clone() | ||
.try_into() | ||
.unwrap() | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let confirmed_messages = Arc::new(AtomicU32::new(0)); | ||
let notify_on_send = Arc::new(Notify::new()); | ||
let stream = "test_stream_filtering"; | ||
|
||
let environment = Environment::builder() | ||
.host("localhost") | ||
.port(5552) | ||
.build() | ||
.await?; | ||
|
||
let message_count = 200; | ||
let create_response = environment | ||
.stream_creator() | ||
.max_length(ByteCapacity::GB(5)) | ||
.create(&stream) | ||
.await; | ||
|
||
if let Err(e) = create_response { | ||
if let StreamCreateError::Create { stream, status } = e { | ||
match status { | ||
// we can ignore this error because the stream already exists | ||
ResponseCode::StreamAlreadyExists => {} | ||
err => { | ||
println!("Error creating stream: {:?} {:?}", stream, err); | ||
} | ||
} | ||
} | ||
} | ||
|
||
let mut producer = environment | ||
.producer() | ||
// we are telling the producer to use the callback filter_value_extractor to compute the filter | ||
.filter_value_extractor(filter_value_extractor) | ||
.build("test_stream_filtering") | ||
.await?; | ||
|
||
// Sending first 200 messages with filter California | ||
for i in 0..message_count { | ||
let counter = confirmed_messages.clone(); | ||
let notifier = notify_on_send.clone(); | ||
|
||
let msg = Message::builder() | ||
.body(format!("super stream message_{}", i)) | ||
.application_properties() | ||
.insert("region", "California") | ||
.message_builder() | ||
.build(); | ||
|
||
producer | ||
.send(msg, move |_| { | ||
let inner_counter = counter.clone(); | ||
let inner_notifier = notifier.clone(); | ||
async move { | ||
if inner_counter.fetch_add(1, Ordering::Relaxed) == (message_count * 2) - 1 { | ||
inner_notifier.notify_one(); | ||
} | ||
} | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
// Sending 200 messages with filter Texas | ||
for i in 0..message_count { | ||
let counter = confirmed_messages.clone(); | ||
let notifier = notify_on_send.clone(); | ||
let msg = Message::builder() | ||
.body(format!("super stream message_{}", i)) | ||
.application_properties() | ||
.insert("region", "Texas") | ||
.message_builder() | ||
.build(); | ||
|
||
producer | ||
.send(msg, move |_| { | ||
let inner_counter = counter.clone(); | ||
let inner_notifier = notifier.clone(); | ||
async move { | ||
if inner_counter.fetch_add(1, Ordering::Relaxed) == (message_count * 2) - 1 { | ||
inner_notifier.notify_one(); | ||
} | ||
} | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
notify_on_send.notified().await; | ||
producer.close().await?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.