From 61ecc42a66807261bb42531d2b2d160e5198f64c Mon Sep 17 00:00:00 2001 From: Aliaksandr Belik Date: Thu, 28 Oct 2021 05:37:17 +0300 Subject: [PATCH] Code cleanup, cosmetic fixes --- pom.xml | 1 + .../jmeter/protocol/amqp/AMQPConsumer.java | 47 +++++++++++-------- .../jmeter/protocol/amqp/AMQPPublisher.java | 5 +- .../jmeter/protocol/amqp/AMQPSampler.java | 25 ++++++---- .../protocol/amqp/gui/AMQPConsumerGui.java | 1 - .../protocol/amqp/gui/AMQPPublisherGui.java | 8 +++- .../protocol/amqp/gui/AMQPSamplerGui.java | 14 ++++-- 7 files changed, 65 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 9255484..b83c1c4 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ org.apache.commons commons-lang3 3.11 + provided diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java index 58e0307..369797b 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java @@ -67,7 +67,6 @@ public SampleResult sample(Entry entry) { try { initChannel(); - // only do this once per thread, otherwise it slows down the consumption by appx 50% if (consumer == null) { log.info("Creating consumer"); @@ -98,7 +97,7 @@ public SampleResult sample(Entry entry) { delivery = consumer.nextDelivery(getReceiveTimeoutAsInt()); if (delivery == null) { - result.setResponseMessage("timed out"); + result.setResponseMessage("Timed out"); return result; } @@ -150,7 +149,7 @@ public SampleResult sample(Entry entry) { } catch (InterruptedException e) { consumer = null; consumerTag = null; - log.info("interuppted while attempting to consume"); + log.info("Interupted while attempting to consume"); result.setResponseCode("200"); result.setResponseMessage(e.getMessage()); } catch (IOException e) { @@ -253,21 +252,21 @@ public void setUseTx(Boolean tx) { } /** - * set whether the sampler should read the response or not + * return whether the sampler should read the response * - * @param read whether the sampler should read the response or not + * @return whether the sampler should read the response */ - public void setReadResponse(Boolean read) { - setProperty(READ_RESPONSE, read); + public String getReadResponse() { + return getPropertyAsString(READ_RESPONSE); } /** - * return whether the sampler should read the response + * set whether the sampler should read the response or not * - * @return whether the sampler should read the response + * @param read whether the sampler should read the response or not */ - public String getReadResponse() { - return getPropertyAsString(READ_RESPONSE); + public void setReadResponse(Boolean read) { + setProperty(READ_RESPONSE, read); } /** @@ -292,7 +291,7 @@ public boolean interrupt() { public void testEnded() { if (purgeQueue()) { - log.info("Purging queue " + getQueue()); + log.info("Purging queue {}", getQueue()); try { channel.queuePurge(getQueue()); @@ -336,7 +335,7 @@ private void trace(String s) { String tl = getTitle(); String tn = Thread.currentThread().getName(); String th = this.toString(); - log.debug(tn + " " + tl + " " + s + " " + th); + log.debug("{} {} {} {}", tn, tl, s, th); } protected boolean initChannel() throws IOException, NoSuchAlgorithmException, KeyManagementException, TimeoutException { @@ -354,15 +353,25 @@ private String formatHeaders(QueueingConsumer.Delivery delivery) { Map headers = delivery.getProperties().getHeaders(); StringBuilder sb = new StringBuilder(); - sb.append(TIMESTAMP_PARAMETER).append(": ") - .append(delivery.getProperties().getTimestamp() != null - ? delivery.getProperties().getTimestamp().getTime() : "").append("\n"); - sb.append(EXCHANGE_PARAMETER).append(": ").append(delivery.getEnvelope().getExchange()).append("\n"); - sb.append(ROUTING_KEY_PARAMETER).append(": ").append(delivery.getEnvelope().getRoutingKey()).append("\n"); + sb.append(TIMESTAMP_PARAMETER) + .append(": ") + .append(delivery.getProperties().getTimestamp() != null ? delivery.getProperties().getTimestamp().getTime() : "") + .append("\n"); + sb.append(EXCHANGE_PARAMETER) + .append(": ") + .append(delivery.getEnvelope().getExchange()) + .append("\n"); + sb.append(ROUTING_KEY_PARAMETER) + .append(": ") + .append(delivery.getEnvelope().getRoutingKey()) + .append("\n"); sb.append(DELIVERY_TAG_PARAMETER).append(": ").append(delivery.getEnvelope().getDeliveryTag()).append("\n"); for (String key : headers.keySet()) { - sb.append(key).append(": ").append(headers.get(key)).append("\n"); + sb.append(key) + .append(": ") + .append(headers.get(key)) + .append("\n"); } return sb.toString(); diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java index 3b81de4..901b737 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java @@ -332,7 +332,10 @@ private String formatHeaders() { StringBuilder sb = new StringBuilder(); for (String key : headers.keySet()) { - sb.append(key).append(": ").append(headers.get(key)).append("\n"); + sb.append(key) + .append(": ") + .append(headers.get(key)) + .append("\n"); } return sb.toString(); diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java index fe81229..9a3dc0c 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java @@ -90,8 +90,7 @@ protected boolean initChannel() throws IOException, NoSuchAlgorithmException, Ke try { if (channel != null && !channel.isOpen()) { - log.warn("channel " + channel.getChannelNumber() - + " closed unexpectedly: ", channel.getCloseReason()); + log.warn("Channel {} closed unexpectedly: {}", channel.getChannelNumber(), channel.getCloseReason()); channel = null; // so we re-open it below } @@ -453,7 +452,7 @@ public void threadStarted() { } protected Channel createChannel() throws IOException, NoSuchAlgorithmException, KeyManagementException, TimeoutException { - log.info("Creating channel " + getVirtualHost() + ":" + getPortAsInt()); + log.info("Creating channel {}:{}", getVirtualHost(), getPortAsInt()); try { if (connection == null || !connection.isOpen()) { @@ -484,14 +483,14 @@ protected Channel createChannel() throws IOException, NoSuchAlgorithmException, addresses[i] = new Address(hosts[i], getPortAsInt()); } - log.info("Using hosts: " + Arrays.toString(hosts) + " addresses: " + Arrays.toString(addresses)); + log.info("Using hosts: {} addresses: {}", Arrays.toString(hosts), Arrays.toString(addresses)); connection = factory.newConnection(addresses); } Channel channel = connection.createChannel(); if (!channel.isOpen()) { - log.error("Failed to open channel: " + channel.getCloseReason().getLocalizedMessage()); + log.error("Failed to open channel: {}", channel.getCloseReason().getLocalizedMessage()); } return channel; @@ -506,14 +505,18 @@ protected void deleteQueue() throws IOException, NoSuchAlgorithmException, KeyMa Channel channel = createChannel(); try { - log.info("Deleting queue " + getQueue()); + log.info("Deleting queue {}", getQueue()); channel.queueDelete(getQueue()); } catch (Exception ex) { log.debug(ex.toString(), ex); // ignore it } finally { if (channel.isOpen()) { - channel.close(); + try { + channel.close(); + } catch (TimeoutException e) { + log.error("Timeout Exception: cannot close channel", e); + } } } } @@ -523,14 +526,18 @@ protected void deleteExchange() throws IOException, NoSuchAlgorithmException, Ke Channel channel = createChannel(); try { - log.info("Deleting exchange " + getExchange()); + log.info("Deleting exchange {}", getExchange()); channel.exchangeDelete(getExchange()); } catch (Exception ex) { log.debug(ex.toString(), ex); // ignore it } finally { if (channel.isOpen()) { - channel.close(); + try { + channel.close(); + } catch (TimeoutException e) { + log.error("Timeout Exception: cannot close channel", e); + } } } } diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPConsumerGui.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPConsumerGui.java index ade6a90..8e6b69e 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPConsumerGui.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPConsumerGui.java @@ -134,5 +134,4 @@ public void modifyTestElement(TestElement te) { protected void setMainPanel(JPanel panel) { mainPanel = panel; } - } diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPPublisherGui.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPPublisherGui.java index d3783bb..c0aae04 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPPublisherGui.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPPublisherGui.java @@ -1,7 +1,11 @@ package com.zeroclue.jmeter.protocol.amqp.gui; -import java.awt.*; -import javax.swing.*; +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import javax.swing.BorderFactory; +import javax.swing.JCheckBox; +import javax.swing.JPanel; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.gui.ArgumentsPanel; diff --git a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPSamplerGui.java b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPSamplerGui.java index 4b5a354..959ac9f 100644 --- a/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPSamplerGui.java +++ b/src/main/java/com/zeroclue/jmeter/protocol/amqp/gui/AMQPSamplerGui.java @@ -1,7 +1,13 @@ package com.zeroclue.jmeter.protocol.amqp.gui; -import java.awt.*; -import javax.swing.*; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import javax.swing.BorderFactory; +import javax.swing.BoxLayout; +import javax.swing.JCheckBox; +import javax.swing.JPanel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -158,8 +164,8 @@ public void modifyTestElement(TestElement element) { sampler.setIterations(iterations.getText()); - log.debug("AMQPSamplerGui.modifyTestElement() called, set user/pass to " - + username.getText() + "/" + password.getText() + " on sampler " + sampler); + log.debug("AMQPSamplerGui.modifyTestElement() called, set user/pass to {}/{} on sampler {}", + username.getText(), password.getText(), sampler); } protected void init() {