tobby48 4 年前
父节点
当前提交
264dda99de

+ 22
- 0
src/main/python/kr/co/swh/lecture/opensource/rabbitmq/receive.py 查看文件

@@ -0,0 +1,22 @@
1
+import pika
2
+
3
+class Consumer: 
4
+    def __init__(self): 
5
+        self.__url = 'dev-swh.ga' 
6
+        self.__port = 5672 
7
+        self.__vhost = '/' 
8
+        self.__cred = pika.PlainCredentials('guest', 'guest') 
9
+        self.__queue = 'py'; 
10
+    
11
+    def on_message(channel, method_frame, header_frame, body): 
12
+        print('Received %s' % body) 
13
+    
14
+    def main(self): 
15
+        conn = pika.BlockingConnection(pika.ConnectionParameters(self.__url, self.__port, self.__vhost, self.__cred)) 
16
+        chan = conn.channel() 
17
+        chan.basic_consume( queue = self.__queue, on_message_callback = Consumer.on_message, auto_ack = True ) 
18
+        print('Consumer is starting...') 
19
+        chan.start_consuming() 
20
+
21
+consumer = Consumer() 
22
+consumer.main()

+ 19
- 0
src/main/python/kr/co/swh/lecture/opensource/rabbitmq/send.py 查看文件

@@ -0,0 +1,19 @@
1
+import pika
2
+
3
+class Publisher: 
4
+    def __init__(self): 
5
+        self.__url = 'dev-swh.ga' 
6
+        self.__port = 5672 
7
+        self.__vhost = '/' 
8
+        self.__cred = pika.PlainCredentials('guest', 'guest') 
9
+        self.__queue = 'py'; 
10
+        
11
+    def main(self): 
12
+        conn = pika.BlockingConnection(pika.ConnectionParameters(self.__url, self.__port, self.__vhost, self.__cred)) 
13
+        chan = conn.channel() 
14
+        chan.basic_publish( exchange = '', routing_key = self.__queue, body = 'Hello RabbitMQ' ) 
15
+        conn.close() 
16
+
17
+publisher = Publisher() 
18
+publisher.main()
19
+