final Map<String, Object> p = new HashMap<String, Object>();
p.put("MySTATEFUL", "new://Container?type=STATEFUL");
p.put("MySTATEFUL.Capacity", "2"); //How many instances of Stateful beans can our server hold in memory?
p.put("MySTATEFUL.Frequency", "1"); //Interval in seconds between checks
p.put("MySTATEFUL.BulkPassivate", "0"); //No bulkPassivate - just passivate entities whenever it is needed
final EJBContainer container = EJBContainer.createEJBContainer(p);
//this is going to track the execution
ExecutionChannel.getInstance().addObserver(this);
{
final Context context = getContext();
CallbackCounter counterA = (CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
Assert.assertNotNull(counterA);
Assert.assertEquals("postConstruct", this.received.remove(0));
Assert.assertEquals(0, counterA.count());
Assert.assertEquals("count", this.received.remove(0));
Assert.assertEquals(1, counterA.increment());
Assert.assertEquals("increment", this.received.remove(0));
Assert.assertEquals(0, counterA.reset());
Assert.assertEquals("reset", this.received.remove(0));
Assert.assertEquals(1, counterA.increment());
Assert.assertEquals("increment", this.received.remove(0));
System.out.println("Waiting 2 seconds...");
Thread.sleep(2000);
Assert.assertEquals("preDestroy", this.received.remove(0));
try {
counterA.increment();
Assert.fail("The ejb is not supposed to be there.");
} catch (javax.ejb.NoSuchEJBException e) {
//excepted
}
context.close();
}
{
final Context context = getContext();
CallbackCounter counterA = (CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
Assert.assertEquals("postConstruct", this.received.remove(0));
Assert.assertEquals(1, counterA.increment());
Assert.assertEquals("increment", this.received.remove(0));
((CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
Assert.assertEquals("postConstruct", this.received.remove(0));
Assert.assertEquals("count", this.received.remove(0));
((CallbackCounter) context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
Assert.assertEquals("postConstruct", this.received.remove(0));
Assert.assertEquals("count", this.received.remove(0));
System.out.println("Waiting 2 seconds...");
Thread.sleep(2000);
Assert.assertEquals("prePassivate", this.received.remove(0));
context.close();
}
container.close();
Assert.assertEquals("preDestroy", this.received.remove(0));
Assert.assertEquals("preDestroy", this.received.remove(0));
Assert.assertTrue(this.received.toString(), this.received.isEmpty());