hot*_*oup 5 bash activemq-classic
我有一个使用 ActiveMQ 的应用程序,通常,我通过使用 AMQ 的 Web UI 将消息发送到我的软件正在使用的队列来测试它。
我想对此进行半自动化,并希望 AMQ 的命令行能够通过在命令调用中将该消息作为文本提供,或者理想情况下从文件中读取它来将消息发送到特定队列。
例子:
./activemq-send queue="my-queue" messageFile="~/someMessage.xml"
Run Code Online (Sandbox Code Playgroud)
或者:
./activemq-send queue="my-queue" message="<someXml>...</someXml>"
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
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
Run Code Online (Sandbox Code Playgroud)