英文:
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("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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论