Need help with HiveMQ SDK API's

Hi,

I am using the hello word plugin to log published data to MongoDB. I am trying to find the syntax to get the client id and message payload. I am able to get the topic name in the “HelloWorldInterceptor.java” file which is created when I run the command

mvn archetype:generate -DarchetypeGroupId=com.hivemq -DarchetypeArtifactId=hivemq-extension-archetype -DarchetypeVersion=4.6.3 -DhivemqExtensionSdkVersion=4.4.9

System.out.println(publishPacket.getTopic())

Also how do I locate API’s if I need to make more modification’s ? I am new to Java and have basic understanding of OOP concepts.

Regards,
Harsha

Hi,

I would also like to know if there is an API to get the resource usage stats like CPU, Memory and disk usage.

Regards,
Harsha

Hi @Harsha,

Also how do I locate API’s if I need to make more modification’s ? I am new to Java and have basic understanding of OOP concepts.

You have two options:

I would use the docu as it is more detailed and has examples.

I am trying to find the syntax to get the client id and message payload. I am able to get the topic name in the “HelloWorldInterceptor.java” file which is created when I run the command

Not sure if you use the PublishInboundInterceptor or the PublishAuthorizer, good thing is the way how to get the client id and the payload are the same.

Most (if not all) Services have the same principle.
The service is provided with an input and output object. The input object contains all relevant information needed for the service. Most objects of the input object are not changeable. Then you have the output object where you can modify information you want to give back to HiveMQ (for example a modified publish packet).

Improved example from the “principle” link:

new PublishInboundInterceptor() {
    @Override
    public void onInboundPublish(final @NotNull PublishInboundInput publishInboundInput, final @NotNull PublishInboundOutput publishInboundOutput) {

         final PublishPacket publishPacket = publishInboundInput.getPublishPacket();
         final String topic = publishPacket.getTopic();

         if (publishPacket.getPayload().isPresent()) {
             final ByteBuffer byteBuffer = publishPacket.getPayload().get();
         }

         final ClientInformation clientInformation = publishInboundInput.getClientInformation();
         final String clientId = clientInformation.getClientId();

    }
};

I would also like to know if there is an API to get the resource usage stats like CPU, Memory and disk usage.

For that you can use the MetricRegistry in the extension SDK. If you use HiveMQ Enterprise these information are already available see Monitoring :: HiveMQ Documentation (for example: com.hivemq.system.process-cpu.load).
If you use HiveMQ Communiy (CE) then you would have to add the metrics via a own extension.

Hope this helps,
Michael from the HiveMQ team

1 Like

public class HelloWorldInterceptor implements PublishInboundInterceptor {

@Override
public void onInboundPublish(final @NotNull PublishInboundInput publishInboundInput, final @NotNull PublishInboundOutput publishInboundOutput) {
    final ModifiablePublishPacket publishPacket = publishInboundOutput.getPublishPacket();
    final PublishPacket iPublishPacket = publishInboundInput.getPublishPacket();
    if ("hello/world".equals(publishPacket.getTopic())) {
        final ByteBuffer payload = ByteBuffer.wrap("Hello World!".getBytes(StandardCharsets.UTF_8));
        publishPacket.setPayload(payload);
        System.out.println(publishPacket.getTopic());
    }else{
        System.out.println(publishPacket.getTopic());
        System.out.println(iPublishPacket.);
        MongoClient mongoClient = new MongoClient( "127.0.0.1" , 27017 );
        MongoDatabase db = mongoClient.getDatabase("mydb"); 
        MongoCollection<Document> table = db.getCollection("mydata");
        Document doc = new Document("name", "harsha"); 
        doc.append("id",12); 
        table.insertOne(doc); 
    }
}

}

This is the code I am trying to modify. After adding the line (using your code as reference)

final PublishPacket iPublishPacket = publishInboundInput.getPublishPacket();

…in VS code editor i am not getting the method “getPayload()” when I type “iPublishPacket.”. Name of the file is “HelloWorldInterceptor.java”.

If I try to run “mvn package” with the above line of code the compilation fails.

I tried commenting this entire code and adding your code. But the program fails to compile when I run “mvn package”.

Could you please point out what I am missing here?

Thank you.

Regards,
Harsha

Hi @Harsha,

I copied your code (just snipped the mongoDB stuff) and added the the line to fetch the payload of the publish packet. As you can see nothing is red and it works. Maybe you miss the import of the PublishPacket and therefore VS code can’t resolve what methods PublishPacket provides?

As for the compiling error this line should be the culprit:

System.out.println(iPublishPacket.);

Ever thought of using Intellij (they provide a free version) as in my opinion it provides a smoother Java development experience in comparison to the VS code editor.

Greetings,
Michael

1 Like

Hi Michael,

I had commented out the line “System.out.println(iPublishPacket.);”, so that is not the reason why I was getting compilation error’s.

My code is identical to what you have provided and yet I cannot get the “getPayload()” method and also compilation fails with the line :
final PublishPacket iPublishPacket = publishInboundInput.getPublishPacket();

The following is the import’s in the file

package com.hivemq.extensions.helloworld;

import com.hivemq.extension.sdk.api.annotations.NotNull;

import com.hivemq.extension.sdk.api.interceptor.publish.PublishInboundInterceptor;

import com.hivemq.extension.sdk.api.interceptor.publish.parameter.PublishInboundInput;

import com.hivemq.extension.sdk.api.interceptor.publish.parameter.PublishInboundOutput;

import com.hivemq.extension.sdk.api.packets.publish.ModifiablePublishPacket;

import com.mongodb.MongoClient;

import com.mongodb.client.MongoCollection;

import com.mongodb.client.MongoDatabase;

import org.bson.Document;

import java.nio.ByteBuffer;

import java.nio.charset.StandardCharsets;

I am continuing to debug. But request you to please verify and let me know why I am not getting the “getPayload()" method and why the program is failing to compile…

I did think about using Intellij but I am comfortable with VS code, so did not install it.

Appreciate your help.

Thank you.

Regards,
Harsha

Hi @Harsha,

you are missing the PublishPacket import, that would also explain why compiling doesn’t work:

import com.hivemq.extension.sdk.api.packets.publish.PublishPacket;

Doesn’t VS highlight an missing import? Something like this:

Greetings,
Michael

Hi Michael,

Running “mvn package” does show the error

cannot find symbol
[ERROR] symbol: class PublishPacket
[ERROR] location: class com.hivemq.extensions.helloworld.HelloWorldInterceptor

I added the line

import com.hivemq.extension.sdk.api.packets.publish.PublishPacket;

…and now when I run “mvn package” I dont see the error anymore and I get “BUILD SUCCESS”.

But I am still not able to get the “getPayload()” method when I type in “iPublishPacket.”

Appreciate your help.

Thank you.

Regards,
Harsha

Hi Michael,

Thanks for your help. I am able to log published data received by the broker to mongodb. VS code was not showing “getPayload()” in autofill. I manually entered the method name and ran mvn package and it worked.

Regards,
Harsha