User Tools

Site Tools


cloud:azure:functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cloud:azure:functions [2024/04/28 12:28] skipidarcloud:azure:functions [2024/05/19 14:36] (current) skipidar
Line 212: Line 212:
  
 {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/MbDC47HSC7.png}} {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/MbDC47HSC7.png}}
 +
 +
 +=== Azure Functions Java ===
 +
 +https://stackoverflow.com/questions/77360325/cannot-create-java-function-in-azure-function-app
 +
 +<sxh java>
 +mvn clean install
 +mvn azure-functions:run
 +mvn azure-functions:deploy
 +</sxh>
 +
 +Java SDK content
 +  * https://azure.github.io/azure-sdk-for-java/servicebus.html
 +  * https://learn.microsoft.com/en-us/java/api/com.azure.messaging.servicebus.servicebusclientbuilder?view=azure-java-stable
 +  * 
 +
 +
 +=== Azure Service Topic Pub with session ===
 +
 +requirements.txt
 +<sxh python>
 +# DO NOT include azure-functions-worker in this file
 +# The Python Worker is managed by Azure Functions platform
 +# Manually managing azure-functions-worker may cause unexpected issues
 +
 +azure-functions
 +datetime
 +azure-servicebus
 +
 +</sxh>
 +
 +local.settings.json
 +<sxh python>
 +
 +{
 +  "IsEncrypted": false,
 +  "Values": {
 +    "AzureWebJobsStorage": "",
 +    "FUNCTIONS_WORKER_RUNTIME": "python",
 +    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
 +    "ServiceBusConnection": "Endpoint=sb://alfdevapi6sb.servicebus.windows.net/;SharedAccessKeyName=alfdevapi6servicebus_auth_rule;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="
 +  }
 +}
 +
 +</sxh>
 +
 +
 +Python V2 app, which uses the Python SDK, to send messages with SessionId
 +
 +  * [[https://learn.microsoft.com/en-us/azure/developer/python/sdk/azure-sdk-overview?view=azure-python|Use the Azure libraries (SDK) for Python]]
 +  *  [[https://learn.microsoft.com/en-us/python/api/azure-servicebus/azure.servicebus.servicebusclient?view=azure-python|ServiceBusClient Class]]
 +
 +<sxh python>
 +import logging
 +import azure.functions as func
 +from datetime import datetime
 +import json
 +from azure.servicebus import ServiceBusClient, ServiceBusMessage
 +import os
 +
 +app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
 +
 +# vs output into queue for python
 +# https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-python
 +
 +
 +TOPIC_NAME_A = "alfdevapi6topic" 
 +CONN_STRING = os.environ['ServiceBusConnection']
 +SESSION_ID = "008"
 +
 +
 +@app.route(route="http_trigger")
 +def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
 +    logging.info('Python HTTP trigger function processed a request.')
 +    myMessage = "Hi alf this is my message via the queue to you."
 +
 +    jsn_message_envelope =  generateMessage(myMessage, SESSION_ID)
 +    logging.info(f"Will send envelope: {jsn_message_envelope}")
 +
 +    servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONN_STRING)
 +    rith_sen = servicebus_client.get_topic_sender(TOPIC_NAME_A)
 +    rith_msg = ServiceBusMessage(jsn_message_envelope)
 +    rith_msg.session_id = SESSION_ID
 +    with rith_sen:
 +        rith_sen.send_messages(rith_msg)
 +    servicebus_client.close()
 +
 +
 +    return func.HttpResponse(
 +            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
 +            status_code=200
 +    )
 +
 +
 +def generateMessage(myMessage, mySessionId):
 +    now = datetime.now()
 +    print("now =", now)
 +    logging.info(f"Time stamp: {now}")
 +
 +    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
 +
 +    my_json_string = f"""
 +    {{
 +        "body": "{myMessage}",
 +        "customProperties": {{
 +            "timePublish": "{dt_string}",
 +        }},
 +        "brokerProperties": {{
 +            "SessionId": "{mySessionId}"
 +        }}
 +    }}
 +    """
 +
 +    return my_json_string
 +
 +</sxh>
 +
 +== Publish a test message ==
 +
 +Publishing a message
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/tVGGBpwi0d.png}}
 +
 +And see, which session was used during publishing, here ''session1''
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/AegrzHZAfS.png}}
 +
 +== Consuming a test message ==
 +
 +Now a consumer can consume the message
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/at6eSPYnFS.png}}
  
  
cloud/azure/functions.1714307282.txt.gz · Last modified: by skipidar