Skip to content

Instantly share code, notes, and snippets.

@tsaeki
Last active August 4, 2019 04:14
Show Gist options
  • Save tsaeki/41f9b915852529f4caae30298fe9c626 to your computer and use it in GitHub Desktop.
Save tsaeki/41f9b915852529f4caae30298fe9c626 to your computer and use it in GitHub Desktop.
Sample Azure Functions with Timer trigger and SendGrid output binding for Java
{
"bindings": [
{
"type": "timerTrigger",
"name": "keepAliveTrigger",
"direction": "in",
"schedule": "0 * * * * *"
},
{
"type": "sendGrid",
"name": "$return",
"direction": "out",
"apiKey": "SendGridAttribute.ApiKey"
}
]
}
package com.functions;
import java.time.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
@FunctionName("keepAlive")
public void keepAlive(
@TimerTrigger(name = "keepAliveTrigger", schedule = "0 * * * * *") String timerInfo,
final ExecutionContext context,
@SendGridOutput(
name = "email", dataType = "String", apiKey = "SendGridAttribute.ApiKey", to = "<to mail address>", from = "<from mail address>",
subject= "Sending with SendGrid", text = "Hello from Azure Functions"
) OutputBinding<String> email
)
{
String name = "test";
final String emailBody = "{\"personalizations\":" +
"[{\"to\":[{\"email\":\"<to mail address>\"}]," +
"\"subject\":\"Sending with SendGrid\"}]," +
"\"from\":{\"email\":\"<from mail address>\"}," +
"\"content\":[{\"type\":\"text/plain\",\"value\": \"Hello" + name + "\"}]}";
email.setValue(emailBody);
context.getLogger().info("Java Timer trigger function executed at: " + LocalDateTime.now());
}
}
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "java",
"SendGridAttribute.ApiKey": "<SendGrid ApiKey>"
}
}
@tsaeki
Copy link
Author

tsaeki commented Aug 4, 2019

概要

  • Azure Functionsで定期的にメールを送信するサンプルプログラムです(for Java)
  • triggerをTimer、output bindingをSendGridにしています
  • 環境はubuntu1904

事前に

プロジェクト作成

$ mvn archetype:generate \
>     -DarchetypeGroupId=com.microsoft.azure \
>     -DarchetypeArtifactId=azure-functions-archetype
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype [com.microsoft.azure:azure-functions-archetype:1.23] found in catalog remote
Define value for property 'groupId' (should match expression '[A-Za-z0-9_\-\.]+'): com.functions
[INFO] Using property: groupId = com.functions
Define value for property 'artifactId' (should match expression '[A-Za-z0-9_\-\.]+'): example-function-timer-sendgrid
[INFO] Using property: artifactId = example-function-timer-sendgrid
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.functions: :
Define value for property 'appName' example-function-timer-sendgrid-20190803135456210: : example-function-timer-sendgrid
Define value for property 'appRegion' westus: : japaneast
Define value for property 'resourceGroup' java-functions-group: : example-function
Confirm properties configuration:
groupId: com.functions
groupId: com.functions
artifactId: example-function-timer-sendgrid
artifactId: example-function-timer-sendgrid
version: 1.0-SNAPSHOT
package: com.functions
appName: example-function-timer-sendgrid
appRegion: japaneast
resourceGroup: example-function
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: azure-functions-archetype:1.23
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.functions
[INFO] Parameter: artifactId, Value: example-function-timer-sendgrid
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.functions
[INFO] Parameter: packageInPathFormat, Value: com/functions
[INFO] Parameter: appName, Value: example-function-timer-sendgrid
[INFO] Parameter: resourceGroup, Value: example-function
[INFO] Parameter: package, Value: com.functions
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.functions
[INFO] Parameter: appRegion, Value: japaneast
[INFO] Parameter: artifactId, Value: example-function-timer-sendgrid
[INFO] Project created from Archetype in dir: /home/tsaeki/Develop/example-function-timer-sendgrid
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  19:10 min
[INFO] Finished at: 2019-08-03T14:12:59+09:00
[INFO] ------------------------------------------------------------------------
  • 各ファイルを設定する
ファイル名 説明
Function.java メインプログラム
function.json bindingsの設定
host.json
local.settings.json ローカル環境の設定値. SendGrid ApiKeyを設定
  • build and run
# 事前にazuriteを実行しておく
$ azurite

# 実行
$ mvn clean package 
$ mvn azure-functions:run

# 1分おきにメールが届くことを確認

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment