Set the expiry of cloud to device message in Azure IoT hub

Amit Rathod 10 Reputation points
2024-05-30T20:00:29.23+00:00

I am well aware of setting the message expiration time from the portal by configuring the Default TTL under Cloud to device Messaging of Iot Hub.

From the documentation(https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-c2d), we can override this property by setting the  ExpiryTimeUtc property in the service(By using iot service SDK)

I am using Java language to develop the solution, and I am using the latest SDK: 2.1.7 of iot-service-client maven artifact. In the previous SDK(1.34.2), there was a method to set the expiration of the message: setExpiryTimeUtc(long milliseconds), but in this new sdk, this method is removed.

I have tried setting the Properties of the Message but still, the message is not getting expired by the desired time as configured. Please refer to the code below. The message should expire after 1 minutes from now, but when I open the device client, the message is still there to recieve.

//Added Properties to set the expiration time of the message
Map<String, String > properties = new HashMap<String, String>();
// Set the absolute expiry time (e.g., 1 hour from now)
Instant expiryTime = Instant.now().plus(1, ChronoUnit.MINUTES);
properties.put("absolute-expiry-time", String.valueOf(expiryTime.toEpochMilli()));
messageToSend.setProperties(properties);
messageToSend.setDeliveryAcknowledgement(DeliveryAcknowledgement.Full);
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,198 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
222 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,736 Reputation points MVP
    2024-06-01T19:27:46.6266667+00:00

    Hello @Amit Rathod,

    welcome to this moderated Azure community forum.

    I checked the GitHub repo but I'm not able to see how this is done the 2.0 version.

    I send a message to my contact within Microsoft to take a look at this.

    Meanwhile. take a look at this migration document.

    You can also submit an issue at the GitHub repo.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.