Skip to content

Latest commit

 

History

History
64 lines (61 loc) · 2.78 KB

SetupGuide_Java.md

File metadata and controls

64 lines (61 loc) · 2.78 KB

Setup Function Project

  1. Follow the Configure your local environment instructions for Java.
  2. Install the .NET SDK
  3. Create a Java function project with mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8
  4. Remove the test folder for now.
  5. Install the Redis Extension (manually for now, while the extension has not been added to the Microsoft.Azure.Functions.ExtensionBundle)
    1. Remove extensionBundle from host.json
    2. Run func extensions install --package Microsoft.Azure.WebJobs.Extensions.Redis --version <version>
      • <version> should be the latest version of the extension from NuGet
    3. Add the Java library for Redis bindings to the pom.xml file:
      <dependency>
        <groupId>com.microsoft.azure.functions</groupId>
        <artifactId>azure-functions-java-library-redis</artifactId>
        <version>[0.0.0,)</version>
      </dependency>
  6. Replace the existing Function.java file with the following code:
    import com.microsoft.azure.functions.*;
    import com.microsoft.azure.functions.annotation.*;
    import com.microsoft.azure.functions.redis.annotation.*;
    
    public class Function {
      @FunctionName("PubSubTrigger")
      public void PubSubTrigger(
        @RedisPubSubTrigger(
          name = "message",
          connection = "Redis",
          channel = "pubsubTest")
          String message,
        final ExecutionContext context) {
        context.getLogger().info("Java function triggered on pub/sub message '" + message + "' from channel 'pubsubTest'.");
      }
    }

Setup Redis Cache

  1. Set up an Azure Cache for Redis instance or install Redis locally.
  2. Add the connection string from your Redis instance to your local.settings.json file. It should look something like this:
    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "",
        "FUNCTIONS_WORKER_RUNTIME": "java",
        "Redis": "<connectionString>"
      }
    }

Run Function locally

  1. Start the function locally:
    mvn clean package
    mvn azure-functions:run
    
  2. Connect to your Redis cache using redis-cli, RedisInsight or some other Redis client.
  3. Publish a message to the channel pubsubTest:
    PUBLISH pubsubTest testing
    
  4. Your function should trigger!