eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 884 forks source link

java.lang.IllegalArgumentException: no NetworkModule installed for scheme "wss" of URI #720

Open lexguy20 opened 4 years ago

lexguy20 commented 4 years ago

Hi, I upgraded DeviceClient to 1.18.0 with all its dependencies and now I am getting following IllegalArgumentException while creating DeviceClient, if I keep the DeviceClient version 1.16.0, I don't see following exception:

java.lang.IllegalArgumentException: no NetworkModule installed for scheme "wss" of URI "wss://dev-lram-iot.azure-devices.net/$iothub/websocket?iothub-no-client-cert=true"java.lang.IllegalArgumentException: no NetworkModule installed for scheme "wss" of URI "wss://dev-lram-iot.azure-devices.net/$iothub/websocket?iothub-no-client-cert=true" at org.eclipse.paho.client.mqttv3.internal.NetworkModuleService.validateURI(NetworkModuleService.java:72) at org.eclipse.paho.client.mqttv3.internal.NetworkModuleService.validateURI(NetworkModuleService.java:72) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:454) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:454) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:320) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:320) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:315) at org.eclipse.paho.client.mqttv3.MqttAsyncClient.(MqttAsyncClient.java:315) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttConnection.(MqttConnection.java:65) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttConnection.(MqttConnection.java:65) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:171) at com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttIotHubConnection.open(MqttIotHubConnection.java:171) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.openConnection(IotHubTransport.java:707) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.openConnection(IotHubTransport.java:707) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.open(IotHubTransport.java:273) at com.microsoft.azure.sdk.iot.device.transport.IotHubTransport.open(IotHubTransport.java:273) at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:158) at com.microsoft.azure.sdk.iot.device.DeviceIO.open(DeviceIO.java:158) at com.microsoft.azure.sdk.iot.device.InternalClient.open(InternalClient.java:123) at com.microsoft.azure.sdk.iot.device.InternalClient.open(InternalClient.java:123) at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:316) at com.microsoft.azure.sdk.iot.device.DeviceClient.open(DeviceClient.java:316)

Any help will be greatly appreciated, thanks Here is my code:

`public class IoT-Application
 {
 private static DeviceClient client = null;
 private static DeviceMethodStatusCallBack deviceMethodStatusCallBack = null;

 public IoT-Application()
 {
    deviceMethodStatusCallBack = new DeviceMethodStatusCallBack();
 }

/**
 * Send message to iot hub
 */
private static void sendMessage(JSONObject content, String type)
{
   content.put("iotDeviceId", client.getConfig().getDeviceId());
   Message message = new Message(content.toString());
   message.setProperty("type", type);
   client.sendEventAsync(message, deviceMethodStatusCallBack, null);
}

/**
 * Hub event callback that just echos status.
 */
protected static class DeviceMethodStatusCallBack implements IotHubEventCallback
{
   @Override
   public void execute(IotHubStatusCode status, Object context)
   {
      Activator.getLog().debug(" IotHubEventCallback execute method called with status::" +
      status.name());
   }
}

/**
 * Message callback that just echos the message received.
 */
private static MessageCallback messageCallback = (message, context) -> {
   Activator.getLog().debug(" MessageCallback received message: " + 
   new String(message.getBytes(), StandardCharsets.UTF_8));
   return IotHubMessageResult.COMPLETE;
};

/**
 * Direct method callback handler.
 */
private static class DirectMethodCallback implements DeviceMethodCallback
{
  /**
   * Executes the specified direct method.
   *
   * @param methodName  The name of the method.
   *           
   * @param methodData  The method body (JSON).
   *           
   * @param context The context in which the method was called.       
   *           
   * @return The status of the method result.
   */
  @Override
  public DeviceMethodData call(String methodName, 
                           Object methodData, Object context)
  {
     Activator.getLog().debug(" Incoming method: " + methodName);
     return new DeviceMethodData(200, "StringData");
  }
}

@Override
public void init()
{

   Thread t = new Thread(() -> {
    try
     {

    client = new DeviceClient("<My-Connection-String>", 
            IotHubClientProtocol.MQTT_WS);
    client.open();
    client.setMessageCallback(messageCallback, new HashMap<>());
    client.subscribeToDeviceMethod(new DirectMethodCallback(), null, 
            deviceMethodStatusCallBack, null);
    Long sasTime = new Long(300);
    client.setOption("SetSASTokenExpiryTime", sasTime);
     }
     catch (URISyntaxException syntaxException)
     {
        Activator.getLog().info("Invalid URI syntax");
     }
     catch (Throwable tr)
     {
        tr.printStackTrace();
     }
  });
  t.setName("IoT-device-thread");
  t.start();
}

}`

timtay-microsoft commented 4 years ago

Can you simplify the repro so that it doesn't reference the device client library you mentioned? This repo is for Paho, not the Azure IoT SDK. Here is some simple code for opening an MQTT websocket connection directly using Paho:

String broker       = "wss://iot.eclipse.org:443";
String clientId     = "JavaSample";
MemoryPersistence persistence = new MemoryPersistence();

try {
    MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
    sampleClient.setCallback(new MqttCallback()
    {
        @Override
        public void connectionLost(Throwable cause) {
            System.out.println("Lost connection");
            cause.printStackTrace();
        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {
        }
    });

    System.out.println("Connecting to broker: "+broker);
    sampleClient.connect();
    System.out.println("Connected");
} catch(MqttException me) {
    me.printStackTrace();
}

Is your device capable of running the above without hitting an exception?