Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.36k stars 2k forks source link

How to Peek And Lock a message in my Spring Boot Application using spring-cloud-azure-starter-servicebus-jms? #37834

Open utkarshk8 opened 1 year ago

utkarshk8 commented 1 year ago

Hey guys so the requirement that I have right now is to setup a Queue Listener Spring Boot Application and I am using spring-cloud-azure-starter-servicebus-jms for that, but the problem is the default setup mentioned here deletes the message from the queue once I receive it i.e its a destructive read.

My code right now is really simple: Application name is QueuelistenerApplication

It only contains one file right now i.e QueuelistenerApplication.java with the following code:

package com.queue.listener.queuelistener;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import jakarta.jms.ConnectionFactory;

@SpringBootApplication
@EnableJms
public class QueuelistenerApplication {
    private static final String QUEUE_NAME = "test";

    public static void main(String[] args) {
        SpringApplication.run(QueuelistenerApplication.class, args);
    }

    @JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory")
    public void receiveMessage(String message) {
            System.out.println("Message received: {}" + message);
    }

}

Now I don't want the message to be deleted from the Queue once I've consumed it, Instead what I want is to Peek the Message and Lock it, then do my operations and IF the operations are successful then I can manually acknowledge the message, but IF the operations fails then I want the message to be returned to the Queue. How can I achieve that?

Netyyyy commented 1 year ago

Hi @utkarshk8 , thank you for reporting this issue. CLIENT_ACKNOWLEDGE of JMS session is consistent with PEEK-LOCK mode, hope this can help you

utkarshk8 commented 1 year ago

Hi, @Netyyyy I've already tried that thread but it doesn't seem to work. https://github.com/Azure/azure-sdk-for-java/issues/26762#issuecomment-1057617864 This comment in that thread uses application.properties to set the acknowledge mode as Client but that's already deprecated.

Netyyyy commented 12 months ago

HI @utkarshk8 , JMS still seems not support peek-lock mode. and if the acknowledge mode is deprecated either, we will take it into consideration. We appreciate your input and will review this matter as soon as possible. Please feel free to provide any additional information or context that you think may be helpful. We'll keep you updated on the progress of our review. Thank you for your contribution to improving our project.