Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: amqppluggin #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -141,6 +142,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- If you need a specific main class for other purposes -->
<!-- <mainClass>your.main.class.path.MainClassName</mainClass> -->
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -184,4 +199,4 @@
<system>GitHub</system>
<url>https://github.com/aliesbelik/jmeter-amqp-plugin/actions</url>
</ciManagement>
</project>
</project>
178 changes: 116 additions & 62 deletions src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPConsumer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.zeroclue.jmeter.protocol.amqp;
package org.example.amqp_consumer;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConsumerCancelledException;
Expand All @@ -18,6 +19,7 @@
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -31,14 +33,17 @@ public class AMQPConsumer extends AMQPSampler implements Interruptible, TestStat
private static final Logger log = LoggerFactory.getLogger(AMQPConsumer.class);

private static final Map<Class<? extends Exception>, String> EXCEPTION_TO_RESPONSE_CODE = Maps.of(
IOException.class, "100",
InterruptedException.class, "200",
ConsumerCancelledException.class, "300",
ShutdownSignalException.class, "400"
IOException.class, "100",
InterruptedException.class, "200",
ConsumerCancelledException.class, "300",
ShutdownSignalException.class, "400"
);

//++ These are JMX names, and must not be changed
private static final String PREFETCH_COUNT = "AMQPConsumer.PrefetchCount";

private static final String XQMODE = "AMQPConsumer.xQueueMode";
private static final String XOVERFLOW = "AMQPConsumer.xOverflow";
private static final String READ_RESPONSE = "AMQPConsumer.ReadResponse";
private static final String PURGE_QUEUE = "AMQPConsumer.PurgeQueue";
private static final String AUTO_ACK = "AMQPConsumer.AutoAck";
Expand All @@ -56,10 +61,20 @@ public class AMQPConsumer extends AMQPSampler implements Interruptible, TestStat
public static final boolean DEFAULT_READ_RESPONSE = true;
public static final boolean DEFAULT_USE_TX = false;
private static final int DEFAULT_PREFETCH_COUNT = 0; // unlimited

private static final String DEFAULT_XQ_MODE = "";

private static final String DEFAULT_XOVERFLOW = "";
public static final String DEFAULT_PREFETCH_COUNT_STRING = Integer.toString(DEFAULT_PREFETCH_COUNT);
public static final String DEFAULT_XQMODE_STRING = DEFAULT_XQ_MODE;

public static final String DEFAULT_XOVERFLOW_STRING = DEFAULT_XOVERFLOW;
public static final String DEFAULT_RESPONSE_CODE = "500";
public static final String DEFAULT_RECEIVE_TIMEOUT = "";

public String DEFAULT_X_QUEUE_MODE_STRING = "lazy";
public String DEFAULT_X_OVERFLOW_STRING = "reject-publish";

