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

3

6

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?

Turbidimeter answered 14/11, 2018 at 20:27 Comment(0)
H
5

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.

Helvetia answered 16/11, 2018 at 13:7 Comment(0)
A
7

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
Awesome answered 15/11, 2018 at 23:47 Comment(0)
H
5

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.

Helvetia answered 16/11, 2018 at 13:7 Comment(0)
M
3

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"
Mortgagee answered 4/10, 2019 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.