简体   繁体   中英

In java with azure function @ServiceBusQueueTrigger, how to get the Label, Custom Properties and Broker Properties?

This is the azure web page example in JAVA to get the message content from the azure service bus:

    @FunctionName("sbprocessor")
 public void serviceBusProcess(
    @ServiceBusQueueTrigger(name = "msg",
                             queueName = "myqueuename",
                             connection = "myconnvarname") String message,
   final ExecutionContext context
 ) {
     context.getLogger().info(message);
 }

This only return the content of the message. How is it possible to get the other fields that you can see in Service bus explorer: Label, Custom Properties and Broker Properties?

You can retrieve message metadata by adding @BindingName("UserProperties") etc. annotation to method parameters like below for example. You can bind to any metadata of a message using binding expression . In this case below, it's "Properties" and "Label".

    @FunctionName("sbprocessor")
    public void serviceBusProcess(
        @ServiceBusQueueTrigger(name = "msg", queueName = "myqueuename", connection = "myconnvarname") 
        String message,
        final ExecutionContext context,
        @BindingName("UserProperties")
        Map<String, Object> properties,
        @BindingName("Label")
        String label) {

            context.getLogger().info("Message received: " + message + " , properties: " + properties + " , label: " + label);
    }

I used Service Bus Explorer as Message Sender to set metadata of the message as below and was able to see those in the consumer side using above code in "UserProperties" binding. 在此处输入图像描述

NB C# function SDK has a benefit here over Java. In C#, you can get the whole BrokeredMessage object which is easier to navigate for metadata directly. But unfortunately, that's not possible in Java SDK as of now where you have to bind separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM