【问题标题】:How can I use Node.js to post messages to ActiveMQ using its REST interface?如何使用 Node.js 通过其 REST 接口向 ActiveMQ 发布消息?
【发布时间】:2015-12-01 20:16:55
【问题描述】:

我有一个在 Node.js 中运行的 STOMP 客户端,它订阅了 ActiveMQ 中的队列。使用 cURL 时,我可以使用以下命令将消息发布到队列(由我在 Node 中运行的 STOMP 客户端立即使用):

curl -d "body=message" http://sub.dummyhost.net:8161/demo/message/dispatch?type=queue

我正在尝试使用 Node.js 的请求模块 (https://github.com/request/request) 使用以下代码重现此 POST 请求,这符合我们应该如何使用 application/x-www-form-urlencoded 数据发送 POST 请求:

request.post({
        url: 'http://sub.dummyhost.net:8161/demo/message/dispatch?type=queue',
        form: {
            body:'message'
        }
    }, 
    function(err, response, body){
      //do something with response
    });

在响应的 headers 属性中,我可以看到 message-id 属性,每次使用上面的函数调用发出 POST 请求时,该属性都会递增。但是,当我检查代理本身的管理视图时,我没有看到入队和出队的消息增加(而且我的 STOMP 消费者什么也没收到,这与我使用 cURL 发出 POST 请求时不同)。消息似乎根本没有发布到队列中。

我在这里缺少什么?

【问题讨论】:

    标签: node.js forms http curl activemq


    【解决方案1】:

    无论出于何种原因,当我使用 request.post 并以这种方式格式化选项时,ActiveMQ 不喜欢它。当我将 URL 作为第一个选项传递时,它似乎工作正常:

      var promisifiedPostRequest = Promise.promisify(request.post);
      return promisifiedPostRequest(
        'http://localhost:8080/demo/message/queue1?type=queue', 
        {
          form : {
            body: JSON.stringify(notification),
            AMQ_SCHEDULED_DELAY : delay
        }
      })
      .then(function(response){
        return response.headers.messageid;
      });
    

    我在这里写了一篇关于 REST、Node.js 和 ActiveMQ 的博文:https://medium.com/@mackplevine/using-activemq-with-node-js-stomp-and-http-b251ce8d995#.4qf53glie

    【讨论】:

      【解决方案2】:
      var options = { method: 'POST',
        url: 'http://sub.dummyhost.net:8161/demo/message/dispatch?type=queue',
        headers: 
         { 'cache-control': 'no-cache',
           'content-type': 'multipart/form-data' },
        form: { body: 'message' } };
      
      request(options, function (error, response, body) {
        if (error) throw new Error(error);
      
        console.log(body);
      });
      

      试试这个~

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-13
        • 2016-04-14
        • 2018-01-31
        • 2011-10-02
        • 2015-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多