Skip to content

Instantly share code, notes, and snippets.

@chirino
Last active December 25, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chirino/6891902 to your computer and use it in GitHub Desktop.
Save chirino/6891902 to your computer and use it in GitHub Desktop.
package org.apache.activemq.advisory;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.transport.TransportListener;
import org.apache.activemq.transport.tcp.TcpTransport;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import java.io.IOException;
public class LoadGenerator {
public static void main(String[] args) throws Exception {
String action = args[0];
String url = args[1];
if ("generate".equals(action)) {
System.out.println("Connecting to: " + url);
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
connection.setWatchTopicAdvisories(false);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("Connected. Generating load...");
int i = 0;
while (true) {
i++;
System.out.print(".");
TemporaryQueue queue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(queue);
consumer.close();
queue.delete();
}
} else if ("slow".equals(action)) {
System.out.println("Connecting to: " + url);
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
TcpTransport t = connection.getTransport().narrow(TcpTransport.class);
// Hook in transport a listener that slows down command processing.
final TransportListener actual = t.getTransportListener();
t.setTransportListener(new TransportListener() {
public void onCommand(Object command) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
actual.onCommand(command);
}
@Override
public void onException(IOException error) {
actual.onException(error);
}
@Override
public void transportInterupted() {
actual.transportInterupted();
}
@Override
public void transportResumed() {
actual.transportResumed();
}
});
connection.start();
System.out.println("Connected. Behaving like a slow advisory consumer.");
while(true) {
Thread.sleep(100000);
}
} else {
System.err.print("Invalid usage: unknown action: " + action);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment