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
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.
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.
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();
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.
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.