Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Report] Why is DeviceClient.closeNow() not recognised as valid method? #1737

Closed
olivertdam opened this issue Aug 31, 2023 · 3 comments
Closed

Comments

@olivertdam
Copy link

olivertdam commented Aug 31, 2023

Context

I am trying to build a simple app for sending data to Azure Iot. My code has previously been working, but I am now encountering timeouts when trying to connect to the Iot hub and send data. I wanted to make sure my connection was properly closed, and noticed that the method close() has been deprecated and you should now use closeNow() instead. However, when I use the new method it is not recognised by android studio as a valid method.
I am using the lasted azure sdk iot version 2.2.0, and the documentations states .close() has been deprecated since 1.1.25.

  • OS and version used: <Windows 10> (Windows 10, Ubuntu 18.04, etc. )
  • Java runtime used: <openjdk version "11.0.16.1" 2022-08-12 LTS
    OpenJDK Runtime Environment Microsoft-40648 (build 11.0.16.1+1-LTS)
    OpenJDK 64-Bit Server VM Microsoft-40648 (build 11.0.16.1+1-LTS, mixed mode)

(in a command prompt: java -version )

  • SDK version used: <32> (Please include the version of the SDK used)

Description of the issue

The DeviceClient seem to have the problem. Fails everytime.

Code sample exhibiting the issue

Please remove any connection string information!

I have used this sample example to write my code link

private boolean connectAndSend(String dataToSend, String connString, int numRepeatMessageSend) throws IOException, URISyntaxException, InterruptedException, IotHubClientException {
        DeviceClient client = new DeviceClient(connString, protocol);
        client.setMessageCallback(new MessageCallbackMqtt(), null);
        client.setConnectionStatusChangeCallback(new IotHubConnectionStatusChangeCallbackLogger(), new Object());
        Log.i(TAG, "Successfully created an IoT Hub client with callback settings.");

        client.open(true);
        Log.i(TAG, "Opened connection to IoT Hub.");


        // send message to azure
        Log.i(TAG, "Constructing message to send to Azure");
        Message msg = new Message(dataToSend);
        // msg.setContentType("application/json");
        msg.setMessageId(UUID.randomUUID().toString());
        boolean messageSend = false;
        // try connecting and sending to Azure for 10 minutes. If there is still
        // no connection abandon and continue with next measurement in 30 minutes
        int longTimeTry = 6; // should be 60 // 10 seconds per try means ~6 tries per minute, trying for ~10 minutes is 60 times trying
        int timesTried = 0;
        while (!messageSend && timesTried < longTimeTry) {
            timesTried++;
            for (int i = 0; i < numRepeatMessageSend; i++) {
                try {
                    client.sendEvent(msg, D2C_MESSAGE_TIMEOUT_MILLISECONDS);
                    messageSend = true;
                    Log.i(TAG, "Successfully send message " + i+1 + " to Azure");
                    timesTried = 0;
                    counter_sendToAzure = mSCGDeviceModel.getCounterAzure();
                    counter_sendToAzure = counter_sendToAzure+1;
                    mSCGDeviceModel.setCounterAzure(counter_sendToAzure);
                } catch (IotHubClientException e) {
                    Log.i(TAG, "Failed to send message to Azure. Status code: " + e.getStatusCode());
                    Log.i(TAG, "Trying to resend message " + i + ". Count: " + timesTried);
                }
            }
        }
        // close the connection to client
        Log.i(TAG, "Closing client");
        client.closeNow();
        return messageSend;
    }

In my gradle build i include:
implementation group: 'com.microsoft.azure.sdk.iot', name: 'iot-device-client', version: '2.2.0'

Console log of the issue

I cannot attach debug log as the app will not start at all. Here is output when trying to build:

       client.closeNow();
              ^
  symbol:   method closeNow()
  location: variable client of type DeviceClient
@timtay-microsoft
Copy link
Member

CloseNow was the recommended function to call in most 1.X.X releases of this SDK, but since 2.X.X it has been removed and Close is the recommended function.

In 1.X.X, close waited for all outstanding requests to finish before closing which risked blocking forever. In 2.X.X, close behaves the same as closeNow did in 1.X.X in that it cancels any pending operations and closes immediately.

It sounds like your IDE may be pulling javadocs from 1.X.X even though you took a dependency on a 2.X.X version.

@olivertdam
Copy link
Author

Sorry. My bad.
I had been looking at old documentation thinking it was new. I just noticed in the top of this page:, that it references maven 1.30.1, when i had looked at the url seeing "azure-java-stable" thinking it was the newest stable build.
Sorry for the inconvinience.

@timtay-microsoft
Copy link
Member

No worries! I'll go ahead and close this issue then

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

No branches or pull requests

2 participants