Search code examples
javaeclipsegradlehivemq

How to add HiveMQ Client as a dependency to HiveMQ Community Edition using Gradle?


I'd like to combine HiveMQ Client and HiveMQ Community Edition which is the implementation for the broker into one project. I tried adding the HiveMQ client as a dependency to the build.gradle file in the Hive MQ Community Edition (broker). It was able to build successfully but I'm not sure if I did it correctly. When I tried to reference client classes inside the Community Edition it gives me errors. Am I missing something? I want to be able to just put put the client project inside of the broker community edition and be able to create a client and access all of the classes I could in HiveMQ client. I left the instructions from the HiveMQ Client website, links, and also what the build.gradle file looks like the HiveMQ community edition.

Error I'm getting: The import com.hivemq.client cannot be resolved (Happens to all imports referencing anything in the HiveMQ Client project)

Link to the HiveMQ GitHubs:

https://github.com/hivemq/hivemq-mqtt-client

https://github.com/hivemq/hivemq-community-edition

enter image description here

M

enter image description here

Code from Main.Java that produces the error

package com.main;

import java.util.UUID;
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient.Mqtt5Publishes;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import java.util.logging.Logger;
import java.nio.ByteBuffer;
import java.util.Optional;

import java.util.logging.Level;
import java.util.concurrent.TimeUnit;




public class Main {

    private static final Logger LOGGER = Logger.getLogger(Main.class.getName());  // Creates a logger instance 


    public static void main(String[] args) {

                // Creates the client object using Blocking API 

            Mqtt5BlockingClient client1 = Mqtt5Client.builder()
            .identifier(UUID.randomUUID().toString()) // the unique identifier of the MQTT client. The ID is randomly generated between 
            .serverHost("0.0.0.0")  // the host name or IP address of the MQTT server. Kept it 0.0.0.0 for testing. localhost is default if not specified.
            .serverPort(1883)  // specifies the port of the server
            .buildBlocking();  // creates the client builder

            client1.connect();  // connects the client
            System.out.println("Client1 Connected");


            String testmessage = "How is it going";
            byte[] messagebytesend = testmessage.getBytes();   // stores a message as a byte array to be used in the payload 

    try {

        Mqtt5Publishes publishes = client1.publishes(MqttGlobalPublishFilter.ALL);  // creates a "publishes" instance thats used to queue incoming messages

            client1.subscribeWith()  // creates a subscription 
            .topicFilter("test/topic")
            .send();
            System.out.println("The client has subscribed");

            client1.publishWith()  // publishes the message to the subscribed topic 
            .topic("test/topic")
            .payload(messagebytesend)
            .send();

         Mqtt5Publish receivedMessage = publishes.receive(5,TimeUnit.SECONDS).orElseThrow(() -> new RuntimeException("No message received.")); // receives the message using the "publishes" instance 
         LOGGER.info("Recieved: " + receivedMessage); 


         byte[] getdata = receivedMessage.getPayloadAsBytes();
         System.out.println(getdata.toString());
         System.out.println(receivedMessage);


    }

    catch (Exception e) {    // Catches all exceptions using the "base exception" 
        LOGGER.log(Level.SEVERE, "Something went wrong.", e);
    }


    }

}

Solution

  • I didn't have the HiveMQ client in my build path. On the line with red errors Eclipse gave me the option of fixing the project set up and I click on it and it automatically added the HiveMQ client to the build path. I posted a screenshot below. enter image description here