How to Create a New Event and Make it to Execute in Order?
Share
Condition for Create a New Event and Make it to Execute in Order
Description: To create and execute a new event in order in CloudSim, define a custom event class or use a simple class with specific attributes. Schedule the event using CloudSim.send(), specifying the event, the entity (like Datacenter or VM), and the time for execution. CloudSim processes events in the order of their scheduled times, with earlier events executing first. By setting sequential times (e.g., 10, 20, 30), events execute in the intended order. When the scheduled time arrives, CloudSim triggers the event's processing method, allowing custom behavior at each event. This method ensures control over event execution order in simulations.
Sample Code
import org.cloudbus.cloudsim.*;
import org.cloudbus.cloudsim.core.CloudSim;
import org.cloudbus.cloudsim.core.SimEvent;
import java.util.Calendar;
import java.util.List;
import org.cloudbus.cloudsim.provisioners.*;
public class CloudSimEventExample {
public static void main(String[] args) {
try {
// Step 1: Initialize CloudSim
int numUsers = 1; // Number of cloud users
Calendar calendar = Calendar.getInstance();
boolean traceFlag = false; // No event tracing
CloudSim.init(numUsers, calendar, traceFlag);
// Step 2: Create Datacenter and Broker
Datacenter datacenter = createDatacenter("Datacenter_0");
DatacenterBroker broker = new CustomDatacenterBroker("Broker_0");
// Step 3: Create and schedule custom events
// Schedule event 1 at time 10
CloudSim.send(broker.getId(), broker.getId(), 10, 0, "Event 1");
// Schedule event 2 at time 20
CloudSim.send(broker.getId(), broker.getId(), 20, 0, "Event 2");
// Schedule event 3 at time 30
CloudSim.send(broker.getId(), broker.getId(), 30, 0, "Event 3");
// Step 4: Start Simulation
CloudSim.startSimulation();
// Step 5: Stop the simulation and print results
CloudSim.stopSimulation();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Datacenter createDatacenter(String name) throws Exception {
List hostList = new java.util.ArrayList<>();
int hostId = 0;
int ram = 20480; // 20 GB RAM
long storage = 1000000; // 1 TB Storage
int bw = 10000; // 10 GB Bandwidth
int cores = 8; // Number of cores
int mips = 10000; // MIPS
// Create a list of processing elements (PEs)
List peList = new java.util.ArrayList<>();
for (int i = 0; i < cores; i++) {
peList.add(new Pe(i, new PeProvisionerSimple(mips))); // Create cores
}
// Create a Host
hostList.add(new Host(
hostId,
new RamProvisionerSimple(ram),
new BwProvisionerSimple(bw),
storage,
peList,
new VmSchedulerTimeShared(peList)
));
// Create a Datacenter with the host list
String arch = "x86";
String os = "Linux";
String vmm = "Xen";
DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
arch, os, vmm, hostList, 10.0, 3.0, 0.05, 0.001, 0.0
);
return new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new java.util.LinkedList<>(), 0);
}
// Custom DatacenterBroker that can handle the custom events
static class CustomDatacenterBroker extends DatacenterBroker {
public CustomDatacenterBroker(String name) throws Exception {
super(name);
}
@Override
public void processEvent(SimEvent ev) {
// Process custom events
String eventData = (String) ev.getData();
System.out.println("Processing: " + eventData);
}
}
}