-
Notifications
You must be signed in to change notification settings - Fork 441
Creating a Java Azure Function
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).
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.
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.
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
}
}
}
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.
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!
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.
- Configuration Settings
- function.json
- host.json
- host.json (v2)
- Http Functions
- Function Runtime Versioning
- Official Functions developers guide
- Host Health Monitor
- Managing Connections
- Renaming a Function
- Retrieving information about the currently running function
- Site Extension Resolution
- Linux Consumption Regions
- Using LinuxFxVersion for Linux Function apps
- Out-of-proc Cancellation Tokens
- Assembly Resolution in Azure Functions
- ILogger
- Precompiled functions
- Official Functions C# developer reference
- Contributor Onboarding
- Development Process
- Deploying the Functions runtime as a private site extension
- Authoring & Testing Language Extensions
- Bindings in out-of-proc
- Language Extensibility
- Worker Capabilities
- Investigating and reporting issues with timer triggered functions not firing
- Sharing Your Function App name privately
- Azure Functions CLI release notes [moved here]
- Function App Zipped Deployment [deprecated]