1,安裝Erlang,RabbitMQ是由Erlang編寫的,所以須要須要先安裝它html
2,安裝RabbitMQ,確保容許打開網絡服務(windows環境下)java
RabbitMQ就像課本中講的生產者消費者模型,我想看文章的人都懂得!python
我將採生產者採用java語言來實現,消費者使用python來實現,步驟以下:windows
3,java工程目錄結構網絡
代碼:
函數
package helloword; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; import java.io.IOException; import java.util.concurrent.TimeoutException; /** * Created by Yue on 2015/10/24. */ public class Send { public static String QUEUE_NAME="Kadima"; public static void main(String[] args) throws IOException, TimeoutException { //建立鏈接工廠 ConnectionFactory connectionFactory=new ConnectionFactory(); //設置消息隊列所在地址,可使用IP connectionFactory.setHost("localhost"); //建立鏈接 Connection connection=connectionFactory.newConnection(); //建立管道 Channel channel=connection.createChannel(); //設置管道相關屬性 channel.queueDeclare(QUEUE_NAME, false, false, false, null); //定義要發送的消息 String message="You can do it!"; //發送消息 channel.basicPublish("",QUEUE_NAME, null, message.getBytes()); System.out.println("Sent [" + message + "]"); channel.close(); connection.close(); } }
4,建立消費者:spa
import pika #須要安裝此包 pip install pika #回調函數 def callback(ch, method, properties, body): #打印接收到的信息 print " [x] Received %r" % (body,) if __name__ == '__main__': connection=pika.BlockingConnection(pika.ConnectionParameters("localhost")) channel=connection.channel() channel.queue_declare("Kadima") channel.basic_consume(callback,queue="Kadima",no_ack=True) print ' [*] Waiting for messages' #循環等待 channel.start_consuming()
5,運行查看結果:code
生產者運行結果:htm
消費者運行結果:rabbitmq
ps:程序還在運行哦!
RabbitMQ之HelloWord結束!