5
votes

I have an app that uses ActiveMQ, and typically, I test it by using AMQ's web UI to send messages to queues that my software is consuming from.

I'd like to semi-automate this and was hoping AMQ's command line has the capability to send a message to a specific queue by either providing that message as text in the command invocation, or ideally, reading it out of a file.

Examples:

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

or:

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

Is there any way to do this?

3

3 Answers

5
votes

ActiveMQ has a REST interface that you can send messages to from the command line, using, for example, the curl utility.

Here is a script I wrote and use for this very purpose:

#!/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
4
votes

You could use the "A" utility to do this.

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

Disclaimer: I'm the author of A, wrote it once to do just this thing. There are other ways as well, such as the REST interface, a Groovy script and whatnot.

1
votes

Based on Rob Newton's answer this is what i'm using to post a file to a queue. I also post a custom property (which is not possible trough the activemq webconsole)

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