First published on MSDN on Nov 01, 2017
Guest post by Jin Sun Park Microsoft Student Partner at Imperial College
Hello! My name is Jin Sun Park and I am currently a second year Computing student at Imperial College London. I’m originally from Korea, but I grew up in Singapore and the Philippines. My academic interests include data science and computer graphics, and my passion lies in finding ways to use technology to aid education. Linkedin Profile:
Microsoft Azure is a cloud computing service that allows users to create, manage, and deploy applications. The advantage of using serverless computing is that the infrastructure on which the application runs on is abstracted from the developer. This allows developers to focus more on creating great applications instead of managing servers. Furthermore, serverless apps are not restricted by infrastructure as resources will scale up or down depending on the amount of activity on the server. Azure also offers a pay-as-you-go plan that allows developers to pay only for the actual quantity of resources used.
What is Azure Functions?
In October 2017, Microsoft announced the addition of Java support for Azure Functions. Azure Functions essentially allows developers to run functions in the cloud. As such, Azure Functions makes use of everything that serverless computing has to offer – abstraction from infrastructure, flexible scaling, and pay-as-you-go pricing plan, to name a few. We can use Azure Functions for tasks such as processing data, integrating systems, and the Internet-of-Things (IoT). Azure Functions provides developers with a wide-ranging tool set to develop these applications.
This blog post will be an introduction into using Azure Functions in Java.
To create a new Functions project, launch the command-line interface and in an empty directory (I named my empty directory hellofunctions), paste the following code snippet.
mvn archetype:generate \
For Windows (CMD)
mvn archetype:generate ^
Maven will then prompt you for values. Fill in groupId and artifactId, and press “enter” for the rest as Maven will generate the values for you. I decided to input com.hello.functions for my groupId and hello-functions for my artifactId. At the end, input Y to confirm.
If the Functions project was successfully created, you will see this message:
Using Visual Studio Code
For ease of development, we will be using Visual Studio Code to develop our Functions project.
1. Open Visual Studio Code and click “File > Open”.
2. Navigate to the directory containing your Functions project and click “Open”
3. On the sidebar we can see that if we navigate through “src > main> java > com > hello > functions”, we get our Function.java file
Function.java should look like this:
Function.java contains a Function class with one method called hello that returns a String.
The function hello is a HTTP triggered function (@HttpTrigger), which means that the function is invoked with an HTTP request. A function should have exactly one
and this trigger is used to define how the function is invoked. There are a wide range of triggers such as the Timer trigger that invokes the function at certain time intervals.
connect input and output data to the function. Unlike triggers, bindings are optional and a function can have multiple bindings.
We use the
property of triggers and bindings to reference them in our code. In the Function.java snippet, we see that the @HttpTrigger has the name “req”, which is then used in the return String.
Back in the command-line interface, type mvn package to build the project.
Following the path “target > azure-functions > hello-functions-20171031221307662 > hello” in our Visual Studio Code sidebar, we see that there is a new file generated called function.json, which looks like this:
Azure understands JSON, but we want to work with Java to develop our application. The Maven plugin thus translates annotations such as @FunctionName and @HttpTrigger for us so that Azure can understand them.
Deploy Functions to Azure
Back in the command-line interface type mvn azure-functions:deploy, which handles everything needed to deploy our function to Azure – the cloud. It returns a URL that we can call.
After running the command, we get this line that gives us the url
Using the URL in the command
curl –X POST –d “world” <insert url here>/api/hello;echo gives us an output
We have successfully created an Azure Function in Java! We were able to deploy our function to the cloud with just a few simple commands. The combination of using Visual Studio Code and Maven allows developers to work on code locally and then seamlessly deploy their work to Azure. Azure offers a host of different tools to help enhance your Functions that I suggest you experiment with. If you would like to learn more about Azure Functions in Java please take a look at the resources section that follows.