如何从客户端设置路由类型ActiveMQ Artemis

How to set routing type ActiveMQ Artemis from client

我正在尝试遵循 hello world 示例。使用常规 ActiveMQ 它可以工作,但 ActiveMQ Artemis 让我头疼。我猜有些配置我做的不正确。 Address 已创建,但它是使用多播路由创建的。我想我需要单播(队列路由)。

以下代码片段不适用于 ActiveMQ 的 artemis 版本。有可能我想做什么吗?我想自动创建一个持久队列。

public class SimpleAmqpTest
    {
        [Fact]
        public async Task TestHelloWorld()
        {
            Address address = new Address("amqp://guest:guest@localhost:5672");
            Connection connection = await Connection.Factory.CreateAsync(address);
            Session session = new Session(connection);

            Message message = new Message("Hello AMQP");

            var target = new Target
            {
                Address = "simple-queue",
                Durable = 1,
            };

            SenderLink sender = new SenderLink(session, "sender-link", target, null);
            await sender.SendAsync(message);

            ReceiverLink receiver = new ReceiverLink(session, "receiver-link", "simple-queue");
            message = await receiver.ReceiveAsync();
            receiver.Accept(message);

            await sender.CloseAsync();
            await receiver.CloseAsync();
            await session.CloseAsync();
            await connection.CloseAsync();
        }
    }

终于找到我哪里做错了,因为Amqp没有queues和topics的配置,可以在Capabilities中定义。出于某种原因,Artemis 默认创建主题(多播)。如果您需要 AnyCast,您可以使用 Capabilities = new Symbol[] { new Symbol("queue") } 指定您的需求。对于完整的测试事实:

public async Task TestHelloWorld()
        {
            //strange, works using regular activeMQ and the amqp test broker from here: http://azure.github.io/amqpnetlite/articles/hello_amqp.html
            //but this does not work in ActiveMQ Artemis
            Address address = new Address("amqp://guest:guest@localhost:5672");
            Connection connection = await Connection.Factory.CreateAsync(address);
            Session session = new Session(connection);

            Message message = new Message("Hello AMQP");

            Target target = new Target
            {
                Address = "q1",
                Capabilities = new Symbol[] { new Symbol("queue") }
            };

            SenderLink sender = new SenderLink(session, "sender-link", target, null);
            sender.Send(message);

            Source source = new Source
            {
                Address = "q1",
                Capabilities = new Symbol[] { new Symbol("queue") }
            };

            ReceiverLink receiver = new ReceiverLink(session, "receiver-link", source, null);
            message = await receiver.ReceiveAsync();
            receiver.Accept(message);

            await sender.CloseAsync();
            await receiver.CloseAsync();
            await session.CloseAsync();
            await connection.CloseAsync();
        }