Exception in thread "main" java.lang.IllegalArgumentException: no NetworkModule installed for scheme "http" of URI "http://xxx.xxx.xxx.xxx:xxxx"
Asked Answered
B

1

6

I'm currently using on MQTT eclipse/paho.mqtt.java library for my project. this is the link of eclipse/paho.mqtt.java library.

https://github.com/eclipse/paho.mqtt.java

I wanted to connect with MQTT with http protocol. but the original library make for the tcp protocol. I tried to make a connection with mqtt by making a mqtt client. I will show my code in below,

Sorry about using the xxx symbols in the IP addresses and the URLs.

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;


public class MQTT {
  public static void main(String[] args) {

    String topic        = "MQTT Examples";
    String content      = "Message from MqttPublishSample";
    int qos             = 2;
    //String broker       = "tcp://iot.eclipse.org:1883";
    String broker       = "http://xxx.xxx.xxx.xxx:xxxx";
    String clientId     = "JavaSample";
    MemoryPersistence persistence = new MemoryPersistence();

    try {
        MqttClient sampleClient = new MqttClient(broker, clientId, null);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        String accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String pass = "";
        char[] charArr = pass.toCharArray();
        //I added in here my access token of the server
        connOpts.setUserName(accessToken);
        connOpts.setPassword(charArr);
        System.out.println("Connecting to broker: "+broker);
        sampleClient.connect(connOpts);
        System.out.println("Connected");
        System.out.println("Publishing message: "+content);
        MqttMessage message = new MqttMessage(content.getBytes());
        message.setQos(qos);
        sampleClient.publish(topic, message);
        System.out.println("Message published");
        sampleClient.disconnect();
        System.out.println("Disconnected");
        System.exit(0);
    } catch(MqttException me) {
        System.out.println("reason "+me.getReasonCode());
        System.out.println("msg "+me.getMessage());
        System.out.println("loc "+me.getLocalizedMessage());
        System.out.println("cause "+me.getCause());
        System.out.println("excep "+me);
        me.printStackTrace();
    }
  }
}

It shows following error,

Exception in thread "main" java.lang.IllegalArgumentException: no NetworkModule installed for scheme "http" of URI "http://xxx.xxx.xxx.xxx:xxxx"
at org.eclipse.paho.client.mqttv3.internal.NetworkModuleService.validateURI(NetworkModuleService.java:70)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:454)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:320)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:315)
at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:227)
at MQTT.main(MQTT.java:17)

So what should I do for this, in example the eclipse had been use tcp protocol in String broker ="tcp://iot.eclipse.org:1883";. but I want to set the http for this, I tried to add it as this, String broker = "http://xxx.xxx.xxx.xxx:xxxx"; then got thte above error, So what should do for this.

Boneblack answered 19/9, 2019 at 6:16 Comment(7)
Where does it say HTTP is supported?Doviedow
I did not see it. Is it not support for HTTP?, but the case is I want to connect this with http protocol.Boneblack
You can want whatever you like, but all it says in the Javadoc is 'An application can connect to an MQTT server using: A plain TCP socket An secure SSL/TLS socket.'Doviedow
So then what should I do for the code, can you change the code.Boneblack
Change it to do what? It can't do the impossible.Doviedow
So shall I use SSL rather than http? Can give me a solution for it, actually I have no idea about id.Boneblack
Use whatever you like that's supported. Only you know what your requirements are. Nobody else can answer this for you.Doviedow
W
13

It is not clear from the question what you are trying to do, but this may help clarify a few things for you.

  1. MQTT is Pub/Sub protocol, HTTP is a request response protocol. The whole paradigm is different, you can not just directly substitute one for the other.
  2. MQTT normally runs natively on top of TCP/IP, but it can also run using Websockets as the transport layer. Websockets do boot strap over HTTP.

The in the Paho client broker URL schema (protocols) can be one of the following:

  • tcp:// This is native MQTT over TCP
  • ssl:// This is native MQTT over TCP with SSL/TLS
  • ws:// This MQTT over Websockets
  • wss:// This is MQTT over Websockets with SSL/TLS
  • local:// special case not useful here.

In order to use anything other than basic MQTT over TCP (tcp://) you will need to configure the broker to support this as an extra listener on a different port to the default 1883.

Wraith answered 19/9, 2019 at 8:7 Comment(1)
Thank you very much sir,I apology from you for unable to submit the question in clearly. But Sir your answer is actually matched with the questionBoneblack

© 2022 - 2024 — McMap. All rights reserved.