Get the latest message in the queue - rabbitmq

I want to fetch the last / latest message added in the queue, is there a specific option available in the rabbitmqadmin utility.
The following command is giving the first message in the queue,
./rabbitmqadmin get queue='log' -H localhost -P 15672 -u <username> -p <password> --vhost=logging count=1

Are you looking to consume or view the messages? I use this tool plumber to view the latest incoming messages without removing them from the queue. If you are looking to consume just the latest message you may have to write a script.
To read the latest inbound message and exit:
plumber read messages rabbitmq --address amqp://user#pass:127.0.0.1:5672 --exchange events --routing-key \#
To watch all messages as they come in:
plumber read messages RabbitMQ --address amqp://user#pass:127.0.0.1:5672 --exchange events --routing-key \# --follow
If you use plumber your queue must be set up on its own exchange you can not use the RabbitMQ default exchange see.

Related

RabbitMQ channel has no consumer

I am trying to send a message to RabbitMQ via MQTT, however when the connection is established, the channel shows that is has ... no consumers .... To my understanding, upon connecting via MQTT to RabbitMQ, the topic will default to the amq.topic, and a new queue will be created for these connections.
Oddly enough, this does happen when I connect using Mosquitto, however, other MQTT clients do not get their own queue.
So, as an example, connecting like so: mosquitto_sub -h rabbit-01.<hostname.com> -p 1883 -u <v_host>:<username> -P <pass> -t "#"
produces:
HOWEVER!
my connection from the other server pub/sub mqtt integration thing results in no new queue displaying. Here are the settings for that:
Any help would be greatly apprecieated!
Thanks!

Rabbitmq can not purge all messages in the queue

I am using rabbitmq, and I try to purge a queue by using commands like below:
[root#test xxx]# rabbitmqctl purge_queue metering.sample
Purging queue 'metering.sample' in vhost '/' ...
[root#test xxx]# rabbitmqadmin purge queue
name=metering.sample
queue purged
[root#test xxx]# rabbitmqctl list_queues | grep sample
metering.sample 17172
Initially, the queue was filled with 296533 messages, after I ran both of the commands, the queue is still filled with 17172 messages. (I am sure there is no publisher running anymore)
why did it happen? is it a bug or I used it by wrong way?
need some help, thanks in advance.
Keep in mind that the unacked messages are not purged by those commands.
https://stackoverflow.com/a/25116528/2047138

want to purge queue in remote machine rabbitmq

I am having rabbitmq up and running in another machine with ip address 10.8.11.12 on port 15672 and in that i am having a queue named "hello". I want to purge hello queue from my machine using CLI (command line interface)
I have tried following to purge queue in localhost
rabbitmqctl purge_queue
it is working fine
and i am trying the same like this
rabbitmqctl purge_queue -p 10.8.11.12 hello
here i am considering 10.8.11.12 as vhost. is it correct?
what actually vhost means in rabbitmq?
Even a simple link will help.
you have to use -n parameter as:
rabbitmqctl -n rabbit#your_other_machine purge_queue hello
here is an example:
./rabbitmqctl -n rabbit#srv-rabbit-cent01 purge_queue my_queue_1
where rabbit#srv-rabbit-cent01 is the rabbitmq node name. ( srv-rabbit-cent01 is the hostname )
about the vhost please read here: https://www.rabbitmq.com/uri-spec.html
2.4. Vhost
The vhost component is used as the basis for the virtual-host field of
the connection.open AMQP 0-9-1 method. Any percent-encoded octets in
the vhost should be decoded before the it is passed to the server.

Remotely shut down RabbitMQ server

I have been trying to remotely kill a rabbitmq server but haven't been lucky so far. I can easily connect to it and publish and receive messages using the pika library.
Steps I have tried so far:
Used RabbitMQ's HTTP API to DELETE a connection
/api/connections/name
An individual connection. DELETEing it
willclose the connection. Optionally set the "X-Reason"
header when DELETEing to provide a reason.'
When I tried something like http://localhost:15672/api/connection/127.0.0.1:31332, I get an error:
{"error":"Object Not Found","reason":"\"Not Found\"\n"}
Used rabbitmqadmin locally
Tried to use rabbitmqctl to remotely shut down the rabbitmq server
rabbitmqctl
Here is how to do that using rabbitmqctl
set RABBITMQ_CTL_ERL_ARGS=-setcookie FWQUGISFBWECSKWFVFRP
rabbitmqctl.bat -n rabbit#gabriele-VirtualBox stop
Erlang
Here is one way to kill a remote node using Erlang:
erl -setcookie FXQUEISFFRECSKWCVB -sname thekiller#gabriele-VirtualBox
Eshell V6.4 (abort with ^G)
(thekiller#gabriele-VirtualBox)1> net_adm:ping('rabbit#gabriele-VirtualBox').
pong
(thekiller#gabriele-VirtualBox)2> rpc:call('rabbit#gabriele-VirtualBox', init, stop, []).
ok
(thekiller#gabriele-VirtualBox)3>
Start erl console using your .erlang.cookie and with -sname
with same rabbitmq domain (in my case gabriele-VirtualBox).
Test if you reach the node using ping
call rpc:call('rabbit#gabriele-VirtualBox', init, stop, []).
Done, you killed the remote node.
After a bit of troubleshooting all over again, I was able to use the HTTP API to kill active connections. The trick was that the whole connection name was to be url encoded.
In my case the connection name was:
127.0.0.1:31332 -> 127.0.0.1:15672
So when I tried the following I got an error:
http://localhost:15672/api/connection/127.0.0.1:31332 ==> object not found error
It worked only after I URL encoded the connection name and sending a CURL DELETE like this:
http://localhost:15672/api/connection/127.0.0.1%3A31332%20-%3E%20127.0.0.1%3A15672

RabbitMQ creating queues and bindings from command line

If I have RabbitMQ installed on my machine, is there a way to create a message queue from the command line and bind it to a certain exchange without using a client?
I think it is not possible, but I want to be sure.
Summary:
Other answers are good alternatives to what was asked for. Below are commands you can use from the command line.
First, do all the necessary prep work, e.g. install rabbit, rabbitmqadmin, and rabbitctl. The idea is to use commands from rabbitmqctl and rabbitmqadmin. You can see some command examples: https://www.rabbitmq.com/management-cli.html
Example Commands/Setup:
The following commands should give you the majority if not all of what you need:
# Get the cli and make it available to use.
wget http://127.0.0.1:15672/cli/rabbitmqadmin
chmod +x rabbitmqadmin
mv rabbitmqadmin /etc/rabbitmq
Add a user and permissions
rabbitmqctl add_user testuser testpassword
rabbitmqctl set_user_tags testuser administrator
rabbitmqctl set_permissions -p / testuser ".*" ".*" ".*"
Make a virtual host and Set Permissions
rabbitmqctl add_vhost Some_Virtual_Host
rabbitmqctl set_permissions -p Some_Virtual_Host guest ".*" ".*" ".*"
Make an Exchange
./rabbitmqadmin declare exchange --vhost=Some_Virtual_Host name=some_exchange type=direct
Make a Queue
./rabbitmqadmin declare queue --vhost=Some_Virtual_Host name=some_outgoing_queue durable=true
Make a Binding
./rabbitmqadmin --vhost="Some_Virtual_Host" declare binding source="some_exchange" destination_type="queue" destination="some_incoming_queue" routing_key="some_routing_key"
Alternative Way to Bind with Python
The following is an alternative to command line binding, as I've had issues with it sometimes and found the following python code to be more reliable.
#!/usr/bin/env python
import pika
rabbitmq_host = "127.0.0.1"
rabbitmq_port = 5672
rabbitmq_virtual_host = "Some_Virtual_Host"
rabbitmq_send_exchange = "some_exchange"
rabbitmq_rcv_exchange = "some_exchange"
rabbitmq_rcv_queue = "some_incoming_queue"
rabbitmq_rcv_key = "some_routing_key"
outgoingRoutingKeys = ["outgoing_routing_key"]
outgoingQueues = ["some_outgoing_queue "]
# The binding area
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host, rabbitmq_port, rabbitmq_virtual_host, credentials))
channel = connection.channel()
channel.queue_bind(exchange=rabbitmq_rcv_exchange, queue=rabbitmq_rcv_queue, routing_key=rabbitmq_rcv_key)
for index in range(len(outgoingRoutingKeys)):
channel.queue_bind(exchange=rabbitmq_send_exchange, queue=outgoingQueues[index], routing_key=outgoingRoutingKeys[index])
The above can be run as part of a script using python. Notice I put the outgoing stuff into arrays, which will allow you to iterate through them. This should make things easy for deploys.
Last Thoughts
I think the above should get you moving in the right direction, use google if any specific commands don't make sense or read more with rabbitmqadmin help subcommands. I tried to use variables that explain themselves.
Install the RabbitMQ management plugin. It comes with a command line tool which you can use to configure all of your queues/exchanges/etc.
Create Exchange:
rabbitmqadmin -u {user} -p {password} -V {vhost} declare exchange name={name} type={type}
Create Queue:
rabbitmqadmin -u {user} -p {password} -V {vhost} declare queue name={name}
Bind Queue to Exchange:
rabbitmqadmin -u {user} -p {password} -V {vhost} declare binding source={Exchange} destination={queue}
Maybe a little late to the party but I've done so using CURL.
For queues:
curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPUT -d'{"durable":true}' \
http://192.168.99.100:15672/api/queues/%2f/QUEUENAME
And for bindings
curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPOST -d"{\"routing_key\":\"QUEUENAME\"}" \
http://192.168.99.100:15672/api/bindings/%2f/e/EXCHANGENAME/q/QUEUENAME
Note 192.168.99.100:15672 points to my RMQ Management
If you are using Linux Debian, there's a package called "amqp-tools". Install it with
apt-get install amqp-tools
You can then use command line such as amqp-publish to send messages to your queue
amqp-publish -e exchange_name -b "your message"
Then you can collect message(s) from the queue using
amqp-get -q queue_name
or
amqp-consume -q queue_name
There are also (command line) examples from rabbitmq-c package / library. After you build it, you can send messages through command line such as
amqp_sendstring localhost 5672 amq.direct test "hello world"
Have fun ...
rabbitmqctl, the provided command line interface, doesn't expose the ability to create a queue and bind it.
It, however, is quite trivial to do it with a quick script though, and the RabbitMQ getting started guide shows several examples of it, both on the publisher as well as the consumer side.
#do some work to connect
#do some work to open a channel
channel.queue_declare(queue='helloworld')
I'm glossing over connecting, but it's a literal one liner to create a queue. The operation is also idempotent, meaning you can include the statement in a script and be safe, knowing that it won't keep recreating the queue or blowing out an existing one of the same name.
Create RabbitMq Exchange, Queue and Bindings dynamically from CLI on Windows
I already had a RabbitMQ Server installed and running with multiple queue and exchange and now wanted to create it on the fly from command line. I know it is an old question but I thought giving out this information will be helpful.
Following is what I did:
Setup
Downloaded and installed Python 2.6.6-201008-24 Windows x86-64 MSI installer , any version of python,
Download RabbitMqAdmin: RabbitMq Web User Interface has a link Command Line which navigates to http://server-name:15672/cli/ (server-name: server on which rabbitmq is installed) alternatively,use the above url and save the file as rabbitmqadmin.exe in the python exe location
eg: C:\Python26
C:\Python26\python
C:\Python26\rabbitmqadmin.exe
Code:in a batch file used the below commands
Create exchange:
c:\python26\python.exe rabbitmqadmin.exe declare exchange name=*ExchangeName1* type=topic durable=true
Create queue:
c:\python26\python.exe rabbitmqadmin.exe declare queue name=*NameofQueue1* durable=true
Create binding:
c:\python26\python.exe rabbitmqadmin.exe declare binding source=ExchangeName1 destination_type=queue destination=*NameofQueue1* routing_key=*RoutingKey1*
by executing rabbitmqadmin.exe -help -subcommands it lists all the available commands
eg: c:\python26\python.exe rabbitmqadmin.exe -help -subcommands
For me, my RabbitMQ Management deal kept trying to redirect to the https version... everything in my setup is vanilla, I don't even have a config file... anyways, my work around was to manually create rabbitmqadmin.py in the sbin folder, then fill it with https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.8.1/bin/rabbitmqadmin
Then, make sure that python is in your PATH and run this to, for example, add an exchange:
python rabbitmqadmin.py declare exchange --vhost=/ name=CompletedMessageExchange type=direct
Here is a more minimal Python example, taken from the RabbitMQ Python tutorial.
First, install pika:
sudo easy_install pika
# (or use pip)
This is all you need to send a message to localhost:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='test-queue')
channel.basic_publish(exchange='', routing_key='test-queue', body='Hello World!')
If any windows user looking for powershell based solution then there is the function I have written.
Function createQueue([string]$QueueName){
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("content-type", "application/json")
$headers.Add("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=")
$body = "{
`n `"vhost`": `"/`",
`n `"name`": `"$QueueName`",
`n `"durable`": `"true`",
`n `"arguments`": {}
`n}"
# Write-Host $body
$url='http://localhost:15672/api/queues/%2f/'+$QueueName
# Write-Host $url
$response = Invoke-RestMethod $url -Method 'PUT' -Headers $headers -Body $body
$response | ConvertTo-Json
}
Save this into helper.ps1 file and include it into your script like this
$queueNames = 'my-queue-name'
. .\helper.ps1
createQueue($queueName)
Walkthrough to Create and delete a queue in RabbitMQ:
I couldn't find a commandline command to do it. Here is how I did it in code with java.
Rabbitmq-server version 3.3.5 on Ubuntu.
List the queues, no queues yet:
sudo rabbitmqctl list_queues
[sudo] password for eric:
Listing queues ...
...done.
Put this in CreateQueue.java
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import java.util.*;
public class CreateQueue {
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-message-ttl", 60000);
channel.queueDeclare("kowalski", false, false, false, args);
channel.close();
connection.close();
}
}
Supply the jar file that came with your rabbitmq installation:
I'm using rabbitmq-client.jar version 0.9.1, use the one that comes with your version of rabbitmq.
Compile and run:
javac -cp .:rabbitmq-client.jar CreateQueue.java
java -cp .:rabbitmq-client.jar CreateQueue
It should finish without errors, check your queues now:
sudo rabbitmqctl list_queues
Listing queues ...
kowalski 0
...done.
the kowalski queue exists.
helps to bind the exchange while you're at it:
channel.queue_bind(queueName, exchange)
C-;