嵌入式的RabbitMQ开源库包

16-10-10 banq
    

RabbitMQ是一款流行的消息系统,与其交互都需要透过JVM通过端口通讯,现在有一款开源库包能够将RabbitMQ作为同一个JVM内的嵌入式服务。其工作原理是当启动该库包时,会从官方仓库下载RabbitMQ库包。

安装:

 <dependency>
      <groupId>io.arivera.oss</groupId>
      <artifactId>embedded-rabbitmq</artifactId>
      <version>X.Y.Z</version>
  </dependency>
<p>

启动:

EmbeddedRabbitMqConfig config = new EmbeddedRabbitMqConfig.Builder().build();
EmbeddedRabbitMq rabbitMq = new EmbeddedRabbitMq(config);
rabbitMq.start();
<p>

当start()被调用,Embedded-RabbitMQ会从RabbitMQ.com下载最新版本,然后运行。

看看RabbitMQ是否正常工作:

ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setVirtualHost("/");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");

Connection connection = connectionFactory.newConnection();
assertThat(connection.isOpen(), equalTo(true));
Channel channel = connection.createChannel();
assertThat(channel.isOpen(), equalTo(true));

channel.close();
connection.close();
<p>

停止RabbitMQ broker:

rabbitMq.stop();

GitHub - AlejandroRivera/embedded-rabbitmq: A JVM