本文介绍了独立Apache Qpid(amqp)Junit测试示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人在独立 junit测试中有一个使用Apache Qpid的例子。

Does anyone have an example of using Apache Qpid within a standalone junit test.

理想情况下我希望能够创建一个队列我可以在我的测试中放入/获取消息。
所以我没有在我的测试中测试QPid,我将使用集成测试,但是测试处理msgs的方法非常有用,不得不模拟一大堆服务。

Ideally I want to be able to create a queue on the fly which I can put/get msgs within my test.So I'm not testing QPid within my test, I'll use integration tests for that, however be very useful to test methods handling msgs with having to mock out a load of services.

推荐答案

这是我用于QPID 0.30的设置方法(我在Spock测试中使用它,但应该可以移植到Junit的Java而没有问题)。这支持SSL连接,HTTP管理,仅使用内存启动。启动时间小于秒。与使用ActiveMQ用于相同目的相比,QPID的配置很难,但QPID符合AMQP,允许对AMQP客户端进行平滑,中立的测试(显然交换的使用不能模仿RabbitMQs实现,但基本用途就足够了)

Here is the setup method I use for QPID 0.30 (I use this in a Spock test but should be portable to Java of Junit with no problems). This supports SSL connection, the HTTP management, and uses only in-memory startup. Startup time is sub-second. Configuration for QPID is awkward compared to using ActiveMQ for the same purpose, but QPID is AMQP compliant and allows for a smooth, neutral testing for AMQP clients (obviously the use of exchanges can not mimic RabbitMQs implementation, but for basic purposes it is sufficient)

首先我创建了一个最小的test-config.json,我把它放在资源文件夹中:

First I created a minimal test-config.json which I put in the resources folder:

{
  "name": "${broker.name}",
  "modelVersion": "2.0",
  "defaultVirtualHost" : "default",
  "authenticationproviders" : [ {
    "name" : "passwordFile",
    "type" : "PlainPasswordFile",
    "path" : "${qpid.home_dir}${file.separator}etc${file.separator}passwd",
    "preferencesproviders" : [{
        "name": "fileSystemPreferences",
        "type": "FileSystemPreferences",
        "path" : "${qpid.work_dir}${file.separator}user.preferences.json"
    }]
  } ],
  "ports" : [  {
    "name" : "AMQP",
    "port" : "${qpid.amqp_port}",
    "authenticationProvider" : "passwordFile",
    "keyStore" : "default",
    "protocols": ["AMQP_0_10", "AMQP_0_8", "AMQP_0_9", "AMQP_0_9_1" ],
    "transports" : [ "SSL" ]
  }, {
    "name" : "HTTP",
    "port" : "${qpid.http_port}",
    "authenticationProvider" : "passwordFile",
    "protocols" : [ "HTTP" ]
  }],
  "virtualhostnodes" : [ {
    "name" : "default",
    "type" : "JSON",
    "virtualHostInitialConfiguration" : "{ \"type\" : \"Memory\" }"
  } ],
  "plugins" : [ {
    "type" : "MANAGEMENT-HTTP",
    "name" : "httpManagement"
  }],
  "keystores" : [ {
     "name" : "default",
        "password" : "password",
      "path": "${qpid.home_dir}${file.separator}keystore.jks"

    }]
}

I
我还需要为localhost创建一个keystore.jks文件,因为QPID代理并且RabbitMQ客户端不喜欢通过未加密的通道进行通信。我还在integTest / resources / etc中添加了一个名为passwd的文件,其中包含以下内容:

II also needed to create a keystore.jks file for localhost because the QPID broker and the RabbitMQ client do not like to communicate over an unencrypted channel. I also added a file called "passwd" in "integTest/resources/etc" that has this content:

guest:password

guest:password

以下是单元测试设置的代码:

Here is the code from the unit test setup:

def tmpFolder = Files.createTempDir()
Broker broker

def amqpPort = PortFinder.findFreePort()
def httpPort = PortFinder.findFreePort()

def qpidHomeDir = 'src/integTest/resources/'
def configFileName = "/test-config.json"






setup()方法的代码:




code for the setup() method:

   def setup() {

    broker = new Broker();
    def brokerOptions = new BrokerOptions()

    File file = new File(qpidHomeDir)
    String homePath = file.getAbsolutePath();
    log.info(' qpid home dir=' + homePath)
    log.info(' qpid work dir=' + tmpFolder.absolutePath)

    brokerOptions.setConfigProperty('qpid.work_dir', tmpFolder.absolutePath);

    brokerOptions.setConfigProperty('qpid.amqp_port',"${amqpPort}")
    brokerOptions.setConfigProperty('qpid.http_port', "${httpPort}")
    brokerOptions.setConfigProperty('qpid.home_dir', homePath);


    brokerOptions.setInitialConfigurationLocation(homePath + configFileName)
    broker.startup(brokerOptions)
    log.info('broker started')
}






清理代码()




code for cleanup()

broker.shutdown()






从Rabbit MQ客户端建立AMQP连接:


To make an AMQP connection from a Rabbit MQ client:

        ConnectionFactory factory = new ConnectionFactory();
        factory.setUri("amqp://guest:password@localhost:${amqpPort}");
        factory.useSslProtocol()

        log.info('about to make connection')


        def connection = factory.newConnection();
        //get a channel for sending the "kickoff" message
        def channel = connection.createChannel();

这篇关于独立Apache Qpid(amqp)Junit测试示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:39