Implement your class with java.util.Observer and add the following code in constructor.
and listen the events in your class as follows:
public void update(Observable arg0, Object arg1) {
SubscriptionObject so = (SubscriptionObject) arg1;
if (
so.response.id == Constants.RESPONSE_SUBSCRIBE_VEHICLE_VARIABLE) {
if (so.variable == Constants.VAR_SPEED) {
SumoPrimitive sp = (SumoPrimitive) so.object;
System.out.println("Speed of the vehicle " + sp.val);
} else if (so.variable == Constants.VAR_POSITION) {
SumoPosition2D sc = (SumoPosition2D) so.object;
System.out.println("Position of the vehicle x = " + sc.x + " y = " + sc.y);
} else if (so.variable == Constants.VAR_DISTANCE) {
SumoPrimitive sp = (SumoPrimitive) so.object;
System.out.println("Distance of the vehicle " + sp.val);
}
} else if (
so.response.id == Constants.RESPONSE_SUBSCRIBE_SIM_VARIABLE) {
SumoStringList ssl=(SumoStringList) so.object;
if(ssl.size()>0) {
if (so.variable == Constants.VAR_DEPARTED_VEHICLES_IDS) {
System.out.println("subscription Departed vehicles: "+ssl.size());
} else if (so.variable == Constants.VAR_ARRIVED_VEHICLES_IDS) {
System.out.println("subscription Arrived vehicles: "+ssl.size());
}
}
}
}