Ejemplo de Acceso a AMQP

We are providing two message sending examples: One in Python, another one in C#.

Python example: Connection and message sending (idUser and password need to be replaced).

#!/usr/bin/env python
import pika
import json

credentSend = pika.PlainCredentials(idUser, password)
hostSend = ‘amqp.emtmadrid.es’
portSend = 5672

message = ‘{ “Test Message” }’
connection = pika.BlockingConnection(pika.ConnectionParameters(hostSend, portSend, ‘/’, credentSend))

channel = connection.channel()
channel.tx_select()

try:

channel.basic_publish(exchange=”,

routing_key=’messages’,
body= message,
properties=pika.BasicProperties(delivery_mode = 2, user_id= idUser))

channel.tx_commit()

except Exception as e:

print “The message can’t be published”
print e

print ” Sent ”
connection.close()

 

C# example: Connection and message sending (idUser and password need to be replaced).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;

public class LogQueue
{
public const string queue = “messages”;

public LogQueue(String message)
{
var factory = new ConnectionFactory() { HostName = “amqp.emtmadrid.es”, Port = int.Parse(“5672”), UserName = “idUser”,
Password =”password”};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{

var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();

channel.BasicPublish(exchange: “”,
routingKey: queue,
basicProperties: properties,
body: body);
}
}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *