Question
· Aug 6

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-rabbitmq...

Set setting = ##class(%External.Messaging.RabbitMQSettings).%New()

Set tClient = ##class(%External.Messaging.Client).CreateClient(setting, .tSC)

Error : 5023,"Connection cannot be established"

Thank you

Product version: IRIS 2024.1
Discussion (15)1
Log in or sign up to continue


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 

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);
    }
}

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 = 5672
 set setting.username = "guest"
 set setting.password = "guest"
 set setting.virtualHost = "/"