Skip to content
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

Unable to deliver message to service bus subscription #11

Open
horizondeep opened this issue Feb 13, 2024 · 18 comments
Open

Unable to deliver message to service bus subscription #11

horizondeep opened this issue Feb 13, 2024 · 18 comments

Comments

@horizondeep
Copy link

horizondeep commented Feb 13, 2024

use azservicebus::{
    ServiceBusClient, ServiceBusClientOptions, ServiceBusReceiverOptions, ServiceBusSenderOptions,
};

#[tokio::main]
async fn main() -> Result<()> {
   
let connection_string = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=<your shared access key>".to_owned();

let topic_name = "test".to_owned();
let subscription_name = "DSResult".to_owned();

let mut client = ServiceBusClient::new_from_connection_string( connection_string,
    ServiceBusClientOptions::default(),
)
    .await.unwrap();



let mut sender = client
    .create_sender(&topic_name, ServiceBusSenderOptions::with_subscriber(&subscription_name))
    .await.unwrap();

// Send one message
sender.send_message("Hello World").await.unwrap();


sender.dispose().await.unwrap();
client.dispose().await.unwrap();
Ok(())

}

@horizondeep
Copy link
Author

I tried many times but service bus subscription is not receiving message. My question is how a sender knows where to send the message (in which subscription of topic)
let mut sender = client
.create_sender(&topic_name, ServiceBusSenderOptions::default())
.await?;

@minghuaw
Copy link
Owner

minghuaw commented Feb 13, 2024

This is not how the with_subscriber method should be used. That method is provided by the tracing crate for logging and the subscriber really means a log subscriber rather than your Service Bus subscription.

If you want to target a specific receiver, you should be using Service Bus Queue instead of Service Bus Topic as explained here (https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#topics-and-subscriptions).

A queue allows processing of a message by a single consumer. In contrast to queues, topics and subscriptions provide a one-to-many form of communication in a publish and subscribe pattern. It's useful for scaling to large numbers of recipients. Each published message is made available to each subscription registered with the topic. Publisher sends a message to a topic and one or more subscribers receive a copy of the message.

However, you could apply a filter on a subscription consumer, and you could modify certain properties of a message so that it can only pass through the filter of the desired consumer.

@minghuaw
Copy link
Owner

I will delete this issue once you consider it settled because you have pasted information (your connection string) that I consider confidential for you.

@horizondeep
Copy link
Author

horizondeep commented Feb 13, 2024

I will delete this issue once you consider it settled because you have pasted information (your connection string) that I consider confidential for you.

I used the dummy values.. :). Thanks.

@horizondeep
Copy link
Author

This is not how the with_subscriber method should be used. That method is provided by the tracing crate for logging and the subscriber really means a log subscriber rather than your Service Bus subscription.

