Home > Software engineering >  How to get SessionId from Azure Service Bus Queue trigger in Azure Function using java?
How to get SessionId from Azure Service Bus Queue trigger in Azure Function using java?

Time:11-19

I am trying to get SessionId, DeliveryCount from an Azure Service Bus Queue trigger in a Java Azure function. I am able to do this easily in a C# Function App. Somehow I found a way to get the Application Properties using binding. But unfortunately am unable to get the above mentioned properties. Any help is appreciated.

@FunctionName("ServiceBusQueueTriggerJava")
    public void run(
            @ServiceBusQueueTrigger(name = "message", queueName = "%ServiceBusQueue%", connection = "ServiceBusConnString", isSessionsEnabled = true) String message,
            final ExecutionContext context, @BindingName("ApplicationProperties") Map<String, Object> properties) {
        Logger log = context.getLogger();
        log.info("Java Service Bus Queue trigger function executed.");
        properties.entrySet().forEach(entry -> {
            log.info(entry.getKey()   " : "   entry.getValue());
        });
        log.info(message);
    }

CodePudding user response:

You can bind IMessageSession to your trigger. I haven't tried this in Java, but should be something along the lines of this:

@FunctionName("ServiceBusQueueTriggerJava")
public void run(
  @ServiceBusQueueTrigger(name = "message",
                          queueName = "%ServiceBusQueue%",
                          connection = "ServiceBusConnString",
                          isSessionsEnabled = true) String message,
  IMessageSession messageSession,
  final ExecutionContext context) {
    Logger log = context.getLogger();
    log.info(messageSession.SessionId)
}

List of available input bindings for ServiceBustrigger.

CodePudding user response:

Below are the example steps on how to get the sessionid while using service bus queue.

You need to install @azure/service-bus npm package.

npm install @azure/service-bus

Import the required service client at the top of your index.js.

const { ServiceBusClient } = require("@azure/service-bus");

Follow the below code sample to directly use SB client to send message with session id:

const sbClient = ServiceBusClient.createFromConnectionString(connectionString);  
const queueClient = sbClient.createQueueClient(queueName);  
const sender = queueClient.createSender();  
const message= {  
body: 'your message body, can be string, js object or byte array',  
sessionId: 'your session id'  
};  
await sender.send(message);

Also Built in Output binding is currently does not support setting message properties.

You can check the Open ticket in Github for the related issue for further information check this Q&A addressed question with related issue.

  • Related