Azure Function App with Java
Azure Function App is lightweight, serverless computation engine which allows to run business code without having serer infrastructure management.
It is quite simple, robust, easy to develop and easy to use.
It provides a trigger based (where various triggering/entry point mechanism available) service platform; when specified trigger event is generated, the written code for Function App will be executed.
The Function App can be created to create piped operation and workflow but it different from the Azure Logic App which is intended to create workflow with connectors.
Azure provides various options, but serverless makes is easier to use without having worry about the different infrastructural burden.
Azure Function App provides various triggering mechanisms:
- HTTPTrigger — It executes code on HTTP request.
- TimerTrigger — It executes function app on pre-specified time, widely used for cleaning activities.
- CosmosDBTrigger — Function App code executes add/update entries in CosmosDB (NoSQL) documents.
- BlobTrigger — Function app executes when documents are added/removed in Azure Blob Storage.
- QueueTrigger — Executes code when message comes to subscribed queue.
- EventGridTrigger — It executes code in response of events comes into Subscribe EventGrid. It is widely used for event-based pipe creation.
- EventHubTrigger — It executes code in repose of events comes to AzureEventHub. Mostly used for workflow processing.
- ServiceBusQueueTrigger — It allows to execute Function App code by connecting to other Azure Service.
- ServiceBusTopicTrigger — Similar to ServiceBusQueueTrigger, only difference is, it works for topics.
Azure Function App provides effective orchestration, it grows/shrink instances as per workload automatically so owner need to worry about liveness. Also, it provides log monitor, metrices, insight and other monitoring tools which provides effective monitoring experience.
Azure provides SDKs which provides dependencies which helps to create Azure Function App with different programming languages e.g., Java, .Net, Python, NodeJs etc.
For Java, Azure Function App version 3.x is preferred.
Azure offers various pricing and structures based upon different use cases. It provides pay as you go model which is widely used.
Azure Function App can be used in wide verity of use cases as it provides different triggering ways.
Let us take a real-world problem: An application to be developed which scan documents, store scanned documents, enhance the captured document, do OCR and read textual content and send notification to a web application for further processing. Once, it is processed by Web Application, user gets result on device.
Consider the diagram for problem statement:
Let us dig out the problem statement in details like how function it can be achieved:
There is infrastructural requirement with the problem statement like how captured document will be stored and then move to next step of processing.
The cross-cutting concerns for any application also apply here like,
- Auto scaling based upon load.
- Logging and health metrics
- Authorization and authentication
And so on.
This article covers only Function App with Java but can be applied same architecture solution with other languages.
Considering the use case discussed into the problem statement section, there may be various solution based upon different technical components.
Let us see solution in Azure Cloud with the help of Azure Blob Storage and Azure Function App:
Explanation of defined solution:
- User capture document image from device
- As user captured, parallelly uploaded to Azure Blob Storage (see Azure Blob Storage Reference)
- Once document uploaded, an event sends to Event Grid
- When event comes into Event Grid, the based upon subscription and filter, Function App automatically triggered
- Azure Function App performs the Image enhancement and notify to OCR Engine, after OCR data stored into database which further utilized by Server to create response to user
Here, there is a piped operation where captured image is processed by one component and notify to next component to for further processing. Consider below diagram which depicts piped operation on captured document:
Azure Function App — Azure Portal
Let us consider, Azure Blob Storage and Event Grid enabled into the Azure portal (please refer the articles: Azure Blob Storage with Java and Azure Event Grid).
To develop the solution first, we need to have Azure Account with Azure Blob Storage account already created.
Need to create Azure Function App by following below steps:
- Login in Azure Portal, search for Function App. Select Function App from search result. A screen will be shown, where a list of Function App will be listed if available or New button will be available in case there is no Function App.
- Click on New Button, a tabbed form will be prompted.
Fill the required details — Basic Tab:
Fill Details: subscription and resource group (same resource group name will be used in pom.xml), provide function app name (same name will be used in pom.xml), select code, select runtime stack Java, Version 8 or 11, select region (same region will be used in pom.xml).
Click Next to open Hosting tab:
Select New in Storage field as you are creating new Function App, select operating system Linux, select plan type serverless (you can change plan as your need).
Click Next to open Monitor Tab:
In case of, Insight available for selected region/os it will be by default selected no.
Click Next to provide tags:
If required, provide tags for this resource.
Click on Review + Create. Once, Function app created, it opens the resource:
Now, function app has been created. One, can stop, restart check metrics etc. For Java, Azure does not provide code edit tool for Azure function app, other other languages e.g. C#, Azure provides code editor pane.
Now, proceed to create code for Function App. Follow below steps:
Azure Function App — Java Code
Once, Azure Function App created, need to write code for Function App and deploy it.
Take code checkout from below link: https://github.com/siddhivinayak-sk/azure-function
This code contains the complete Azure function app code which trigger based upon EventGrid subscription and execute specified code into function App body.
To compile, package and Deploy need to setup pre-requisite:
- JDK 8 or later
- DotNet SDK 5 or later
- Azure CLI
- Azure Function Core Tool — with NPM (npm i -g azure-functions-core-tools@3
- Apache Maven Build Tool
To Package execute below maven command:
mvn package azure-functions:package
To deploy function app, configure below in pom.xml:
- Define the function name
2. Define the region
3. Define the Resource Group for the function
4. Define the FUNCTIONS_EXTENSION_VERSION Latest stable version is
Login in Azure CLI by command:
Now, deploy by running below maven command:Define the function name
<functionAppName>my-azure-function</functionAppName> <! — Azure Function Name →
Define the region
<functionAppRegion>Central India</functionAppRegion> <! — Region →
Define the Resource Group for the function
<functionResourceGroup>my-azure-function-RG</functionResourceGroup> <! — Azure Function Resource Group →
Define the FUNCTIONS_EXTENSION_VERSION Latest stable version is
Login in Azure CLI by command:
Now, deploy by running below maven command:
An alternate solution can also be with Azure Function App with HTTP Trigger. Consider the below diagram:
There are alternate approaches also available like using other technical components e.g. upload image directly to web server but it requires to handle the load and consider the size of files involved in processing which takes time to move at next step.
Additionally, each Cloud Vendor provides similar kind serverless compute engines like GCP provides GCP Function, which are fully managed and do not need to worry about common concerns.
Fully managed, serverless compute resources/services are getting popularity for developing piped operation without having infra related concern. It helps to develop application in very easy manner and can be scaled up as load grows. The consumption-based pricing model as made it very effective cost wise.
Java developers can easily create Function App and take benefit of serverless computation and integrate it with various triggering options.
- Microsoft Azure Function App — https://azure.microsoft.com/en-in/services/functions/
- Java Azure Function App — https://github.com/siddhivinayak-sk/azure-function
- Azure Blob Storage — https://siddhivinayak-sk.medium.com/azure-blob-storage-with-java-5817347a89d0
- Azure Event Grid — https://siddhivinayak-sk.medium.com/azure-event-grid-with-real-world-example-4b1a541b03d8
About the Author
Sandeep Kumar holds Master of Computer Application degree has been Java developer having 10 years of working experience. He has experience designing and development of enterprises applications into various education, content, laboratory, and banking domains, got various appreciation for providing solutions including spot appreciation for Glassfish to JBoss migration project. He secured Google Cloud Developer certificate and participated into OCI training. He is a part of HCL-ERS platform as Sr. Lead developer.