Skip to content

Commit

Permalink
support gui custom queue declare arguments like 'x-' prefix, from htt…
Browse files Browse the repository at this point in the history
  • Loading branch information
oulaly committed Apr 18, 2021
1 parent ea92255 commit 3960951
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
57 changes: 53 additions & 4 deletions src/main/com/zeroclue/jmeter/protocol/amqp/AMQPSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import com.rabbitmq.client.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.apache.jmeter.testelement.ThreadListener;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

public abstract class AMQPSampler extends AbstractSampler implements ThreadListener {

Expand Down Expand Up @@ -57,6 +60,7 @@ public abstract class AMQPSampler extends AbstractSampler implements ThreadListe
private static final String QUEUE_EXCLUSIVE = "AMQPSampler.QueueExclusive";
private static final String QUEUE_AUTO_DELETE = "AMQPSampler.QueueAutoDelete";
private static final int DEFAULT_HEARTBEAT = 1;
private static final String CHANNEL_ARGUMENTS = "AMQPPublisher.ChannelArguments";

private transient ConnectionFactory factory;
private transient Connection connection;
Expand Down Expand Up @@ -122,9 +126,46 @@ private Map<String, Object> getQueueArguments() {
if(getMessageExpires() != null && !getMessageExpires().isEmpty())
arguments.put("x-expires", getMessageExpiresAsInt());

// You can explicitly set x-expires, x-message-ttl and other arguments.
// Use getChannelArguments().getArgumentsAsMap() to get String value; use argumentsAsMap(getChannelArguments()) to get Object value (Number, Boolean, String).
arguments.putAll(argumentsAsMap(getChannelArguments()));

return arguments;
}

private Map<String, Object> argumentsAsMap(Arguments args) {
PropertyIterator iter = args.getArguments().iterator();
Map<String, Object> argMap = new LinkedHashMap<String, Object>();
while (iter.hasNext()) {
Argument arg = (Argument) iter.next().getObjectValue();
if (!argMap.containsKey(arg.getName())) {
String value = arg.getValue();
Number numVal = argumentValueAsNumber(value);
Boolean boolVal = argumentValueAsBoolean(value);
argMap.put(arg.getName(), numVal != null ? numVal : (boolVal != null ? boolVal : value));
}
}
return argMap;
}

private Number argumentValueAsNumber(String value) {
if (value == null) {
return null;
}
try {
return NumberFormat.getInstance().parse(value);
} catch (ParseException e) {
return null;
}
}

private Boolean argumentValueAsBoolean(String value) {
if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
return Boolean.valueOf(value);
}
return null;
}

protected abstract Channel getChannel();
protected abstract void setChannel(Channel channel);

Expand Down Expand Up @@ -384,6 +425,14 @@ public void setQueueRedeclare(Boolean content) {
setProperty(QUEUE_REDECLARE, content);
}

public Arguments getChannelArguments() {
return (Arguments) getProperty(CHANNEL_ARGUMENTS).getObjectValue();
}

public void setChannelArguments(Arguments channelArguments) {
setProperty(new TestElementProperty(CHANNEL_ARGUMENTS, channelArguments));
}

protected void cleanup() {
try {
//getChannel().close(); // closing the connection will close the channel if it's still open
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ private void configureHeaders(AMQPPublisher sampler)
headers.clearGui();
}
}
}
}
26 changes: 26 additions & 0 deletions src/main/com/zeroclue/jmeter/protocol/amqp/gui/AMQPSamplerGui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.zeroclue.jmeter.protocol.amqp.gui;

import com.zeroclue.jmeter.protocol.amqp.AMQPSampler;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
Expand Down Expand Up @@ -42,6 +44,10 @@ public abstract class AMQPSamplerGui extends AbstractSamplerGui {

private final JLabeledTextField iterations = new JLabeledTextField("Number of samples to Aggregate");

/**
* AMQP Channel Arguments Panel (Auto parsed 'Value' type: 'Number', 'Boolean', 'String')
*/
private ArgumentsPanel channelArguments = new ArgumentsPanel("Channel Arguments");


protected abstract void setMainPanel(JPanel panel);
Expand All @@ -52,7 +58,9 @@ public abstract class AMQPSamplerGui extends AbstractSamplerGui {
@Override
public void configure(TestElement element) {
super.configure(element);

if (!(element instanceof AMQPSampler)) return;

AMQPSampler sampler = (AMQPSampler) element;

exchange.setText(sampler.getExchange());
Expand All @@ -78,9 +86,22 @@ public void configure(TestElement element) {
username.setText(sampler.getUsername());
password.setText(sampler.getPassword());
SSL.setSelected(sampler.connectionSSL());

configureChannelArguments(sampler);

log.info("AMQPSamplerGui.configure() called");
}

private void configureChannelArguments(AMQPSampler sampler)
{
Arguments sampleChannelArguments = sampler.getChannelArguments();
if (sampleChannelArguments != null) {
channelArguments.configure(sampleChannelArguments);
} else {
channelArguments.clearGui();
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -110,6 +131,8 @@ public void clearGui() {
username.setText("guest");
password.setText("guest");
SSL.setSelected(false);

channelArguments.clearGui();
}

/**
Expand Down Expand Up @@ -145,6 +168,8 @@ public void modifyTestElement(TestElement element) {
sampler.setPassword(password.getText());
sampler.setConnectionSSL(SSL.isSelected());
log.info("AMQPSamplerGui.modifyTestElement() called, set user/pass to " + username.getText() + "/" + password.getText() + " on sampler " + sampler);

sampler.setChannelArguments((Arguments) channelArguments.createTestElement());
}

protected void init() {
Expand Down Expand Up @@ -244,6 +269,7 @@ private Component makeCommonPanel() {
JPanel exchangeQueueSettings = new VerticalPanel();
exchangeQueueSettings.add(exchangeSettings);
exchangeQueueSettings.add(queueSettings);
exchangeQueueSettings.add(channelArguments);

commonPanel.add(exchangeQueueSettings, gridBagConstraintsCommon);

Expand Down

0 comments on commit 3960951

Please sign in to comment.