git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1170867 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2011-09-14 22:07:30 +00:00
parent 334ade2f5d
commit b65626689d
2 changed files with 85 additions and 5 deletions

View File

@ -16,6 +16,12 @@
*/
package org.apache.activemq.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
@ -83,7 +89,20 @@ public class JmsProducerClient extends AbstractJmsMeasurableClient {
}
try {
getConnection().start();
LOG.info("Starting to publish " + client.getMessageSize() + " byte(s) of " + messageCount + " messages...");
if (client.getMsgFileName() != null) {
LOG.info("Starting to publish " +
messageCount +
" messages from file " +
client.getMsgFileName()
);
} else {
LOG.info("Starting to publish " +
messageCount +
" messages of size " +
client.getMessageSize() +
" byte(s)."
);
}
// Send one type of message only, avoiding the creation of different messages on sending
if (!client.isCreateNewMsg()) {
@ -158,8 +177,19 @@ public class JmsProducerClient extends AbstractJmsMeasurableClient {
try {
getConnection().start();
LOG.info("Starting to publish " + client.getMessageSize() + " byte(s) messages for " + duration + " ms");
if (client.getMsgFileName() != null) {
LOG.info("Starting to publish messages from file " +
client.getMsgFileName() +
" for " +
duration +
" ms");
} else {
LOG.info("Starting to publish " +
client.getMessageSize() +
" byte(s) messages for " +
duration +
" ms");
}
// Send one type of message only, avoiding the creation of different messages on sending
if (!client.isCreateNewMsg()) {
// Create only a single message
@ -252,7 +282,11 @@ public class JmsProducerClient extends AbstractJmsMeasurableClient {
}
public TextMessage createJmsTextMessage() throws JMSException {
return createJmsTextMessage(client.getMessageSize());
if (client.getMsgFileName() != null) {
return loadJmsMessage();
} else {
return createJmsTextMessage(client.getMessageSize());
}
}
public TextMessage createJmsTextMessage(int size) throws JMSException {
@ -300,4 +334,38 @@ public class JmsProducerClient extends AbstractJmsMeasurableClient {
}
}
}
/**
* loads the message to be sent from the specified TextFile
*/
protected TextMessage loadJmsMessage() throws JMSException {
try {
// couple of sanity checks upfront
if (client.getMsgFileName() == null) {
throw new JMSException("Invalid filename specified.");
}
File f = new File(client.getMsgFileName());
if (f.isDirectory()) {
throw new JMSException("Cannot load from " +
client.getMsgFileName() +
" as it is a directory not a text file.");
}
// try to load file
BufferedReader br = new BufferedReader(new FileReader(f));
StringBuffer payload = new StringBuffer();
String tmp = null;
while ((tmp = br.readLine()) != null) {
payload.append(tmp);
}
jmsTextMessage = getSession().createTextMessage(payload.toString());
return jmsTextMessage;
} catch (FileNotFoundException ex) {
throw new JMSException(ex.getMessage());
} catch (IOException iox) {
throw new JMSException(iox.getMessage());
}
}
}

View File

@ -31,13 +31,14 @@ public class JmsProducerProperties extends JmsClientProperties {
public static final String COUNT_BASED_SENDING = "count"; // Produce a specific count of messages
public static final String DELIVERY_MODE_PERSISTENT = "persistent"; // Persistent message delivery
public static final String DELIVERY_MODE_NON_PERSISTENT = "nonpersistent"; // Non-persistent message delivery
protected String deliveryMode = DELIVERY_MODE_NON_PERSISTENT; // Message delivery mode
protected int messageSize = 1024; // Send 1kb messages by default
protected long sendCount = 1000000; // Send a million messages by default
protected long sendDuration = 5 * 60 * 1000; // Send for 5 mins by default
protected String sendType = TIME_BASED_SENDING;
protected long sendDelay = 0; // delay in milliseconds between each producer send
protected String msgFileName = null; // for sending a particular msg from a file
protected Map<String,Object> headerMap = null;
@ -153,4 +154,15 @@ public class JmsProducerProperties extends JmsClientProperties {
public void clearHeaders() {
this.headerMap.clear();
}
public void setMsgFileName(String file) {
LOG.info("\"producer.msgFileName\" specified. " +
"Will ignore setting \"producer.messageSize\".");
this.msgFileName = file;
}
public String getMsgFileName() {
return this.msgFileName;
}
}