How do I use rabbitmq?
Hi
I'm getting an error when trying to connect to rabbitmq using the link and source code below.
Is there a solution?
https://community.intersystems.com/post/how-produce-and-consume-rabbitm…
Set setting = ##class(%External.Messaging.RabbitMQSettings).%New()
Set tClient = ##class(%External.Messaging.Client).CreateClient(setting, .tSC)
Error : 5023,"Connection cannot be established"
Thank you
Comments
Hi, are you using the rabbit docker instance provided by the sample? Or are you using another rabbit server?
I'm using the rabbitmq server I installed earlier.
I configured rabbitmq the same way as in the link.
The username and password remain the same: guest/guest.
To allow services within your Docker Compose application to resolve external hosts by name, you can use the extra_hosts directive in your docker-compose.yml file. This directive adds entries to the /etc/hosts file within the container, effectively mapping hostnames to IP addresses.
Here is an example of how to configure extra_hosts:
version:'3.8'services: my_app: image:my_app_image extra_hosts: -"external-service.example.com:192.168.1.100" -"another-host.com:10.0.0.50"# Other service configurations...extra_hosts:
This key is used within a service definition to specify additional host entries.
- "hostname:ip_address":
Each entry under extra_hosts should be a string in this format, where hostname is the name you want to use within the container and ip_address is the corresponding IP address of the external host.
To access the Docker host from within a container (for local development):
For Docker Desktop on macOS and Windows, you can use the special DNS name host.docker.internal to resolve to the IP address of the host machine. On Linux, you might need to find the IP address of the docker0 bridge interface or the docker_gwbridge network.
Example using host.docker.internal (Docker Desktop):
version:'3.8'services: my_app: image:my_app_image extra_hosts: -"host.docker.internal:host-gateway"# Recommended for Docker Desktop# Other service configurations...Note: The host-gateway keyword is a convenient way for Docker Desktop to automatically resolve host.docker.internal to the correct host IP.
After making changes to your docker-compose.yml file, remember to restart your services using docker-compose up -d for the changes to take effect.
Source: Google AI
And I can connect in C# or NodeJS, but I can't connect in Iris.
This is probably happening because you're using an IRIS that's internal to Docker's internal network, while your rabbit server is on a network external to Docker. Because of this, the IRIS on this internal Docker network can't find your rabbit server, which is on a different network.
Can you post your C# or NodeJS code here?
public static async Task Main(string[] args)
{
var factory = new ConnectionFactory { HostName = "localhost" };
var connection = await factory.CreateConnectionAsync();
var channel = await connection.CreateChannelAsync();
string queueName = "temperature";
await channel.QueueBindAsync(queueName, "temperature", "temperature.current");
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += async (_, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] Received '{message}'");
};
channel.BasicConsumeAsync(queueName, true, consumer);
while (true)
{
Task.Delay(100);
}
}
Originally, C# was the consumer and Node.js was the producer.
I forced a publish on RabbitMQ, and C# was able to receive.
Node.js was able to send.
If you post the C# code here, I can see some config required to the objectscript code
thank you
but, Even if you copy only the production source code and run it locally, the result is the same.
(Instead of using RabbitCreds, I created separate guest credentials.)
.png)
iris - local
rabbitmq - local
And the MQPort?
I set it to 5672.
The sample and the article not used this:
Set setting = ##class(%External.Messaging.RabbitMQSettings).%New()
Set tClient = ##class(%External.Messaging.Client).CreateClient(setting, .tSC)
If you use only the ObjectScript code to connect Rabbit, you must set some properties into your setting variable:
set setting = ##class(%External.Messaging.RabbitMQSettings).%New()
set setting.host = "localhost"set setting.port = 5672set setting.username = "guest"set setting.password = "guest"set setting.virtualHost = "/"External.Messaging.RabbitMQSettings itself has an InitialExpression set, so no other settings are needed.
.png)
When I checked the input data, all the necessary data was entered.