spring-cloud-function icon indicating copy to clipboard operation
spring-cloud-function copied to clipboard

Please provide an Azure function sample with TimerTrigger

Open edavedian opened this issue 5 years ago • 2 comments

The project located at https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-azure is extremely helpful. Could I ask you to please add a sample on how to use an Azure function with TimerTrigger.

edavedian avatar Mar 07 '19 18:03 edavedian

I also fighting to implement TimerTriggered azure function. Sample code would be really helpful. Thank you.

Questions:

  • what should AzureSpringBootRequestHandler be abstract of ? <String, String> ? <Void>, <?>, not at all?
  • What functional interface shout implementing bean implements ? Function/Consumer/Producer... or runnable if working?
  • How to manually trigger this function during dev/testing?
  • How to write tests? ...

JV-TMCZ avatar Jan 12 '21 08:01 JV-TMCZ

I managed to implement TimerTriggered function with Function<String, String>... Here is sample for anybody who could be interested.

// BUG1: input parameter does not accept timerInfo string - it is JSON and it tries to deserialize to object which fails )-: // BUG2: input parameter could not be null -> NPE in identifying type of input parameter // PROBLEM: input parameter of AzureSpringBootRequestHandler could not be VOID and than call handlerRequest with null - fails on BUG2

public class MyFunctionHandler extends AzureSpringBootRequestHandler<String, String> {

    @FunctionName("MyFunction")
    public void run(
            @TimerTrigger(name = "CronTrigger", schedule = "0 0 0 * * *") String timerInfo,
            final ExecutionContext context) throws Exception {
        String result = handleRequest("", context); 
    }
}
// NOTE input parameter timerInfo is not used 
@Component("MyFunction")
public class MyFunction implements Function<String, String> {
    @Override
    public String apply(String empty) {
        return "anything";
    }
}

Requests:

###
POST http://localhost:8080/MyFunction
Content-Type: text/plain


###
POST http://localhost:7071/admin/functions/MyFunction
Content-Type: application/json

{
}

JV-TMCZ avatar Jan 13 '21 10:01 JV-TMCZ