简体   繁体   中英

On bluemix message hub, how do I exchange messages between a rest and mqlight client?

according to the documentation https://console.ng.bluemix.net/docs/services/MessageHub/index.html#messagehub it should be possible to submit a message to MessageHub via REST and receive it via a MQLight client. However the documentation is lacking an example and is somewhat ... opaque.

So, if I create the MQLight topic, and have a python client listening,

    import json
    import logging
    import mqlight
    import time

    amqps = 'amqps://xxxxxxxxxxxxx.messagehub.services.us-south.bluemix.net:5671'
    options = {
        'user' : 'xxxxxxxxxxxxxxxx',
        'password' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    }

    def on_message(message_type, data, delivery):
        d = json.loads(data)
        print str(d)

    def on_started(err):
        client.subscribe('test', on_message = on_message)

    def on_stopped(err):
        logging.info('stopped')

    client = mqlight.Client(amqps, security_options = options, client_id = 'client', on_started=on_started)

    while True:
        logging.info(str(client.get_state()))
        time.sleep(5)

how would I post a message via curl. I have tried, where the value string is base64 encoded,

    curl  -i                                                                                                      \
          -X POST                                                                                                 \
          -H "X-Auth-Token:${APIKEY}"                                                                             \
          -H "Content-Type: application/vnd.kafka.binary.v1+json"                                                 \
          --data '{"records":[{"value":"S2Fma2E="}]}'                                                             \
          "https://kafka-rest-prod01.messagehub.services.us-south.bluemix.net:443/topics/MQLight/test"

but that returns,

    {"error_code":404,"message":"HTTP 404 Not Found"}

You're correct that the documentation here isn't particularly fleshed out. The only detail on this is in the small section here which is trying to explain that in order to inter-operate with an MQLight client from some other Kafka or REST client, you'd need to be able to encode/decode the AMQP 1.0 message format (see section 3 of the spec ).

You'd struggle to achieve this in curl scripts as you need access to an AMQP 1.0 library, and even Python isn't ideal as currently your only real option there is to pull in python-qpid-proton which is quite heavyweight as it wraps the proton-c native library and hence requires install-time compilation.

For example, in Java you could use a combination of the official Java kafka-clients and qpid proton-j to provide the AMQP message encoding + decoding. Or if you must use the REST api, then pull in something like feign .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM