public int truncate(DestinationConfig dConfig) throws JMSException {
boolean keepRunning = true;
StringBuffer message = new StringBuffer();
int numToDelete = 0;
ProgressMonitor monitor = new ProgressMonitor(HermesBrowser.getBrowser(), "Deleting from " + dConfig.getName(), "Discovering size of " + dConfig.getName(), 0, 102);
monitor.setMillisToDecideToPopup(100);
monitor.setMillisToPopup(400);
try {
QueueBrowser browser = getHermes().createBrowser(dConfig);
Enumeration iter = browser.getEnumeration();
monitor.setProgress(1);
Hermes.ui.getDefaultMessageSink().add("Discovering size of " + dConfig.getName());
while (iter.hasMoreElements() && keepRunning && !monitor.isCanceled()) {
iter.nextElement();
numToDelete++;
}
browser.close();
getHermes().rollback();
if (monitor.isCanceled()) {
keepRunning = false;
}
String message2 = dConfig.getName() + " is already empty.";
if (numToDelete == 1) {
message2 = "Deleting one message from " + dConfig.getName();
} else {
message2 = "Deleting " + numToDelete + " messages from " + dConfig.getName();
}
Hermes.ui.getDefaultMessageSink().add(message2);
monitor.setProgress(2);
final Destination destination = getHermes().getDestination(dConfig.getName(), Domain.getDomain(dConfig.getDomain()));
for (int i = 0; i < numToDelete && keepRunning && !monitor.isCanceled(); i++) {
Message m = null;
while (m == null && keepRunning) {
m = getHermes().receive(destination, 10000);
}
monitor.setNote(new Long(i) + " messages deleted");
float l_i = i;
float l_numToDelete = numToDelete;
int progress = Math.round(2 + ((l_i / l_numToDelete) * 100));
monitor.setProgress(progress);
}
if (monitor.isCanceled()) {
keepRunning = false;
}
if (numToDelete > 0) {
if (keepRunning) {
getHermes().commit();
message.append("Committed. ").append(numToDelete).append(" messages deleted from " + dConfig.getName());
} else {
getHermes().rollback();
message.append("Trucate of ").append(dConfig.getName()).append(" rolled back");
}
}
} catch (Exception ex) {
message.append("During truncate of ").append(dConfig.getName()).append(": ").append(ex.getMessage());
log.error(ex);
} finally {
if (monitor != null) {
monitor.close();
}
}
Hermes.ui.getDefaultMessageSink().add(message);