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



我正在尝试以hello世界为例。对于常规的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();
}
}

终于发现我做错了什么,因为Amqp没有队列和主题的配置,它可以在Capabilities中定义。出于某种原因,阿尔忒弥斯默认创建主题(多播(。如果您需要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();
}

相关内容

  • 没有找到相关文章

最新更新