提问者:小点点

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


我试图遵循hello world的例子。使用常规的ActiveMQ它可以工作,但是ActiveMQ Artemis让我头疼。我想我没有正确地进行一些配置。地址是制作的,但是它是使用多播路由制作的。我想我需要单播(队列路由)。

下面的代码片段不适用于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();
        }
    }

共1个答案

匿名用户

终于发现我做错了什么,因为Amqp没有队列和主题的配置,所以它可以在功能中定义。出于某种原因,Artemis默认创建主题(多播)。如果您需要AnyCast,您可以使用Cap能力=new Symbol[]{new Symbol("队列")}指定您的需求。对于完整的测试事实:

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