Skip to content

Commit

Permalink
Code cleanup, cosmetic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aliesbelik committed Oct 29, 2021
1 parent 62c5363 commit 61ecc42
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 36 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
47 changes: 28 additions & 19 deletions src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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());
Expand Down Expand Up @@ -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 {
Expand All @@ -354,15 +353,25 @@ private String formatHeaders(QueueingConsumer.Delivery delivery) {
Map<String, Object> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
}
Expand All @@ -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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,4 @@ public void modifyTestElement(TestElement te) {
protected void setMainPanel(JPanel panel) {
mainPanel = panel;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 61ecc42

Please sign in to comment.