简体   繁体   English

在 java 和 azure function @ServiceBusQueueTrigger 中,如何获取 Label,Custom Properties 和 Broker Properties?

[英]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:这是JAVA中的azure web页面示例,从azure服务总线获取消息内容:

    @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?如何获得您可以在服务总线资源管理器中看到的其他字段:Label、自定义属性和代理属性?

You can retrieve message metadata by adding @BindingName("UserProperties") etc. annotation to method parameters like below for example.例如,您可以通过向方法参数添加@BindingName("UserProperties")等注释来检索消息元数据,如下所示。 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.我使用 Service Bus Explorer 作为 Message Sender 来设置消息的元数据,如下所示,并且能够在“UserProperties”绑定中使用上面的代码在消费者端看到那些元数据。 在此处输入图像描述

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.注意 C# function SDK 比 Java 有一个好处。在 C# 中,您可以获得整个BrokeredMessage object,这更容易直接导航元数据。 But unfortunately, that's not possible in Java SDK as of now where you have to bind separately.但不幸的是,到目前为止,这在 Java SDK 中是不可能的,您必须单独绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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