简体   繁体   English

扩展AbstractJavaSamplerClient时应如何管理连接?

[英]How should I manage connections when extending AbstractJavaSamplerClient?

I am looking to extend AbstractJavaSamplerClient so that I can fire messages to RabbitMQ. 我希望扩展AbstractJavaSamplerClient以便可以将消息激发到RabbitMQ。 The current setup I have is: 我当前的设置是:

  • Have connection and channel objects as instance members 将连接和通道对象作为实例成员
  • Create the connection and channel connection in setupTest() setupTest()创建连接和通道连接
  • Send the messages in runTest() runTest()发送消息
  • Clean up the connection in teardownTest() teardownTest()清理连接

Code: 码:

package com.the.package.samplers.TheSampler

import ...
...

public final class TheSampler extends AbstractJavaSamplerClient {
    private final ConnectionFactory factory = new ConnectionFactory();
    private Connection connection = null;
    private Channel channel = null;
    ...

    @Override
    public Arguments getDefaultParameters() {
        Arguments parameters = new Arguments();
        ...
        return parameters;

    @Override
    public void setupTest(JavaSamplerContext context) {
        ...
        factory.setHost(host);
        factory.setVirtualHost(vhost);
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);

        routingKey = queue;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare(exchange, EXCHANGE_TYPE, true);
            channel.queueDeclare(queue, true, false, false, null);
            channel.queueBind(queue, exchange, routingKey);
        }
        catch(IOException e) {
            ...
        }
    }

    @Override
    public SampleResult runTest(JavaSamplerContext context) {
        ...
        channel.basicPublish(exchange, routingKey, null, message.getBytes());
        ...
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        try {
            channel.close();
            connection.close();
        }
        catch(IOException e) {
            ...
        }
    }
}

After running the JMeter test with 5 threads for some time, the message rate drops and I start seeing the following exception (repeated indefinitely): 使用5个线程运行JMeter测试一段时间后,消息速率下降,并且我开始看到以下异常(无限期重复):

ERROR - jmeter.threads.JMeterThread: Error while processing sampler 'Java Request' : com.rabbitmq.client.AlreadyClosedException: connection is already closed due to connection error; cause: java.net.SocketException: Connection reset
    at com.rabbitmq.client.impl.AMQChannel.ensureIsOpen(AMQChannel.java:190)
    at com.rabbitmq.client.impl.AMQChannel.transmit(AMQChannel.java:291)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:647)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:630)
    at com.rabbitmq.client.impl.ChannelN.basicPublish(ChannelN.java:621)
    at com.the.package.samplers.TheSampler.runTest(TheSampler.java:102)
    at org.apache.jmeter.protocol.java.sampler.JavaSampler.sample(JavaSampler.java:191)
    at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
    at java.lang.Thread.run(Unknown Source)

I tried to be a bit more safe by creating and closing the connection and channel objects in runTest() , but that incurred a huge performance hit (fires at a max of 50 messages per second, and previously was in the thousands). 我试图通过在runTest()创建和关闭连接和通道对象来提高安全性,但是却造成了巨大的性能runTest()每秒最多发送50条消息,以前是数千条)。

Is there a way to safely create a connection to RabbitMQ when extending AbstractJavaSamplerClient and running with multiple threads? 扩展AbstractJavaSamplerClient并使用多个线程运行时,是否有办法安全地建立与RabbitMQ的连接?

I don't see any issue in your code from what you show. 从您显示的内容来看,您的代码中没有任何问题。

Where is JMeter located regarding rabbitmq server ? 关于Rabbitmq服务器,JMeter位于何处? ie is there a firewall between them ? 即它们之间是否有防火墙?

You should check this: 您应该检查以下内容:

Detecting Dead TCP Connections with Heartbeats 使用心跳检测死的TCP连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM