How to Create and Publish Azure Functions in Java
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will explore how to create and publish Azure functions and integrate them with a SQL database in Java.
You Will Learn
How can we create and publish Azure functions by using Java:
HttpTrigger
.QueueTrigger
.BlobTrigger
.TimeTrigger
.HttpTrigger
to integrate with Azure SQL.
Tools Required
- Maven 3.0+ build tool.
- Eclipse as IDE.
- Postman.
- Azure Subscription.
The complete Maven project with code examples is available in Github: https://github.com/kanuparthikish/Java-AzureFunctions.git.
Add a new Arch type while creating a new Maven project in Eclipse. Please refer to the following link for the latest version: https://mvnrepository.com/artifact/com.microsoft.azure/azure-functions-archetype.
Enter the value based on the Maven Rules in the “Group Id” and “Artifact Id” fields and click “Finish”.
HTTP Trigger
Create the HTTP Trigger Function that reads a request parameter and request body as a simple Employee POJO. It then sends the response as an HTTP status OK (200) with a request parameter value and employee
object name.
In case a request parameter value is empty or null, then it will send a response as HTTP Status Bad request (400).
Sample Code for HTTP Trigger
Sample Code for Employee pojo
In the POM.xml file, update the function App name as “kishazureappfunction”.
Update the resource group name, region, OS, and App service plan:
We can create a function name and other details, like subscription, resource group, OS details, and storage details in the Azure portal and update those details in the POM.xml file.
To deploy the Azure function, use the following command
mvn install package azure-functions:deploy
If the Azure function is published successfully, we'll see the following output:
Log on to the Azure Web portal. Under resource group, you can find the Azure app function and ASP:
Navigate to the app function:
Navigate to the HTTP example:
The HttpExample function listens on the api/HttpExample route, and the URL is https://kishazureappfunction.azurewebsites.net/api/HttpExample.
Click on the log console:
Go to Postman and add the new request with the request parameter "name" as "hello".
And a request body with employee information as JSON.
Click on send request.
Navigate to the HTTPExample logs console on the Azure portal. The request parameters and Employee Object details are printed in console:
The HTTP Response posted with a status of 200.
If a request parameter value is null, then the HTTP response is posted with a status of 400.
To the above HTTP trigger code, I have added a new input parameter @QueueOutput
so that the Employee pojo could be written to the Azure storage queue for each HTTP request received.
To Deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
The system creates a new Azure storage account under the resource group:
Navigate to the storage account -> Queues. No new queues were created, but when we send a new HTPP request to the HTTP Trigger function, a new queue is created.
The queue contains a message text, as an Employee JSON object.
Queue Trigger
Create the Queue Trigger Function, which polls the Employee POJO from the httpRequestQueue (Message posted in previous HttpTrigger function) and writes a log each time a queue item is processed.
@QueueTrigger(name = "myQueueItem", dataType = "", queueName =
"httpRequestQueue", connection = "AzureWebJobsStorage") Employee
message
The data type attribute should be empty so that message text will be converted to an Employee pojo. To Deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
Navigate to the QueueTrigger logs console on the Azure portal:
Post a new request to the HTTP Trigger function:
The QueueTrigger
function consumed the message from the httpRequestQueue
and Employee object details printed in the logger console.
To the above QueueTrigger
code, I have added a new input parameter, @BlobInput
, so that it will read a blob file from Azure storage based on an employee object id value that matches with the name of the filename.
@BlobInput(name = "file", dataType = "binary", connection =
"AzureWebJobsStorage", path = "employee/{Id}.txt")byte[] content
The {Id}
value is Employee Object--> id property.
Create a new blob container in the storage account
Here, we've uploaded two text files to the employee container with the employee id value:
To deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
Navigate to the QueueTriger logs console on the Azure portal:
Send the HTTP Trigger request with a simple Employee JSON object.
The object will be based on the employee id of the name of the blob file details printed in the logger console.
If the employee id blob file is not found in the container, the system throws an exception and creates a poison queue message placed into the poison queue for the exception process.
Blob Trigger
The following code block creates the Blob Trigger function that logs the filename and size when a blob is added or updated in the mydrive container:
Create a mydrive container in Azure storage and upload a sample file.
To Deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
Navigate to the BlobTrigger logs console in the Azure portal:
To the above BlobTrigger code, I have added a new input parameter, @BlobOutput
, so that it will read a blob file from the Azure storage Blob container and make a copy of a text blob.
To Deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
Upload sample file
The backup container created with the backup file.
Time Trigger
Create the Time Trigger function that runs based a CRON expression schedule. The following function executes every minute:
Use the following link to generate a CRON expression scheduler: https://www.freeformatter.com/cron-expression-generator-quartz.html.
Deploy the Azure function using the following command:
mvn install package azure-functions:deploy
Navigate to the TimerTrigger logs console in the Azure portal.
HttpTrigger Function Integration With Azure SQL
Create the SQL Server and SQL database in Azure. Add a new data_trx
table to kishoresqldb.
Copy the connection String value from the database.
Add the connection String value as the URL.
Add the following dependency to the POM.xml
To deploy the Azure function, use the following command:
mvn install package azure-functions:deploy
Send a new request to HTTP Trigger:
Select the query from the data_trx
table after the request:
Conclusion
I hope you explore the overview of running a Java-based Maven project and publish it to Azure functions. We have developed a serverless compute service that enables you to run code on-demand.
Opinions expressed by DZone contributors are their own.
Comments