简单的 Java AMQP 服务器

发布于 2024-08-30 09:06:20 字数 145 浏览 10 评论 0 原文

是否有任何用 Java 编写的简单 AMQP 服务器/代理实现?

我需要用它进行本地集成测试。我想从ant/maven启动,不需要任何集群、持久化、性能等功能。只是一个类似 RabbitMQ 的模拟实例,无需安装(就像 maven pom 中的依赖项)和配置。

Are there any simple AMQP server/broker implementation written in Java?

I need to use it for local integration tests. I would like to start it from ant/maven, and I don't need any features like clustering, persistence, performance and so on. Just a mock RabbitMQ-like instance, without installation (just as a dependency at maven pom) and configuration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-09-06 09:06:20

我想说,编写测试整个应用程序的集成测试/端到端测试/自动用户验收测试是完全合法的,包括在 MQ 中完成的所有操作。您应该明智地选择引发此类事件的测试用例,因为它会大大减慢测试的反馈循环。

org.apache.qpid,您可以将其简单地包含在您的应用程序作为 mvn/gradle (mvncentral) 依赖项(gradle 示例):

testCompile 'org.apache.qpid:qpid-broker:6.0.1'

然后添加一个包含 ExternalResourceRule,该规则在测试之前启动代理,有点类似于这个相当简单的设置:

@Rule
private static final ExternalResource embeddedAMQPBroker = new ExternalResource() {
    Broker broker;

    @Override
    protected void before() throws Throwable {
        BrokerOptions brokerOptions = new BrokerOptions();
        brokerOptions.setConfigProperty("qpid.amqp_port", "55672");
        broker = new Broker();
        broker.startup(brokerOptions);
    }

    @Override
    protected void after() {
        broker.shutdown();
    }
};

未经测试,因为对我来说这不起作用,因为我的所有应用程序都包含 Jetty 9 并且 QPID(仍然)需要 Jetty<9。

I'd say it is perfectly legal to write integration tests / end-to-end tests / automated user acceptance tests that test the whole application, including everything that is done within the MQs. You should select the test cases that fire up something like this wiesely, as it drastically slows down the feedback loop of your tests.

There is org.apache.qpid, which you can simply include in your application as mvn/gradle (mvn central) dependency (gradle example):

testCompile 'org.apache.qpid:qpid-broker:6.0.1'

and then add a Rule containing a ExternalResource that fires up the broker before your tests, somewhat similar to this rather simple setup:

@Rule
private static final ExternalResource embeddedAMQPBroker = new ExternalResource() {
    Broker broker;

    @Override
    protected void before() throws Throwable {
        BrokerOptions brokerOptions = new BrokerOptions();
        brokerOptions.setConfigProperty("qpid.amqp_port", "55672");
        broker = new Broker();
        broker.startup(brokerOptions);
    }

    @Override
    protected void after() {
        broker.shutdown();
    }
};

Untested, since for me this did not work, because all my applications contain Jetty 9 and QPID (still) requires a Jetty<9.

吲‖鸣 2024-09-06 09:06:20

您正在寻找的是 AMQP 模拟对象。我真的不知道,并且怀疑您是否会找到现成的。
如果您使用 JUnit 作为测试,那么您正在执行 UNIT 测试。单元测试与集成测试不同,不包括实际读取/写入队列。
也许在这里您可以重组您的测试甚至代码以包含除队列读/写之外的所有内容?
另一种选择是,如果您已将 AMQP 包装到其他类中以实现可移植性,那么只需模拟该对象即可。

What you are looking for is an AMQP Mock Object. I really do not know of any and doubt that you will find any off the shelf.
If you are using JUnit as your testing then your are doing UNIT testing. Unit testing is different than integration testing and does not include actually reading/writing to a queue.
Maybe here you could restructure your test or even code to include everything but the read/write to the queue ?
Another option is if you have wrapped your AMQP into some other class for portability then just mock that object.

彼岸花似海 2024-09-06 09:06:20

在 2016 年发布我的答案后,最近一个新选项引起了我的注意:

RabbitMQ Mock (https://github.com/fridujo/rabbitmq-mock)项目达到了确切的目的,并且重量轻得多。它仍然是一个非常年轻的项目(2018 年 5 月才开始),但我自己能够使用它进行集成测试。

为了验证模拟是否像“真实的东西”一样工作,我首先针对 RabbitMQ 实例运行我的代码,然后切换到模拟。

After publishing my answer in 2016, a new option has come to my attention just recently:

The RabbitMQ Mock (https://github.com/fridujo/rabbitmq-mock) project serves the exact purpose, and is much lighter in weight. It is still a very young project (only started in May 2018), but I was able to use it for integration tests myself.

In order to verify the mock works like "the real thing", I first run my code against a RabbitMQ instance, and switch to the mock thereafter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文