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

huangapple go评论99阅读模式
英文:

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

问题

以下是您要求的翻译内容:

这是一个用JAVA编写的Azure网页示例,用于从Azure Service Bus获取消息内容:

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

这只会返回消息的内容。如何获取在Service Bus Explorer中可以看到的其他字段:标签(Label)、自定义属性(Custom Properties)和代理属性(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 ?

答案1

得分: 2

你可以通过将 @BindingName("UserProperties") 等注解添加到方法参数上,例如下面的示例,来检索消息的元数据。您可以使用绑定表达式将消息绑定到任何元数据。在下面的示例中,它是“Properties”和“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);
    }

我使用了Service Bus Explorer作为消息发送器,如下所示设置了消息的元数据,并且能够在消费者端使用上述代码中的“UserProperties”绑定来查看这些元数据。

注意:C#函数SDK在这方面优于Java。在C#中,您可以获得整个BrokeredMessage对象,这对于直接导航元数据更容易。但不幸的是,目前Java SDK中还无法实现这一点,您必须单独进行绑定。

英文:

You can retrieve message metadata by adding @BindingName(&quot;UserProperties&quot;) 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(&quot;sbprocessor&quot;)
    public void serviceBusProcess(
        @ServiceBusQueueTrigger(name = &quot;msg&quot;, queueName = &quot;myqueuename&quot;, connection = &quot;myconnvarname&quot;) 
        String message,
        final ExecutionContext context,
        @BindingName(&quot;UserProperties&quot;)
        Map&lt;String, Object&gt; properties,
        @BindingName(&quot;Label&quot;)
        String label) {

            context.getLogger().info(&quot;Message received: &quot; + message + &quot; , properties: &quot; + properties + &quot; , label: &quot; + 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.
In java with azure function @ServiceBusQueueTrigger, how to get the Label, Custom Properties and Broker Properties?

N.B. 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.

huangapple
  • 本文由 发表于 2020年10月27日 20:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64554593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定