If you want to target a specific receiver, you should be using Service Bus Queue instead of Service Bus Topic as explained here (https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#topics-and-subscriptions).

A queue allows processing of a message by a single consumer. In contrast to queues, topics and subscriptions provide a one-to-many form of communication in a publish and subscribe pattern. It's useful for scaling to large numbers of recipients. Each published message is made available to each subscription registered with the topic. Publisher sends a message to a topic and one or more subscribers receive a copy of the message.

However, you could apply a filter on a subscription consumer, and you could modify certain properties of a message so that it can only pass through the filter of the desired consumer.

So I have a service bus "topic" under which I have "subscription". How do I send a message to that particular subscription?

@horizondeep
Copy link
Author

Azure/azure-service-bus#366 . This diagram depicts the correct illustration.

@minghuaw
Copy link
Owner

So I have a service bus "topic" under which I have "subscription". How do I send a message to that particular subscription?

You send to the topic, and all subscriptions under that topic will get a copy of that message. You should use the topic name when you create the sender and use both the topic and subscription names when you create the receiver.

If you are looking for a one-to-one mapping, a queue is what you are after not topic

@minghuaw
Copy link
Owner

Azure/azure-service-bus#366 . This diagram depicts the correct illustration.

As shown in the diagram you quoted. You don't send to subscription directly. You send to the topic (by creating a sender to the topic), then all subscriptions under that topic will get the same message. Message filtering is handled on the subscription side, you could add filter rules on azure portal or in code following this example (https://github.com/minghuaw/azservicebus/blob/main/examples/manage_rules.rs)

@minghuaw
Copy link
Owner

The difference between queue, and topic/subscription is better explained here (https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions)

@horizondeep
Copy link
Author

horizondeep commented Feb 13, 2024

Azure/azure-service-bus#366 . This diagram depicts the correct illustration.

As shown in the diagram you quoted. You don't send to subscription directly. You send to the topic (by creating a sender to the topic), then all subscriptions under that topic will get the same message. Message filtering is handled on the subscription side, you could add filter rules on azure portal or in code following this example (https://github.com/minghuaw/azservicebus/blob/main/examples/manage_rules.rs)

Thanks for the explanation. Yes I do have a filter on subscription to receive/filter message, but still not receiving message. Below are the system properties key in Azure to filter out the message: 1. correlationID 2.contenType 3. label/subject 4. messageId 5.replyto 6.replytoSessionId 7.sessionId 8. to

In Python I use to set the property "to" while sending message and on the Azure side I use to put filter on "to". How can I set the "to" property while sending the message to the topic?

@minghuaw
Copy link
Owner

In Python I use to set the property "to" while sending message and on the Azure side I use to put filter on "to". How can I set the "to" property while sending the message to the topic?

There is a built-in method that allows you to set the to field for a message. https://docs.rs/azservicebus/latest/azservicebus/primitives/service_bus_message/struct.ServiceBusMessage.html#method.set_to

Something like this should work (not tested though)

let mut message = ServiceBusMessage::from("hello world");
message.set_to(String::from("<subscription name>"));
sender.send_message(message).await.unwrap();

@horizondeep
Copy link
Author

In Python I use to set the property "to" while sending message and on the Azure side I use to put filter on "to". How can I set the "to" property while sending the message to the topic?

There is a built-in method that allows you to set the to field for a message. https://docs.rs/azservicebus/latest/azservicebus/primitives/service_bus_message/struct.ServiceBusMessage.html#method.set_to

Something like this should work (not tested though)

let mut message = ServiceBusMessage::from("hello world");
message.set_to(String::from("<subscription name>"));
sender.send_message(message).await.unwrap();

Thanks @minghuaw. This solved my problem. Also thanks for the prompt replies.

@horizondeep
Copy link
Author

Just one more little help. How do I set content type of the message. Can you give an example?

@horizondeep
Copy link
Author

Is the below code snippet correct ?

let mut message = ServiceBusMessage::from("hello world");
message.set_to(String::from(""));
message.set_content_type(String::from("application/json"));
sender.send_message(message).await.unwrap();

@minghuaw
Copy link
Owner

I think all methods are documented on docs.rs (https://docs.rs/azservicebus/latest/azservicebus/index.html). The API mainly follows that of the dotnet SDK, so you could refer to the official tutorials written for the dotnet SDK and the look for the corresponding function in the rust SDK on docs.rs.

Just one more little help. How do I set content type of the message. Can you give an example?

This should do. https://docs.rs/azservicebus/latest/azservicebus/primitives/service_bus_message/struct.ServiceBusMessage.html#method.set_content_type

@minghuaw
Copy link
Owner

Is the below code snippet correct ?

let mut message = ServiceBusMessage::from("hello world"); message.set_to(String::from("")); message.set_content_type(String::from("application/json")); sender.send_message(message).await.unwrap();

Looks right to me. One more note, if you want an empty to field here, you could set it to None like

message.set_to(None);

That function takes anything that implements Into<Option<String>> and this includes String, Some(String) and None

@horizondeep
Copy link
Author

set_content_type

Got it thanks.

@horizondeep
Copy link
Author

I think all methods are documented on docs.rs (https://docs.rs/azservicebus/latest/azservicebus/index.html). The API mainly follows that of the dotnet SDK, so you could refer to the official tutorials written for the dotnet SDK and the look for the corresponding function in the rust SDK on docs.rs.

Just one more little help. How do I set content type of the message. Can you give an example?

This should do. https://docs.rs/azservicebus/latest/azservicebus/primitives/service_bus_message/struct.ServiceBusMessage.html#method.set_content_type

I will refer to this one. Thanks @minghuaw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants