Rabbitmq (2)

Source: Internet
Author: User
Tags rabbitmq

The last time rabbitmq was installed and vhost and user were successfully created, but the production and consumption processes were not completed yet. This time, we called this process.

The main problem last time was that the Process Code was not compiled and saved, which is actually a python program. In the past two days, I have read the basic knowledge of Python and completed it.

Basic production and consumption of Hello World:

1. Production send. py:

Go to vim and compile the production process.

!/usr/bin/env pythonimport pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))channel = connection.channel()channel.queue_declare(queue='hello')channel.basic_publish(exchange='',routing_key='hello',body='Hello World!')print " [x] Sent 'Hello World!'"connection.close()

Process: first establish a connection with the rabbitmq server. localhost indicates the local host. If you want to connect to another host, use the corresponding host address.

Declare the queue hello; because the message cannot be directly transmitted to the message queue, an exchange is required. The default exchange is used here.

'', Routing_key is the queue name, and then close the connection.

2. Consume receive. py

Go to vim and write the consumption process.

!/usr/bin/env pythonimport pikaconnection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))channel = connection.channel()channel.queue_declare(queue='hello')print ' [*] Waiting for messages. To exit press CTRL+C'def callback(ch, method, properties, body):    print " [x] Received %r" % (body,)channel.basic_consume(callback,queue='hello',no_ack=True)channel.start_consuming()

Process: establish a connection; declare a queue. The reason for this statement is that we do not know what the pre-existing queue is, so we determine our goals.

The consumption queue is the queue hello in send. py, which we abbreviated earlier. Then, we establish our consumption method callback, and then declare the consumption

The object queue is a hello queue and then started.

During the test, start the production process send. py:

$ python send.py [x] Sent 'Hello World!'

Then we will display the production ID message: [x] sent 'Hello world! '

The production process stops every execution.

Then start the consumption process:

$ python receive.py [*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!'

Here we will find the message 'Hello world! ', So that a production consumption is completed.

Payment process.

You can also open two terminals and execute the production process on one of them. We will find that our consumption ID is always displayed on the other terminal.

Information, that is, consumption while producing.

The second part is the consumption of multiple worker .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.