Changeset c164f8f for backend/GlobeGuru-backend/src/main/java/Scraper.java
- Timestamp:
- 01/09/25 18:31:38 (6 days ago)
- Branches:
- master
- Children:
- 53bad7e
- Parents:
- d4d8f61
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
backend/GlobeGuru-backend/src/main/java/Scraper.java
rd4d8f61 rc164f8f 7 7 import java.util.Iterator; 8 8 import java.util.List; 9 import java.util.concurrent.Callable; 9 10 import java.util.concurrent.ConcurrentLinkedQueue; 10 11 import java.util.concurrent.CountDownLatch; 11 12 12 public class Scraper extends Thread { 13 public class Scraper implements Callable<Void> { 14 13 15 private List<String> urls; 14 private String destination;15 private String departureDate;16 private int numberOfPeople;17 16 private ConcurrentLinkedQueue<Option> optionsQueue; 18 17 private CountDownLatch latch; 19 18 20 public Scraper( String destination, String departureDate, int numberOfPeople) {19 public Scraper() { 21 20 urls = new ArrayList<>(); 22 21 this.optionsQueue = new ConcurrentLinkedQueue<>(); 23 22 ObjectMapper mapper = new ObjectMapper(); 24 23 try { 25 JsonNode root = mapper.readTree(new File("src/main/java/URLsJSON.json")); 24 ClassLoader classLoader = getClass().getClassLoader(); 25 JsonNode root = mapper.readTree(new File(classLoader.getResource("URLsJSON.json").getFile())); 26 26 27 JsonNode urlNode = root.get("agencyurls"); 27 28 if (urlNode.isArray()) { … … 36 37 throw new RuntimeException(e); 37 38 } 38 this.destination = destination;39 this.departureDate = departureDate;40 this.numberOfPeople = numberOfPeople;41 39 this.latch = new CountDownLatch(urls.size()); 42 40 } 43 41 44 @Override 45 public void run() {42 43 public Void call() { 46 44 System.out.println("Scraper has started "); 47 45 for (String url : urls) { 48 new ScraperThread(url, destination, departureDate, numberOfPeople,optionsQueue, latch).start();46 new ScraperThread(url, optionsQueue, latch).start(); 49 47 } 50 } 51 public List<Option> getOptions() { 52 try { 53 latch.await(); // Wait for all threads to finish 54 } catch (InterruptedException e) { 55 e.printStackTrace(); 56 } 57 return new ArrayList<>(optionsQueue); 48 return null; 58 49 } 59 50 }
Note:
See TracChangeset
for help on using the changeset viewer.