Repository navigation
Description
Context
cloudsimplus/cloudsimplus#613 (which closed cloudsimplus/cloudsimplus#607) added 2 features to reduce the memory and code needed for listeners:
Listoverloads in the*Listenersviews ofCloudlet,VmandHost, so many objects can share a single immutable list of listeners;EventListener.once(...), for a listener that is removed automatically after being notified once.
#42 moved the examples to the listeners() API, but no example uses these 2 features yet. The examples are where users learn the recommended way to use the framework, so they should show them. The saving is small in these examples since they create few objects. It matters in large simulations that copy this code.
Line numbers below refer to commit 266719d.
Proposal
1. Share listener lists
Change the examples that add the same listener to many objects, so they create the list once and pass it to each object:
// Created once, outside the loop
final List<EventListener<CloudletVmEventInfo>> finishListeners = List.of(this::onCloudletFinish);
for (int i = 0; i < CLOUDLETS; i++) {
final var cloudlet = CloudletSimple.of(LENGTH, PES);
cloudlet.listeners().addOnFinish(finishListeners);
cloudletList.add(cloudlet);
}| Example | Line | Event |
|---|---|---|
migration/MigrationExample1 |
335, 336 | migration start and finish |
migration/ManualMigrationExample1 |
234 | migration start |
migration/InterDatacenterMigration1 |
357 | migration start |
listeners/CloudletListenersExample1 |
195 to 197 | start and finish |
listeners/CloudletListenersExample2_ResourceUsageAlongTime |
201 | update processing |
listeners/VmListenersExample2 |
173, 176 | host placement and removal |
listeners/VmListenersExample3_DynamicVmCreation |
215 | host removal |
dynamic/CreateCloudletAfterLastFinishedOne |
149 | finish |
Where the listener is added in a method called once per object (such as createVm()), the list must be created outside that method, for instance in a field. Otherwise, each object still gets its own list.
2. Use one-time listeners
These listeners remove themselves as soon as they are called, just to run once. They can use EventListener.once(...) and drop the removal line:
broker.listeners().addOnVmsCreated(EventListener.once(this::onVmsCreatedListener));| Example | Registration line | Removal line | Event |
|---|---|---|---|
migration/MigrationExample1 |
205 | 449 | VMs created |
migration/InterDatacenterMigration1 |
299 | 321 | VMs created |
power/HostActivationExample |
151 | 182 | Host shutdown |
Also update the comments and docs that describe the manual removal.
What should stay as it is
- Listeners added to a single object (e.g.
vmList.getFirst(),simulationor a singlebroker): there's nothing to share. Simulation,DatacenterBroker,DatacenterandCloudletSchedulerviews: they have noListoverloads.- Listeners that capture per-object state, such as the lambda in
MigrationExample1line 255, which captures the VM and the target Host. dynamic/DynamicHostCreation: its listener removes itself (line 177) only after a condition is met, so it must keep being called until then. A one-time listener would be removed on the first clock tick.
Acceptance criteria
- All examples compile.
- Each changed example runs to the end without exceptions, and its listeners are still called as before, comparing the output before and after the change.
🤖 Generated with Claude Code
Activity