{{announcement.body}}
{{announcement.title}}

How to Create and Publish Azure Functions in Java

DZone 's Guide to

How to Create and Publish Azure Functions in Java

In this article, we will explore how to create and publish Azure functions and integrate them with a SQL database in Java.

· Java Zone ·
Free Resource

In 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:

  1. HttpTrigger.
  2. QueueTrigger.
  3. BlobTrigger.
  4. TimeTrigger.
  5. 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.

Creating a new Maven project


Selecting an Archtype

Enter the value based on the Maven Rules in the “Group Id” and “Artifact Id” fields and click “Finish”.

Entering group and artifact Ids

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

Sample Code for Employee pojo

Sample Code for Employee pojo

In the POM.xml file, update the function App name as “kishazureappfunction”.

Updating POM.xml file

Update the resource group name, region, OS, and App service plan:

Updating POM.xml file

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:

Azure function published

Log on to the Azure Web portal. Under resource group, you can find the Azure app function and ASP:

Azure Web portal

Adding ASP

Navigate to the app function:

App function

Navigate to the HTTP example:

HTTPExample

The HttpExample function listens on the api/HttpExample route, and the URL is https://kishazureappfunction.azurewebsites.net/api/HttpExample.

Listening on api/HttpExample route

Click on the log console:

Log console output

Go to Postman and add the new request with the request parameter "name" as "hello".

Adding key-values in Postman

And a request body with employee information as JSON.

Adding employee into request body

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:

GET request output

The HTTP Response posted with a status of 200.

HTTP response

If a request parameter value is null, then the HTTP response is posted with a status of 400.

400 status response

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.

Updating HttpTriggerFunction

Updating HttpTriggerFunctionTo 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:

Creating new Azure storage account

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.

Sending request to HttpTriggerFunction

New queue created

The queue contains a message text, as an Employee JSON object.

New employee 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.

QueueTriggerExample class

@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:

QueueTrigger logs

Post a new request to the HTTP Trigger function:

Posting a new request

The QueueTrigger function consumed the message from the httpRequestQueue and Employee object details printed in the logger console.

QueueTrigger 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

Creating a new blob container

Here, we've uploaded two text files to the employee container with the employee id value:

Uploading text files to employee container

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:

QueueTrigger logs

Send the HTTP Trigger request with a simple Employee JSON object.

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.

QueueTrigger console output

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.

System throwing exception

Error added to poison queue

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:

BlobTriggerFunction

Create a mydrive container in Azure storage and upload a sample file.

Creating mydrive container

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:

BlobTrigger logs

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.

Making copy of text blob

To Deploy the Azure function, use the following command:

mvn install package azure-functions:deploy

Upload sample file

Uploading the sample file

The backup container created with the backup file.

Backup container with backup file

Time Trigger

Create the Time Trigger function that runs based a CRON expression schedule. The following function executes every minute:

TimeTriggerExample

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.

TimeTrigger logs console


TimeTrigger log output

HttpTrigger Function Integration With Azure SQL

Create the SQL Server and SQL database in Azure. Add a new data_trx table to kishoresqldb. 

Creating database in Azure

Copy the connection String value from the database.

Getting connection string

 Add the connection String value as the URL.

Adding connection string URL

Add the following dependency to the POM.xml

Adding dependency to POM.xml

To deploy the Azure function, use the following command:

mvn install package azure-functions:deploy

Send a new request to HTTP Trigger:

Sending request to HTTP Trigger

Select the query from the data_trx table after the request:

Selecting correct query

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.

Topics:
azure ,azure functions ,cloud ,java ,microsoft ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}