public void testThrottle() throws Exception {
// Retrieve a delivery channel
TestComponent component = new TestComponent(new QName("service"), "endpoint");
container.activateComponent(new ActivationSpec("component", component));
final DeliveryChannel channel = component.getChannel();
// test
ComponentMBeanImpl componentMbeanImpl = container.getRegistry().getComponent("component");
assertNotNull(componentMbeanImpl);
componentMbeanImpl.setExchangeThrottling(true);
componentMbeanImpl.setThrottlingTimeout(4000);
class ProviderThread extends Thread {
private int counter;
private DeliveryChannel channel;
public ProviderThread(int counter, DeliveryChannel channel) {
this.counter = counter;
this.channel = channel;
}
public void run() {
try {
InOut me = (InOut) channel.accept(10000);
NormalizedMessage nm = me.createMessage();
nm.setContent(new StringSource("<response>" + counter
+ "</response>"));
me.setOutMessage(nm);
channel.sendSync(me);
} catch (MessagingException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
for (int i = 0; i < 6; i++) {
MessageExchangeFactory factory = channel.createExchangeFactoryForService(new QName("service"));
InOut me = factory.createInOutExchange();
NormalizedMessage nm = me.createMessage();
nm.setContent(new StringSource("<request>" + i + "</request>"));
me.setInMessage(nm);
Thread t = new ProviderThread(i, channel);
t.start();
long before = System.currentTimeMillis();
channel.sendSync(me, 5000);
long after = System.currentTimeMillis();
if (i % 2 == 1) {
// throttle sleep 4000ms for every 2 message, so
// the duration should > 4000ms
assertTrue(after - before > 4000);
} else {
assertTrue(after - before < 4000);
}
assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
me.setStatus(ExchangeStatus.DONE);
channel.send(me);
t.join();
}
}