public VmBuilder createAndSubmitVms(final int amount) { final List<Vm> vms = new ArrayList<>(); for (int i = 0; i < amount; i++) { final Vm vm = new VmSimple(numberOfCreatedVms++, mips, pes) .setRam(ram).setBw(bandwidth).setSize(size) .setCloudletScheduler(cloudletSchedulerSupplier.get()) .addOnHostAllocationListener(onHostAllocationListener) .addOnHostDeallocationListener(onHostDeallocationListener) .addOnCreationFailureListener(onVmCreationFailureListener) .addOnUpdateProcessingListener(onUpdateVmProcessingListener); vm.setBroker(broker); vms.add(vm); } broker.submitVmList(vms); return this; }
/** * Creates VMs and submit them to the broker. */ private void createAndSubmitVms() { Vm vm0 = createVm(0); /* Sets the Listener to intercept allocation of a Host to the Vm. * The Listener is created using Java 8 Lambda Expressions. */ vm0.addOnHostAllocationListener(eventInfo -> System.out.printf( "\n\t#EventListener: Host %d allocated to Vm %d at time %.2f\n", eventInfo.getHost().getId(), eventInfo.getVm().getId(), eventInfo.getTime())); /* Sets the listener to intercept deallocation of a Host for the Vm. * The Listener is created using Java 8 Lambda Expressions. */ vm0.addOnHostDeallocationListener(eventInfo -> System.out.printf( "\n\t#EventListener: Vm %d moved/removed from Host %d at time %.2f\n", eventInfo.getVm().getId(), eventInfo.getHost().getId(), eventInfo.getTime())); /* This VM will not be place due to lack of a suitable host. * The Listener is created using Java 8 Lambda Expressions. */ Vm vm1 = createVm(1); vm1.addOnCreationFailureListener(eventInfo -> System.out.printf( "\n\t#EventListener: Vm %d could not be placed into any host of Datacenter %d at time %.2f due to lack of a host with enough resources.\n", eventInfo.getVm().getId(), eventInfo.getDatacenter().getId(), eventInfo.getTime())); this.vmList.add(vm0); this.vmList.add(vm1); this.broker.submitVmList(vmList); }