// Add a couple of queues
Queue queue1 = new MessagingQueue(1, "sub1", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue1.activate();
Condition condition1 = new SimpleCondition("condition1");
boolean added = office1.addBinding(new Binding(condition1, queue1, false), false);
assertTrue(added);
Queue queue2 = new MessagingQueue(1, "sub2", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue2.activate();
added = office1.addBinding(new Binding(condition1, queue2, false), false);
assertTrue(added);
// Start another office - make sure it picks up the bindings from the first node
office2 = createClusteredPostOffice(2);
// Should return all queues
Collection queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue1));
assertTrue(queues.contains(queue2));
// Add another queue on node 2
Queue queue3 = new MessagingQueue(2, "sub3", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue3.activate();
added = office2.addBinding(new Binding(condition1, queue3, false), false);
assertTrue(added);
// Make sure both nodes pick it up
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue1));
assertTrue(queues.contains(queue2));
assertTrue(queues.contains(queue3));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue1));
assertTrue(queues.contains(queue2));
assertTrue(queues.contains(queue3));
// Add another binding on node 2
Queue queue4 = new MessagingQueue(2, "sub4", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue4.activate();
added = office2.addBinding(new Binding(condition1, queue4, false), false);
assertTrue(added);
// Make sure both nodes pick it up
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(4, queues.size());
assertTrue(queues.contains(queue1));
assertTrue(queues.contains(queue2));
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(4, queues.size());
assertTrue(queues.contains(queue1));
assertTrue(queues.contains(queue2));
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
// Unbind binding 1 and binding 2
Binding removed = office1.removeBinding(queue1.getName(), false);
assertNotNull(removed);
removed = office1.removeBinding(queue2.getName(), false);
assertNotNull(removed);
// Make sure bindings are not longer available on either node
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
// Add a third office
office3 = createClusteredPostOffice(3);
// Maks sure it picks up the bindings
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
// Add another binding on node 3
Queue queue5 = new MessagingQueue(3, "sub5", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue5.activate();
added = office3.addBinding(new Binding(condition1, queue5, false), false);
assertTrue(added);
// Make sure all nodes pick it up
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
// Add a durable and a non durable binding on node 1
Queue queue6 = new MessagingQueue(1, "sub6", channelIDManager.getID(), ms, pm, true, -1, null, true);
queue6.activate();
added = office1.addBinding(new Binding(condition1, queue6, false), false);
assertTrue(added);
Queue queue7 = new MessagingQueue(1, "sub7", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue7.activate();
added = office1.addBinding(new Binding(condition1, queue7, false), false);
assertTrue(added);
// Make sure all nodes pick them up
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(5, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
assertTrue(queues.contains(queue7));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(5, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
assertTrue(queues.contains(queue7));
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(5, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
assertTrue(queues.contains(queue7));
// Stop office 1
office1.stop();
// Need to sleep since it may take some time for the view changed request to reach the
// members which causes the bindings to be removed.
Thread.sleep(3000);
// All it's bindings should be removed from the other nodes, including durable
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(3, queues.size());
assertTrue(queues.contains(queue3));
assertTrue(queues.contains(queue4));
assertTrue(queues.contains(queue5));
// Stop office 2
office2.stop();
Thread.sleep(3000);
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(1, queues.size());
assertTrue(queues.contains(queue5));
// Restart office 1 and office 2
office1.start();
office2.start();
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(2, queues.size());
assertTrue(queues.contains(queue5));
assertTrue(queues.contains(queue6));
// Stop all offices
office1.stop();
office2.stop();
office3.stop();
// Start them all
office1.start();
office2.start();
office3.start();
// Only the durable queue should survive
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(1, queues.size());
assertTrue(queues.contains(queue6));
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(1, queues.size());
assertTrue(queues.contains(queue6));
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertEquals(1, queues.size());
assertTrue(queues.contains(queue6));
//Unbind it
log.info("Removing queue6 binding");
removed = office1.removeBinding(queue6.getName(), false);
assertNotNull(removed);
queues = office1.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertTrue(queues.isEmpty());
queues = office2.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertTrue(queues.isEmpty());
queues = office3.getQueuesForCondition(condition1, false);
assertNotNull(queues);
assertTrue(queues.isEmpty());
//Bind another few more clustered
Queue queue8 = new MessagingQueue(1, "sub8", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue8.activate();
Queue queue9 = new MessagingQueue(2, "sub9", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue9.activate();
Queue queue10 = new MessagingQueue(2, "sub10", channelIDManager.getID(), ms, pm, false, -1, null, true);
queue10.activate();
//Bind on different conditions
added = office1.addBinding(new Binding(condition1, queue8, false), false);
assertTrue(added);
added = office2.addBinding(new Binding(condition1, queue9, false), false);
assertTrue(added);
Condition condition2 = new SimpleCondition("condition2");
added = office2.addBinding(new Binding(condition2, queue10, false), false);
assertTrue(added);
queues = office1.getQueuesForCondition(condition1, false);