gpt4 book ai didi

java - 独立 Apache Qpid (amqp) Junit 测试示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:54:37 25 4
gpt4 key购买 nike

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

理想情况下,我希望能够动态创建一个队列,我可以在测试中放置/获取消息。所以我没有在我的测试中测试 QPid,我将为此使用集成测试,但是对于测试处理消息的方法非常有用,而必须模拟服务负载。

最佳答案

这是我用于 QPID 0.30 的设置方法(我在 Spock 测试中使用它,但应该可以毫无问题地移植到 Junit 的 Java)。这支持 SSL 连接、HTTP 管理,并且仅使用内存启动。启动时间为亚秒级。与出于相同目的使用 ActiveMQ 相比,QPID 的配置很笨拙,但 QPID 符合 AMQP 标准,并允许对 AMQP 客户端进行平滑、中立的测试(显然,交换的使用不能模仿 RabbitMQ 的实现,但对于基本目的来说已经足够了)

首先,我创建了一个最小的 test-config.json,并将其放入资源文件夹中:

{
"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"

}]
}

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

guest :密码

这是单元测试设置中的代码:

类级变量:

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() 方法的代码:

   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')
}

cleanup() 代码

broker.shutdown()

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

        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();

关于java - 独立 Apache Qpid (amqp) Junit 测试示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2176043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com