0

我正在 Azure Functions 中编写 @TimerTrigger 函数。为此,我使用了 Spring Cloud Function for Azure。问题是每次访问服务或 JPARepository 时都会出现空指针异常。@Value("${VARIABLE}") 也给出了 null 但 System.getenv("VARIABLE") 确实提供了值。

代码 Main.Java 是:

@SpringBootApplication
@EnableJpaRepositories("com.xyz.*")
@ComponentScan(basePackages = "com.xyz.*")
@EntityScan("com.xyz.*")
public class Scheduler {

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

    @Bean
    public void hello() {}
}

函数 HelloHandler.java 文件是:

@Component
public class HelloHandler extends AzureSpringBootRequestHandler<Void, Void> {

    @Autowired
    MyService myService;

    @Value("${VARIABLE}")
    private String variable;

    @FunctionName("hello")
    public void execute(@TimerTrigger(name = "timerInfo", schedule = "0 * * * * *") String timerInfo,
                        final ExecutionContext context){
        context.getLogger().info("Executing the function..."+variable);    // this gives null.
        context.getLogger().info(System.getenv("VARIABLE"));               // this does provide value.
        myService.hello(context);                                          // null pointer exception here.
        context.getLogger().info("Here I am...");
    }
}

服务 MyService.Java 是:

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    private MyRepository myRepository;

    public void hello(ExecutionContext context) {
        List<User> userList = myRepository.findAll();
        for (User user : userList) {
            context.getLogger().info("USER: "+user.getId());
        }

        context.getLogger().info("Process Finished.");
    }
}

我正在使用以下依赖项:

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-function-adapter-azure -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-azure</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-function-web -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-function-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

它给出了例外:

Executed 'Functions.hello' (Failed, Id=90714488-b07a-41ac-ae37-d120a1a8e732, Duration=372ms)
System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result: Failure
Exception: NullPointerException: 
Stack: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
    at com.microsoft.azure.functions.worker.broker.JavaMethodExecutorImpl.execute(JavaMethodExecutorImpl.java:54)
    at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:57)
    at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
    at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
    at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
    at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at com.xyz.scheduler.HelloHandler.execute(HelloHandler.java:26)
    ... 16 more
.

我不明白为什么它不能按预期使用所有 Spring 功能。

4

2 回答 2

1

你必须用你的 Azure 函数声明一个同名的 bean。我的 Azure 函数与 HttpTrigger 和 TimerTrigger 在同一个 Spring Cloud Azure 函数应用程序中工作。尝试这个

HelloHandler.java

public class HelloHandler extends AzureSpringBootRequestHandler<String, String> {

    @FunctionName("hello")
    public void execute(@TimerTrigger(name = "timerInfo", schedule = "0 * * * * *") String timerInfo,
                        final ExecutionContext context){
        context.getLogger().info("Executing the function..."+variable);    
        context.getLogger().info(System.getenv("VARIABLE"));               
        super.handleRequest(timerInfo, context);  // this is correct way to invoke your Spring Cloud function.
        context.getLogger().info("Here I am...");
    }
}

HelloFunction.java

@Configuration
public class HelloFunction {

    @Autowired
    private MyRepository myRepository;
    
    @Autowired
    private ExecutionContext context;
    
    @Bean
    public Function<String, String> hello() {
        return timerInfo -> {
            List<User> userList = myRepository.findAll();
            for (User user : userList) {
                context.getLogger().info("USER: "+user.getId());
            }
            context.getLogger().info("Process Finished.");
            return "";
        };
        
    }

}
于 2021-01-14T06:00:04.687 回答
0

不知道为什么它不起作用,它看起来是正确的。我都试过了@ValueSystem.getenv()在演示中,它们运行良好。

如果您在环境变量中找不到键 ( VARIABLE ),您将收到此异常 java.lang.IllegalArgumentException: Could not resolve placeholder 'XXX' in string value "${XXX}"

所以好像在你的 Method 中得到了 NullPointerException hello(),尝试一步步调试方法,说不定会发生在user.getId().

在此处输入图像描述

于 2020-09-14T09:11:04.227 回答