嵌入式 AMQP Java Broker

我正在尝试为连接到RabbitMQ代理的Scala / Java应用程序创建集成测试。为了实现这一目标,我想要一个嵌入式代理,它讲AMQP,我在每次测试之前启动和停止。最初,我试图将ActiveMQ作为AMQP的嵌入式代理引入,但是应用程序使用RabbitMQ,因此只说AMQP版本0.9.3,而ActiveMQ需要AMQP版本1.0。

我可以使用另一个嵌入式代理来代替ActiveMQ吗?


答案 1

完全内存解决方案。根据需要替换属性。spring.*

<dependency>
  <groupId>org.apache.qpid</groupId>
  <artifactId>qpid-broker</artifactId>
  <version>6.1.1</version>
  <scope>test</scope>
</dependency>
public class EmbeddedBroker {
  public void start() {
    Broker broker = new Broker();
    BrokerOptions brokerOptions = new BrokerOptions();
    brokerOptions.setConfigProperty("qpid.amqp_port", environment.getProperty("spring.rabbitmq.port"));
    brokerOptions.setConfigProperty("qpid.broker.defaultPreferenceStoreAttributes", "{\"type\": \"Noop\"}");
    brokerOptions.setConfigProperty("qpid.vhost", environment.getProperty("spring.rabbitmq.virtual-host"));
    brokerOptions.setConfigurationStoreType("Memory");
    brokerOptions.setStartupLoggedToSystemOut(false);
    broker.startup(brokerOptions);
  }
}

添加为资源:initial-config.json

{
  "name": "Embedded Test Broker",
  "modelVersion": "6.1",
  "authenticationproviders" : [{
    "name": "password",
    "type": "Plain",
    "secureOnlyMechanisms": [],
    "users": [{"name": "guest", "password": "guest", "type": "managed"}]
  }],
  "ports": [{
    "name": "AMQP",
    "port": "${qpid.amqp_port}",
    "authenticationProvider": "password",
    "protocols": [ "AMQP_0_9_1" ],
    "transports": [ "TCP" ],
    "virtualhostaliases": [{
      "name": "${qpid.vhost}",
      "type": "nameAlias"
    }]
  }],
  "virtualhostnodes" : [{
    "name": "${qpid.vhost}",
    "type": "Memory",
    "virtualHostInitialConfiguration": "{ \"type\": \"Memory\" }"
  }]
}

答案 2

我围绕下载,提取,启动和管理RabbitMQ的过程开发了一个包装器,因此它可以像任何JVM项目控制的嵌入式服务一样工作。

检查:https://github.com/AlejandroRivera/embedded-rabbitmq

它就像这样简单:

EmbeddedRabbitMqConfig config = new EmbeddedRabbitMqConfig.Builder()
    .version(PredefinedVersion.V3_5_7)
    .build();
EmbeddedRabbitMq rabbitMq = new EmbeddedRabbitMq(config);
rabbitMq.start();
...
rabbitMq.stop();

适用于Linux,Mac和Windows。


推荐