基于 Java 的 Azure 函数的服务总线输出绑定不起作用 - 无错误

Service Bus Output binding for Java based Azure Function is not working - No errors

我尝试构建基于 Java 的 Azure 函数,由 HTTP 触发并将数据发送到服务总线主题。这是基于示例代码。 HTTP 触发器工作正常(返回 Hello 名称),但我没有收到任何发送到服务总线的数据。而且我没有收到错误消息。我已经用基于 C# 的函数测试了 local.settings.json 中的 "queueconstring" 是正确的。

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=java

/* 20.3.2020 HTTP Trigger and Topic output binding*/
package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

/**
 * Azure Functions with HTTP Trigger.
 */
public class HttpTriggerSBOutputJava {
    /**
     * This function listens at endpoint "/api/HttpTriggerSBOutputJava". Two ways to invoke it using 
   "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/HttpTriggerSBOutputJava
     * 2. curl {your host}/api/HttpTriggerSBOutputJava?name=HTTP%20Query
     */


    @FunctionName("HttpTriggerSBOutputJava")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
        @ServiceBusTopicOutput(name = "message", topicName = "ContactInformationChanged", subscriptionName = "Playground", connection = "queueconstring") OutputBinding<String> message,
        final ExecutionContext context) {

        String name = request.getBody().orElse("Azure Functions");

        message.setValue(name);
        return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();

    }

}

您可以使用下面的代码,在我这边效果很好:

Function.java

package com.function;

import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/HttpExample". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/HttpExample
     * 2. curl "{your host}/api/HttpExample?name=HTTP%20Query"
     */
    @FunctionName("HttpExample")
    @ServiceBusTopicOutput(name = "message", topicName = "test", subscriptionName = "test", connection = "ServiceBusConnection")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");
        String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
        } else {
            return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
        }
    }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "java",
    "ServiceBusConnection":"Endpoint=sb://bowmantest.servicebus.windows.net/;SharedAccessKeyName=test;SharedAccessKey=xxxxxxxxx"
  }
}