public function collectGarbage()
{
// we need the bean manager that handles all the beans
/** @var \AppserverIo\Psr\EnterpriseBeans\BeanContextInterface $beanManager */
$beanManager = $this->getApplication()->search('BeanContextInterface');
// load the map with the stateful session beans
/** @var \AppserverIo\Storage\StorageInterface $statefulSessionBeans */
$statefulSessionBeans = $beanManager->getStatefulSessionBeans();
// initialize the timestamp with the actual time
$actualTime = time();
// load the map with the SFSB lifetime data
$lifetimeMap = $statefulSessionBeans->getLifetime();
// write a log message with size of SFSBs to be garbage collected
$this->log(LogLevel::DEBUG, sprintf('Found %d SFSBs be garbage collected', sizeof($lifetimeMap)));
// iterate over the applications sessions with stateful session beans
foreach ($lifetimeMap as $identifier => $lifetime) {
// check the lifetime of the stateful session beans
if ($lifetime < $actualTime) {
// if the stateful session bean has timed out, remove it
$statefulSessionBeans->remove($identifier, array($beanManager, 'destroyBeanInstance'));
// write a log message
$this->log(LogLevel::DEBUG, sprintf('Successfully removed SFSB %s', $identifier));
// reduce CPU load
usleep(1000);
} else {
// write a log message
$this->log(LogLevel::DEBUG, sprintf('Lifetime %s of SFSB %s is > %s', $lifetime, $identifier, $actualTime));
}
}
// profile the size of the sessions
/** @var \Psr\Log\LoggerInterface $this->profileLogger */
if ($this->profileLogger) {
$this->profileLogger->debug(sprintf('Processed standard garbage collector, handling %d SFSBs', sizeof($statefulSessionBeans)));
}
}