问题
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):
- 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);
}
}
- 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());
}
}
- After you get the base working you can modify the
AzureSpringBootRequestHander
type and theFunction
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