private transient Channel channel;
private transient DeliverCallback consumer;
private transient BlockingQueue<Delivery> response;
Expand Down Expand Up @@ -107,58 +122,67 @@ public SampleResult sample(Entry entry) {
/*
* Perform the sampling
*/

// aggregate samples
// Perform the sampling
int loop = getIterationsAsInt();
result.sampleStart(); // start timing
Delivery delivery = null;
result.sampleStart(); // start timing
Delivery delivery=null;

boolean messageFound = false; // Flag to indicate if a message is found

try {
for (int idx = 0; idx < loop; idx++) {
delivery = response.poll(getReceiveTimeoutAsInt(), TimeUnit.MILLISECONDS);

if (delivery == null) {
result.setResponseMessage("Timed out");
return result;
if (delivery != null) {
messageFound = true; // Set flag to true if a message is found

/*
* Set up the sample result details
*/
if (getReadResponseAsBoolean()) {
String responseStr = new String(delivery.getBody());
result.setResponseData(responseStr, StandardCharsets.UTF_8.name());
} else {
result.setResponseData("Read response failed", StandardCharsets.UTF_8.name());
}

if (!autoAck()) {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}

// If no messages found, set the result as successful
if (!messageFound) {
result.setResponseMessage("No Messages in the queue");
result.setResponseCodeOK();
result.setSuccessful(true);
} else {
// commit the sample
if (getUseTx()) {
channel.txCommit();
}

/*
* Set up the sample result details
*/
if (getReadResponseAsBoolean()) {
String responseStr = new String(delivery.getBody());
result.setResponseData(responseStr, StandardCharsets.UTF_8.name());
} else {
result.setResponseData("Read response failed", StandardCharsets.UTF_8.name());
}
result.setDataType(SampleResult.TEXT);
result.setResponseHeaders(delivery != null ? formatHeaders(delivery) : null);

if (!autoAck()) {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}

// commit the sample
if (getUseTx()) {
channel.txCommit();
result.setResponseMessage("OK");
result.setResponseCodeOK();
result.setSuccessful(true);
}

/*
* Set up the sample result details
*/
result.setDataType(SampleResult.TEXT);
result.setResponseHeaders(delivery != null ? formatHeaders(delivery) : null);

result.setResponseMessage("OK");
result.setResponseCodeOK();
result.setSuccessful(true);
} catch(InterruptedException ie) {
Thread.currentThread().interrupt(); // re-interrupt the current thread
Thread.currentThread().interrupt(); // re-interrupt the current thread
response = null;
consumer = null;
consumerTag = null;
log.warn("Interrupted while attempting to consume", ie);
result.setResponseCode(EXCEPTION_TO_RESPONSE_CODE.get(ie.getClass()));
result.setResponseMessage(ie.getMessage());

} catch (ShutdownSignalException | ConsumerCancelledException | IOException e) {
response = null;
consumer = null;
Expand All @@ -168,7 +192,7 @@ public SampleResult sample(Entry entry) {
result.setResponseMessage(e.getMessage());
interrupt();
} finally {
result.sampleEnd(); // end timing
result.sampleEnd(); // end timing
}

trace("AMQPConsumer.sample ended");
Expand Down Expand Up @@ -257,10 +281,30 @@ public void setPrefetchCount(String prefetchCount) {
setProperty(PREFETCH_COUNT, prefetchCount);
}

public String getXqmode() {
return getPropertyAsString(XQMODE, DEFAULT_X_QUEUE_MODE_STRING);
}

public void setXqmode(String xqmode) {
setProperty(XQMODE, xqmode);
}

public String getXOverFlow() {
return getPropertyAsString(XOVERFLOW, DEFAULT_X_OVERFLOW_STRING);
}

public void setXOverFlow(String xOverFlow) {
setProperty(XOVERFLOW, xOverFlow);
}

public int getPrefetchCountAsInt() {
return getPropertyAsInt(PREFETCH_COUNT);
}

public int getPrefetchCount1AsInt() {
return getPropertyAsInt(PREFETCH_COUNT);
}

public boolean getUseTx() {
return getPropertyAsBoolean(USE_TX, DEFAULT_USE_TX);
}
Expand Down Expand Up @@ -328,16 +372,22 @@ public void testStarted(String arg0) {
@Override
public void cleanup() {
try {
if (consumerTag != null) {
channel.basicCancel(consumerTag);
if (channel != null && channel.isOpen()) {
if (consumerTag != null) {
channel.basicCancel(consumerTag);
log.info("Cancelled consumer tag: {}", consumerTag);
}
channel.close();
log.info("Closed channel");
}
} catch (IOException e) {
log.error("Couldn't safely cancel the sample {}", consumerTag, e);
} catch (Exception e) {
log.error("Failed to clean up resources", e);
}finally{
super.cleanup();
}

super.cleanup();
}

}
/**
* Helper method.
*/
Expand Down Expand Up @@ -366,40 +416,44 @@ private String formatHeaders(Delivery delivery) {

if (delivery.getProperties().getTimestamp() != null) {
sb.append(TIMESTAMP_PARAMETER)
.append(": ")
.append((delivery.getProperties().getTimestamp().getTime())/1000)
.append("\n");
.append(": ")
.append((delivery.getProperties().getTimestamp().getTime())/1000)
.append("\n");
}

sb.append(EXCHANGE_PARAMETER)
.append(": ")
.append(delivery.getEnvelope().getExchange())
.append("\n");
.append(": ")
.append(delivery.getEnvelope().getExchange())
.append("\n");
// sb.append(x_queue_mode)
// .append(": ")
// .append(delivery.getEnvelope().get)
// .append("\n");
sb.append(ROUTING_KEY_PARAMETER)
.append(": ")
.append(delivery.getEnvelope().getRoutingKey())
.append("\n");
.append(": ")
.append(delivery.getEnvelope().getRoutingKey())
.append("\n");
sb.append(DELIVERY_TAG_PARAMETER)
.append(": ")
.append(delivery.getEnvelope().getDeliveryTag())
.append("\n");
.append(": ")
.append(delivery.getEnvelope().getDeliveryTag())
.append("\n");

if (delivery.getProperties().getAppId() != null) {
sb.append(APP_ID_PARAMETER)
.append(": ")
.append(delivery.getProperties().getAppId())
.append("\n");
.append(": ")
.append(delivery.getProperties().getAppId())
.append("\n");
}

if (headers != null) {
for (Map.Entry<String,Object> entry : headers.entrySet()) {
sb.append(entry.getKey())
.append(": ")
.append(entry.getValue())
.append("\n");
.append(": ")
.append(entry.getValue())
.append("\n");
}
}

return sb.toString();
}
}
}
Loading