Skip to content

Instantly share code, notes, and snippets.

@clebertsuconic
Created August 3, 2012 16:01
Show Gist options
  • Save clebertsuconic/3248965 to your computer and use it in GitHub Desktop.
Save clebertsuconic/3248965 to your computer and use it in GitHub Desktop.
public void testFailMessages() throws Exception
{
clearData();
Configuration config = createDefaultConfig();
HashMap<String, AddressSettings> settings = new HashMap<String, AddressSettings>();
AddressSettings set = new AddressSettings();
set.setAddressFullMessagePolicy(AddressFullMessagePolicy.FAIL);
settings.put(PagingTest.ADDRESS.toString(), set);
server = createServer(true, config, 1024, 5 * 1024, settings);
server.start();
locator.setBlockOnNonDurableSend(true);
locator.setBlockOnDurableSend(true);
locator.setBlockOnAcknowledge(true);
sf = createSessionFactory(locator);
ClientSession session = sf.createSession(true, true, 0);
session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
ClientMessage message = null;
int biggerMessageSize = 1024;
byte[] body = new byte[biggerMessageSize];
ByteBuffer bb = ByteBuffer.wrap(body);
for (int j = 1; j <= biggerMessageSize; j++)
{
bb.put(getSamplebyte(j));
}
// send enough messages to fill up the address
for (int i = 0; i < 32; i++)
{
message = session.createMessage(true);
message.getBodyBuffer().writeBytes(body);
producer.send(message);
}
ClientConsumer consumer = session.createConsumer(ADDRESS);
session.start();
for (int i = 0 ; i < 10; i++)
{
validateExceptionOnSending(producer, message);
}
// Receive a message.. this should release credits
ClientMessage msgReceived = consumer.receive(5000);
assertNotNull(msgReceived);
msgReceived.acknowledge();
session.commit(); // to make sure it's on the server (roundtrip)
producer.send(message);
for (int i = 0 ; i < 10; i++)
{
validateExceptionOnSending(producer, message);
}
}
/**
* This method validates if sending a message will throw an exception
* */
private void validateExceptionOnSending(ClientProducer producer, ClientMessage message)
{
HornetQException expected = null;
try
{
// after the address is full this send should fail (since the address full policy is FAIL)
producer.send(message);
}
catch (HornetQException e)
{
e.printStackTrace();
expected = e;
}
assertNotNull(expected);
assertEquals(HornetQExceptionType.ADDRESS_FULL, expected.getType());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment