harkesh kumar
harkesh kumar

Reputation: 883

ProtoBuf Message Over Mqtt subcriber issue

I trying to do send payload message using mqtt proto which transfer data in byte format.

So first i am creating protocol buffer format (its Google Format like json or xml) after setting my value into protobuf format then I need to convert object into byte so publish over mqtt protocol that I successfully able to do it

That issue am facing when i subscribe topic and get payload its in btye array format not i convert back to protobuf or json format and i getting issue while converting data

This i am publish my Message using Protobuf format

 @Override
        public void publishMessage(String topic, String message) {


            Account account=        Account.newBuilder().setId(1).setCustomerId(1).setNumber("111111").build()


            try {
                System.out.println(Account.toByteArray());
                MqttMessage mqttmessage = new MqttMessage(Account.toByteArray());
                mqttmessage.setQos(this.qos);
                this.mqttClient.publish(topic, mqttmessage);
            } catch (MqttException me) {
                logger.error("ERROR", me);
            }
        }

My Subscriber where i can receive my message/payload

@Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // TODO Auto-generated method stub

        String time = new Timestamp(System.currentTimeMillis()).toString();
        System.out.println();
        System.out.println("***********************************************************************");
        System.out.println("Message Arrived at Time: " + time + "  Topic: " + topic + "  Message: "
                + new String(message.getPayload()));
        System.out.println("***********************************************************************");


        // String printToString = new JsonFormat().printToString(message.getPayload())

        System.out.println("I am waitign");
}

Output i am getting like this

***********************************************************************
Message Arrived at Time: 2019-09-11 14:57:25.159  Topic: demoTopic2017  Message: 
    bin122001��
System.out.println("I am waitign");

Upvotes: 0

Views: 3039

Answers (1)

hardillb
hardillb

Reputation: 59751

You are publishing a protobuf message, but the code you have posted to process the incoming message you are trying to directly convert it to a JSON object.

You can't just cast the raw bytes of the protobuf to a string and then try and parse it as JSON, you need to unpack it as a protobuf message first, then you can look at converting it to something else.

Something like this:

Account act = Account.parseFrom(message.getPayload())

Upvotes: 0

Related Questions