azure-functions-java-worker icon indicating copy to clipboard operation
azure-functions-java-worker copied to clipboard

WIP: Hooks for DI frameworks #310

Open patriot1burke opened this issue 6 years ago • 17 comments

This is a work in progress. Will add tests if there's interest in this. This simple patch provides a hook so that DI frameworks can instantiate function class instances.

In my PoC, I was able to hook in Quarkus Arc CDI implementation using this WIP quarkus azure extension so that an Azure function class could be a CDI bean.

JavaMethodExecutor looks for a text file META-INF/service/com.microsoft.azure.functions.FunctionFactory in function app. If exists, reads that file to find a FQN classname. Does reflection to find a method named "newInstande(Class)". If this static method exists, it will use that to allocate a function class instance instead of class.newInstance().

patriot1burke avatar Jul 26 '19 20:07 patriot1burke

CLA assistant check
All CLA requirements met.

msftclas avatar Jul 26 '19 20:07 msftclas

Thanks @patriot1burke for the PR, really appreciate your work. Just wondering if we can follow a same pattern as spring cloud function on initialize the DI frameworks. This give your maximum flexibility to update/release on your side instead of having a hard dependency on our side. The spring cloud function example can be found https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-azure. The are initialize spring application context at the interface at https://github.com/spring-cloud/spring-cloud-function/blob/526e6eebec0777157cc4bf2dfe75a79c4a706f00/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/FunctionInvoker.java#L84. Please let me know what you think of this approach, it make the function a little bit verbose but remove the hard dependency on us. Thank you.

kaibocai avatar Aug 08 '22 21:08 kaibocai

@kaibocai Thanks for getting back to me.

We could do it that way, but we can have smoother integration.

With the approach I'm suggesting, the application developer writing a function class would not have to extend any special class like they do with the Spring example. Quarkus would be initialized automatically and you could use injection annotations (CDI or Spring) directly in the function class providing nice smooth integration with Azure functions. Again, take a look at the sample class I posted which is what the integration would look like if you added this small hook.

patriot1burke avatar Aug 09 '22 14:08 patriot1burke

@kaibocai Just trying to improve the user/appdev experience with Azure Functions + Quarkus. I'm sure the Spring guys would love to have this hook as well!

patriot1burke avatar Aug 09 '22 14:08 patriot1burke

once PR https://github.com/Azure/azure-functions-java-worker/pull/634 is ready, it can be utilized as DI hook. @patriot1burke please review above PR and give any feedback you would have. (Feedback from expert like you helps us a lot.) Thank you.

kaibocai avatar Aug 24 '22 15:08 kaibocai

@kaibocai #634 is still allocating the function class in JavaMethodExecutorImpl. Would want to delegate function class instantiation to another framework (Quarkus or Spring).

patriot1burke avatar Aug 24 '22 16:08 patriot1burke

@kaibocai #634 is still allocating the function class in JavaMethodExecutorImpl. Would want to delegate function class instantiation to another framework (Quarkus or Spring).

That's right, I didn't notice the core point for di hook is to create the function class and register it in di framework bean container. In this case probably framework like Quarkus and Spring cannot utilize the middleware invocation pipeline. Let me discuss with the team on this. Thanks for your reply.

kaibocai avatar Aug 24 '22 16:08 kaibocai

@kaibocai I've updated my WIP to refactor some things and add some tests.

FYI: Could create some kind of FunctionFactory interface, just thought it would be less a burdon on MSFT and anybody writing an extension if they didn't have to include azure-functions-java-worker dependency.

patriot1burke avatar Aug 24 '22 16:08 patriot1burke

/azp run

kaibocai avatar Aug 25 '22 12:08 kaibocai

Azure Pipelines successfully started running 1 pipeline(s).

azure-pipelines[bot] avatar Aug 25 '22 12:08 azure-pipelines[bot]

Hi @patriot1burke , just want to confirm that for Quarkus to work for azure function, is it a must that function class need to be created/registered by/to Quarkus bean container , can we just initialize the framework without the next line. Thank you.

BTW maybe update the commit message as well.

kaibocai avatar Aug 25 '22 12:08 kaibocai

Hi @patriot1burke , just want to confirm that for Quarkus to work for azure function, is it a must that function class need to be created/registered by/to Quarkus bean container , can we just initialize the framework without the next line. Thank you.

BTW maybe update the commit message as well.

Yes, its required that Quarkus create the function class. Please see the example of what the integration would look like. I imagine that Spring would want the same thing.

patriot1burke avatar Aug 25 '22 17:08 patriot1burke

Hi @patriot1burke , sorry for late reply (not seeing the notification of your comment), this PR https://github.com/Azure/azure-functions-java-worker/pull/634 didn't build cause we haven't update necessary dependencies, which is also required for this PR. Will make the updates asap.

Please feel free to branch off this new PR https://github.com/Azure/azure-functions-java-worker/pull/641, which has a clear implementation. The dependency PR is here which you can build it on you local for testing/implementing the DI hook https://github.com/Azure/azure-functions-java-core-library/pull/7 at https://github.com/Azure/azure-functions-java-worker/blob/ac6cb815d9493a0fcef7dd87a3006da3623b8b18/pom.xml#L69 Please let me know if anything else you need from myside. Appreciate your contribution! Thank you very much.

kaibocai avatar Sep 02 '22 21:09 kaibocai

@kaibocai Saw that your 'middleware" changes made it into master! :)

Has this version of the java worker been released? When will it be available to use with Azure Functions live? TY!!

patriot1burke avatar Oct 12 '22 20:10 patriot1burke

@kaibocai will the ability to change the target function class instance be added to the MiddlewareContext interface as we discussed in previous exchanges? I'm would still be unable to integrate Quarkus with the current java worker in main.

patriot1burke avatar Oct 12 '22 20:10 patriot1burke

@kaibocai Saw that your 'middleware" changes made it into master! :)

Has this version of the java worker been released? When will it be available to use with Azure Functions live? TY!!

Hi @patriot1burke , thanks very much for reaching out. After detailed discussion with whole team, we moved out DI hook from middleware feature, we are currently actively working on PR DI hook feature and PR spi library, it will provide an interface that you can use to initialize the framework. We are looking forward to release this feature in next two weeks. Thanks very much for your help and contribution.

BTW: we are using dev branch as latest development branch. Thank you.

kaibocai avatar Oct 12 '22 20:10 kaibocai

@kaibocai Saw that your 'middleware" changes made it into master! :) Has this version of the java worker been released? When will it be available to use with Azure Functions live? TY!!

Hi @patriot1burke , thanks very much for reaching out. After detailed discussion with whole team, we moved out DI hook from middleware feature, we are currently actively working on PR DI hook feature and PR spi library, it will provide an interface that you can use to initialize the framework. We are looking forward to release this feature in next two weeks. Thanks very much for your help and contribution.

BTW: we are using dev branch as latest development branch. Thank you.

Thank you @kaibocai I will update the Quarkus integration code once this gets merged to dev. Ty for help.

patriot1burke avatar Oct 12 '22 21:10 patriot1burke

close this PR as PR https://github.com/Azure/azure-functions-java-worker/pull/667 is merged

kaibocai avatar Oct 31 '22 18:10 kaibocai