ActiveMQ 命令行:将消息从文件发布到队列?

ActiveMQ command line: publish messages to a queue from a file?

我有一个使用 ActiveMQ 的应用程序,通常,我通过使用 AMQ 的网络 UI 将消息发送到我的软件正在使用的队列来测试它。

我想半自动化这个,并希望 AMQ 的命令行能够通过在命令调用中以文本形式提供消息,或者理想情况下,从一个文件。

示例:

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

或:

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

有什么办法吗?

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=
DESTINATION=
FILE=

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

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

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

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

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

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