-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTasks.java
More file actions
51 lines (44 loc) · 2.05 KB
/
Copy pathMyTasks.java
File metadata and controls
51 lines (44 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Random;
@Component
public class MyTasks {
private static int nextVehicleId = 1;
private final RestTemplate restTemplate = new RestTemplate();
private final Random random = new Random();
// Scheduled task to add a vehicle at fixed intervals
@Scheduled(fixedRate = 10000)
public void addVehicle() {
Vehicle newVehicle = generateRandomVehicle();
restTemplate.postForObject("http://localhost:8080/addVehicle", newVehicle, Vehicle.class);
System.out.println("Added vehicle: " + newVehicle);
}
// Scheduled task to delete a vehicle at fixed intervals
@Scheduled(fixedRate = 15000)
public void deleteVehicle() {
int randomId = generateRandomId(100);
restTemplate.delete("http://localhost:8080/deleteVehicle/" + randomId);
System.out.println("Deleted vehicle with ID: " + randomId);
}
// Scheduled task to update a vehicle at fixed intervals
@Scheduled(fixedRate = 20000)
public void updateVehicle() {
int randomId = generateRandomId(100);
Vehicle updatedVehicle = new Vehicle(randomId, "Updated Model", 2015, 32000);
restTemplate.put("http://localhost:8080/updateVehicle", updatedVehicle);
Vehicle updatedFromServer = restTemplate.getForObject("http://localhost:8080/getVehicle/" + randomId, Vehicle.class);
System.out.println("Updated vehicle: " + updatedFromServer);
}
// Helper method to generate a random vehicle
private Vehicle generateRandomVehicle() {
String makeModel = "Model" + random.nextInt(1000);
int year = random.nextInt(2016 - 1986) + 1986;
double retailPrice = 15000 + random.nextDouble() * 30000;
return new Vehicle(nextVehicleId++, makeModel, year, retailPrice);
}
// Helper method to generate a random ID
private int generateRandomId(int upperBound) {
return random.nextInt(upperBound);
}
}