c# easynetq : publish , python pika : consume
1. pika consume
credentials = pika.PlainCredentials('id','pw')
# create connection
connection = pika.BlockingConnection(
pika.ConnectionParameters(host=ip, virtual_host='/', credentials=credentials))
# create channel
channel = connection.channel()
# declare queue
result = channel.queue_declare(queue='MqttTestQueue', exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='ExchangeTest',queue=queue_name, routing_key='RoutingTest')
channel.basic_consume(
queue=queue_name, on_message_callback=callback, auto_ack=True)
# start Consumne
channel.start_consuming()
def callback(self, channel, method, properties, body):
try:
...
except Exception as ex:
print('rabbit_mq_callback erorr : {ex}')
2. c# publish
public class RandomTest
{
public int First { get; set; }
public int Second { get; set; }
}
class MainTest
{
Random rd = new Random();
IBus bus = null;
IExchange exchange = null;
public MainTest()
{
bus = RabbitHutch.CreateBus("host=localhost");
exchange = bus.Advanced.ExchangeDeclare("ExchangeTest", "fanout");
MattTest();
}
public void MattTest()
{
int rand1 = rd.Next(5000);
int rand2 = rd.Next(5000);
var message = new Message<RandomTest>(new RandomTest
{
First = rand1,
Second = rand2
});
bus.Advanced.Publish<RandomTest>(exchange, "RoutingTest", false, message);
Thread.Sleep(1000);
}
}
publish 는 간단한 반복문을 사용해서 1초마다 계속해서 데이터를 보내게 했고,
consume 은 데이터가 오길 기다리다가 데이터가 오면 해당 데이터를 출력.
'Rabbitmq' 카테고리의 다른 글
[ Rabbitmq ] Rabbitmq 사용하기 2 _ Request, Response 방식 (0) | 2022.12.27 |
---|---|
[ Rabbitmq ] 서버에서 클라이언트로 hello world 보내기 (0) | 2022.12.27 |
[ Rabbitmq ] Rabbitmq 사용하기 1. Window 에 RabbitMQ 설치, management 띄우기 (0) | 2022.12.27 |
[ Rabbitmq ]Window에 Rabbitmq 설치, management 띄우기 (0) | 2022.09.02 |