Spring cloud function [Azure] TimerTrigger

江枫思渺然 提交于 2021-02-11 13:31:36

问题


Anybody has Spring cloud Function [Azure] TimerTrigger sample implementation ?Please do share


回答1:


I was able to get this running by doing the following (this is based off the HttpTrigger code):

  1. Create the handler that has the TimerTrigger:
@Component
public class TimerHandler extends AzureSpringBootRequestHandler<User, Greeting> {

    @FunctionName("timedTrigger")
    public void timedTrigger(
            @TimerTrigger(name = "trigger", schedule = "0 */30 * * * *") String timerInfo,
            ExecutionContext context) {

        // We need to invoke the handleRequest so our spring function (defined as a bean) will be invoked.
        Greeting g = handleRequest(new User("Trigger"), context);
    }
}
  1. Add your bean (with the same name as the function above).
@SpringBootApplication(
        scanBasePackages = "your.package.structure"
)
public class SpringApp {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TimedHandler.class);
    }

    @Bean
    public Function<User, Greeting> timedTrigger() {        
        return user -> new Greeting("Welcome, " + user.getName());
    }

}
  1. After you get the base working you can modify the AzureSpringBootRequestHander type and the Function types.



回答2:


This is the sample of httptrigger:

https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-azure

you can replace the httptrigger with the timetrigger:

@FunctionName("TimerTriggerJava1")
    public void run(
        @TimerTrigger(name = "timerInfo", schedule = "0 */5 * * * *") String timerInfo,
        final ExecutionContext context
    ) {
        context.getLogger().info("Java Timer trigger function executed at: " + LocalDateTime.now());
    }

This is you need to import:

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


来源:https://stackoverflow.com/questions/62432810/spring-cloud-function-azure-timertrigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!