如何从客户端

时间:2018-07-17 20:21:43

标签: activemq activemq-artemis

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

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

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 个答案:

答案 0 :(得分:0)

最后发现我做错了什么,因为Amqp没有队列和主题的配置,可以在功能中定义它。由于某些原因,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();
        }