【问题标题】:ActiveMQ command line: publish messages to a queue from a file?ActiveMQ 命令行:将消息从文件发布到队列?
【发布时间】:2019-04-17 21:28:28
【问题描述】:

我有一个使用 ActiveMQ 的应用程序,通常我会使用 AMQ 的 Web UI 将消息发送到我的软件正在使用的队列中对其进行测试。

我想半自动化这一点,并希望 AMQ 的命令行能够通过在命令调用中将消息作为文本提供,或者理想情况下从文件中读取消息来将消息发送到特定队列.

例子:

./activemq-send queue="my-queue" messageFile="~/someMessage.xml"

或:

./activemq-send queue="my-queue" message="<someXml>...</someXml>"

有什么办法吗?

【问题讨论】:

    标签: bash activemq


    【解决方案1】:

    根据 Rob Newton 的回答,这就是我用来将文件发布到队列的方法。我还发布了一个自定义属性(通过 activemq webconsole 是不可能的)

    ( echo -n "body="  ;  cat file.xml ) | curl --data-binary '@-' -d "customProperty=value" "http://admin:admin@localhost:8161/api/message/$QueueName?type=$QueueType"
    

    【讨论】:

      【解决方案2】:

      您可以使用"A" utility 来执行此操作。

      a -b tcp://somebroker:61616 -p @someMessage.xml my-queue

      免责声明:我是 A 的作者,写过一次就是为了做这件事。还有其他方法,例如 REST 接口、Groovy 脚本等等。

      【讨论】:

        【解决方案3】:

        ActiveMQ 有一个 REST 接口,您可以从命令行向它发送消息,例如,使用 curl 实用程序。

        这是我为此目的编写并使用的脚本:

        #!/bin/bash
        #
        #
        # Sends a message to the message broker on localhost.
        # Uses ActiveMQ's REST API and the curl utility.
        #
        
        if [ $# -lt 2 -o $# -gt 3 ]  ; then
            echo "Usage: msgSender (topic|queue) DESTINATION [ FILE ]"
            echo "   Ex: msgSender topic myTopic msg.json"
            echo "   Ex: msgSender topic myTopic <<< 'this is my message'"
            exit 2
        fi
        
        UNAME=admin
        PSWD=admin
        
        TYPE=$1
        DESTINATION=$2
        FILE=$3
        
        BHOST=${BROKER_HOST:-'localhost'}
        BPORT=${BROKER_REST_PORT:-'8161'}
        
        if [ -z "$FILE" -o "$FILE" = "-" ]  ; then
            # Get msg from stdin if no filename given
        
            ( echo -n "body="  ;  cat )  \
                | curl -u $UNAME:$PSWD --data-binary '@-' --proxy ""  \
                     "http://$BHOST:$BPORT/api/message/$DESTINATION?type=$TYPE"
        else
            # Get msg from a file
            if [ ! -r "$FILE" ]  ; then
                echo "File not found or not readable"
                exit 2
            fi
        
            ( echo -n "body="  ;  cat $FILE )  \
                | curl -u $UNAME:$PSWD --data-binary '@-' --proxy ""  \
                     "http://$BHOST:$BPORT/api/message/$DESTINATION?type=$TYPE"
        fi
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-03
          • 2019-08-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多