DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Practical Generators in Go 1.23 for Database Pagination
  • Navigating NoSQL: A Pragmatic Approach for Java Developers
  • Getting Started With HarperDB and Java: Your First "Hello, World" Integration
  • Providing Enum Consistency Between Application and Data

Trending

  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Orchestrating Microservices with Dapr: A Unified Approach
  • How to Merge HTML Documents in Java
  • Detection and Mitigation of Lateral Movement in Cloud Networks
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Create and Publish Azure Functions in Java

How to Create and Publish Azure Functions in Java

By 
kishore kanuparthi user avatar
kishore kanuparthi
·
Updated Apr. 15, 20 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
15.2K Views

Join the DZone community and get the full member experience.

Join For Free

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.

azure Database Requests Java (programming language) Console (video game CLI) Data Types Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Practical Generators in Go 1.23 for Database Pagination
  • Navigating NoSQL: A Pragmatic Approach for Java Developers
  • Getting Started With HarperDB and Java: Your First "Hello, World" Integration
  • Providing Enum Consistency Between Application and Data

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!