04. Azure Functions For Java Devs

nitya

Nitya Narasimhan, Ph.D

Posted on September 6, 2022

04. Azure Functions For Java Devs

✨✨Register for the Cloud Skills Challenge! ✨✨
Skill up by completing learn modules and competing with your peers for a spot on the leaderboard. You've got till Sep 30 to get it done! 🎯


Welcome to Day 4 of #30DaysOfServerless!

Yesterday we walked through an Azure Functions Quickstart with JavaScript, and used it to understand the general Functions App structure, tooling and developer experience.

Today we'll look at developing Functions app with a different programming language - namely, Java - and explore developer guidance, tools and resources to build serverless Java solutions on Azure.


What We'll Cover


About The Author

Rory Preddy is a Principal Cloud Advocate on the Java Cloud Advocacy team at Microsoft. He co-authored the Java Learning Path at Microsoft and was named a Top Influencer for "Who's Who in Cloud". You can find him @rorypreddy or follow him right here on dev.to!


Developer Guidance

If you're a Java developer new to serverless on Azure, start by exploring the Azure Functions Java Developer Guide. It covers:

In this blog post, we'll dive into one quickstart, and discuss other resources briefly, for awareness! Do check out the recommended exercises and resources for self-study!


My First Java Functions App

In today's post, we'll walk through the Quickstart: Azure Functions tutorial using Visual Studio Code. In the process, we'll setup our development environment with the relevant command-line tools and VS Code extensions to make building Functions app simpler.

Note: Completing this exercise may incur a a cost of a few USD cents based on your Azure subscription. Explore pricing details to learn more.

First, make sure you have your development environment setup and configured.

  1. An Azure account with an active subscription - Create an account for free
  2. The Java Development Kit, version 11 or 8. - Install
  3. Apache Maven, version 3.0 or above. - Install
  4. Visual Studio Code. - Install
  5. The Java extension pack - Install
  6. The Azure Functions extension for Visual Studio Code - Install

VS Code Setup

New to Visual Studio Code? - Start with the Java in Visual Studio Code tutorial to jumpstart your learning!

Install the Extension Pack for Java (shown below) to install 6 popular extensions to help development workflow from creation to testing, debugging, and deployment.

Extension Pack for Java

Now, it's time to get started on our first Java-based Functions app.


1. Create App

  1. Open a command-line terminal and create a folder for your project. Use the code command to launch Visual Studio Code from that directory as shown:

  2. Open the Visual Studio Command Palette (Ctrl + Shift + p) and select Azure Functions: create new project to kickstart the create workflow. Alternatively, you can click the Azure icon (on activity sidebar), to get the Workspace window, click "+" and pick the "Create Function" option as shown below.

  3. This triggers a multi-step workflow. Fill in the information for each step as shown in the following prompts. Important: Start this process from an empty folder - the workflow will populate it with the scaffold for your Java-based Functions app.

Visual Studio Code uses the provided information and generates an Azure Functions project. You can view the local project files in the Explorer - it should look like this:

Azure Functions Scaffold For Java


2. Preview App

Visual Studio Code integrates with the Azure Functions Core tools to let you run this project on your local development computer before you publish to Azure.

  1. To build and run the application, use the following Maven command. You should see output similar to that shown below.

  2. Copy the URL of your HttpExample function from this output to a browser and append the query string ?name= , making the full URL something like http://localhost:7071/api/HttpExample?name=Functions. The browser should display a message that echoes back your query string value. The terminal in which you started your project also shows log output as you make requests.

🎉 CONGRATULATIONS - You created and ran a function app locally!

With the Terminal panel focused, press Ctrl + C to stop Core Tools and disconnect the debugger. After you've verified that the function runs correctly on your local computer, it's time to use Visual Studio Code and Maven to publish and test the project on Azure.


3. Sign into Azure

Before you can deploy, sign in to your Azure subscription.



az login


Enter fullscreen mode Exit fullscreen mode

The az login command signs you into your Azure account.

Use the following command to deploy your project to a new function app.



mvn clean package azure-functions:deploy


Enter fullscreen mode Exit fullscreen mode

When the creation is complete, the following Azure resources are created in your subscription:

  • Resource group. Named as java-functions-group.
  • Storage account. Required by Functions. The name is generated randomly based on Storage account name requirements.
  • Hosting plan. Serverless hosting for your function app.The name is java-functions-app-service-plan.
  • Function app. A function app is the deployment and execution unit for your functions. The name is randomly generated based on your artifactId, appended with a randomly generated number.

4. Deploy App

  1. Back in the Resources area in the side bar, expand your subscription, your new function app, and Functions. Right-click (Windows) or Ctrl - click (macOS) the HttpExample function and choose Execute Function Now....

  2. In Enter request body you see the request message body value of { "name": "Azure" }. Press Enter to send this request message to your function.

  3. When the function executes in Azure and returns a response, a notification is raised in Visual Studio Code.

You can also copy the complete Invoke URL shown in the output of the publish command into a browser address bar, appending the query parameter ?name=Functions. The browser should display similar output as when you ran the function locally.

🎉 CONGRATULATIONS - You deployed your function app to Azure, and invoked it!


5. Clean up

Use the following command to delete the resource group and all its contained resources to avoid incurring further costs.



az group delete --name java-functions-group


Enter fullscreen mode Exit fullscreen mode

Next Steps

So, where can you go from here? The example above used a familiar HTTP Trigger scenario with a single Azure service (Azure Functions). Now, think about how you can build richer workflows by using other triggers and integrating with other Azure or third-party services.

Other Triggers, Bindings

Check out Azure Functions Samples In Java for samples (and short use cases) that highlight other triggers - with code! This includes triggers to integrate with CosmosDB, Blob Storage, Event Grid, Event Hub, Kafka and more.

Scenario with Integrations

Once you've tried out the samples, try building an end-to-end scenario by using these triggers to integrate seamlessly with other Services. Here are a couple of useful tutorials:

Exercise

Time to put this into action and validate your development workflow:

Resources

💖 💪 🙅 🚩
nitya
Nitya Narasimhan, Ph.D

Posted on September 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

05. Functions for JS Devs
beginners 05. Functions for JS Devs

September 7, 2022

04. Azure Functions For Java Devs
beginners 04. Azure Functions For Java Devs

September 6, 2022