Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

http

mqtt

paho

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.

like image 933
Hilton Avatar asked Sep 19 '19 06:09

Hilton


1 Answers

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.

like image 57
hardillb Avatar answered Oct 20 '22 01:10

hardillb