Creating a Java Azure Function

David Ebbo edited this page Jan 17, 2017 · 2 revisions

Azure Functions does not currently have any direct support for running Java Functions. However, you can take advantage of the fact that the JDK in on the machine to hook things up manually.

Note that what is described below is very experimental and is not supported. It is only documented here for users to play around with Java in the context of Azure Functions (see https://github.com/Azure/azure-webjobs-sdk-script/issues/224).

Sample scenario

This walk-through builds a Java Function which reads input from an input storage queue, changes it to upper case, and writes the result to an output queue. Not the most exciting thing, but it demonstrates the concepts.

Prerequisite

The walk-through assumes that you have already created a Function App, and that you are familiar with the basic concepts of Azure Functions, and of the Azure Functions Portal UI.

Creating the Java Function

In the Azure Functions portal, create a new Function, click Batch in the Language drop down, and choose QueueTrigger-Batch:

  • Call the function QueueTriggerJava (or whatever you want).
  • Change the queue name to incoming

In the Integrate UI, add a new Output binding of type Azure Queue Storage, and keep everything default. This way, you've declared both the input and output queues.

In the Develop tab, change the content of run.bat to:

echo OFF

rem inputMessage has the path to a file that has the content of the input queue item
rem Read it into an environment variable to make it easier for the Java code to consume
rem As an alternative, the Java code could just read the file itself
SET /p inputMessage=<%inputMessage%

rem run the Java Function
"D:\Program Files (x86)\Java\jdk1.8.0_73\bin\java" AzureFunction

rem or if you have a Jar file, run this instead:
rem "D:\Program Files (x86)\Java\jdk1.8.0_73\bin\java" -jar AzureFunction.jar

Now go to the 'View Files' UI and create a new file called AzureFunction.java. Paste and Save the following code:

import java.io.*;

public class AzureFunction {
    public static void main(String[] args) {
        // inputMessage contains the content of the input queue item
        String input = System.getenv("inputMessage");
        System.out.println("Java app got message: " + input);

        try {
            // Env variable outputQueueItem has the path of the file to which output needs to be written
            PrintWriter writer = new PrintWriter(System.getenv("outputQueueItem"), "UTF-8");
            writer.println(input.toUpperCase());
            writer.close();
        } catch (IOException e) {
            // do something
        }
    }
}

Building the Java Function

The only thing left to do is to build the Java code. We'll do this from the Kudu Console, which you can access by clicking on 'Function App Settings'. From Kudu, go to D:\home\site\wwwroot\QueueTriggerJava and run the following:

"D:\Program Files (x86)\Java\jdk1.8.0_73\bin\javac" AzureFunction.java 

Now you have an AzureFunction.class file that's ready to run.

Note: if you prefer, you can also create a Jar from the class, and run the Jar instead of the '.class' file in the batch file above.

Also, you don't need to build the Jar file from Kudu. You can build it on your local machine and drag and drop it into Kudu file explorer.

Testing the Java Function from the Functions portal

Go to the 'Test' UI, and paste Let's run a Java Function in the request body, then hit Run. Your Function will run and do its thing. If you look at your storage account (e.g. using Storage Explorer), you'll see a new queue called outqueue, and it should contain a new message with text LET'S RUN A JAVA FUNCTION. It worked!

Running the Java Function with real data

From Storage Explorer, create a new queue called 'incoming'. Add a new message to it, and it should get processed very quickly. Go back to the 'outqueue' to see the result.