简体   繁体   中英

How to trigger both Azure Function by Service Bus message and output message to Service Bus within single Azure Function

I need to trigger an Azure Function based on Service Bus message that will do some logic and will write back to Service Bus some message that will potentially trigger another Azure function etc..

I have lack of understanding how to do it properly in standard way.

Based on this document Azure Service Bus trigger for Azure Functions we can do first part: trigger azure function by Service Bus message.

Code:

@FunctionName("sbtopicprocessor")
    public void run(
        @ServiceBusTopicTrigger(
            name = "message",
            topicName = "mytopicname",
            subscriptionName = "mysubscription",
            connection = "ServiceBusConnection"
        ) String message,
        final ExecutionContext context
    ) {
        context.getLogger().info(message);
    }

Based on this document Azure Service Bus output binding for Azure Functions we can do second part: trigger output message to Service Bus.

Code:

@FunctionName("sbtopicsend")
    public HttpResponseMessage run(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            @ServiceBusTopicOutput(name = "message", topicName = "mytopicname", subscriptionName = "mysubscription", connection = "ServiceBusConnection") 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();
        
    }

But I need both input / output functionalities within one function? Should I call second function from the first one via http which seems for me a little bit awkward or should I use Service bus sdk within fist function.

Thanks for any help.

I don't work with Java but you can combine the Trigger and the Output in one function.

@FunctionName("sbtopicprocessor")
public void run(
    @ServiceBusTopicTrigger(
        name = "message",
        topicName = "mytopicname",
        subscriptionName = "mysubscription",
        connection = "ServiceBusConnection"
    ) String messageRequest,
   @ServiceBusTopicOutput(name = "message", topicName = "mytopicname", subscriptionName = "mysubscription", connection = "ServiceBusConnection") OutputBinding<String> message, final ExecutionContext context
) {
    message.setValue(messageRequest.name);
}

You can combine any type of Trigger with any type of Output in one function.

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