|
1 | 1 | package com.programming.commentService.controller; |
2 | 2 |
|
3 | 3 | import java.util.List; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.UUID; |
4 | 8 |
|
5 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
6 | 10 | import org.springframework.http.HttpStatus; |
7 | 11 | import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.kafka.annotation.KafkaListener; |
8 | 13 | import org.springframework.kafka.core.KafkaTemplate; |
9 | 14 | import org.springframework.web.bind.annotation.GetMapping; |
10 | 15 | import org.springframework.web.bind.annotation.PathVariable; |
@@ -42,26 +47,68 @@ public String getServiceName() { |
42 | 47 | return "Comment Service"; |
43 | 48 | } |
44 | 49 |
|
| 50 | + private final Gson gson = new Gson(); |
| 51 | + |
| 52 | + private final ConcurrentHashMap<String, CompletableFuture<Comment>> futures = new ConcurrentHashMap<>(); |
| 53 | + private static final String SANITIZE_TOPIC = "sanitize-comments"; |
| 54 | + private static final String RESULT_TOPIC = "sanitized-comments"; |
45 | 55 |
|
46 | | - @PostMapping("/upload") |
47 | | - public ResponseEntity<?> uploadComment(@RequestBody Comment comment) { |
| 56 | + @KafkaListener(topics = RESULT_TOPIC, groupId = "comment-group") |
| 57 | + public void listenSanitizedComments(String message) { |
48 | 58 | try { |
49 | | - String commentJson = new Gson().toJson(commentRepository.save(comment)); |
50 | | - kafkaTemplate.send(TOPIC, commentJson); |
| 59 | + // Chuyển đổi JSON từ Kafka thành Comment |
| 60 | + Comment sanitizedComment = gson.fromJson(message, Comment.class); |
| 61 | + |
| 62 | + // Nếu comment không có ID, tạo một ID mới |
| 63 | + if (sanitizedComment.getId() == null) { |
| 64 | + sanitizedComment.setId(UUID.randomUUID().toString()); |
| 65 | + } |
| 66 | + |
| 67 | + // Lấy CompletableFuture từ futures map và hoàn thành nó |
| 68 | + CompletableFuture<Comment> future = futures.get(sanitizedComment.getId()); |
| 69 | + if (future != null) { |
| 70 | + future.complete(sanitizedComment); // Hoàn thành CompletableFuture với comment đã xử lý |
| 71 | + } else { |
| 72 | + // Nếu không tìm thấy CompletableFuture, log thông báo lỗi |
| 73 | + System.err.println("No future found for comment ID: " + sanitizedComment.getId()); |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + // Ghi lại thông báo lỗi nếu có bất kỳ ngoại lệ nào xảy ra |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + } |
51 | 80 |
|
| 81 | + // Nhận comment từ client và gửi tới Kafka để xử lý |
| 82 | + @PostMapping("/upload") |
| 83 | + public ResponseEntity<?> uploadComment(@RequestBody Comment comment) { |
| 84 | + // Nếu comment không có ID, tạo một ID mới |
| 85 | + if (comment.getId() == null) { |
| 86 | + comment.setId(UUID.randomUUID().toString()); |
| 87 | + } |
52 | 88 |
|
53 | | - Comment save = commentRepository.save(comment); |
| 89 | + try { |
| 90 | + // Chuyển comment sang JSON và gửi qua Kafka tới Flask (topic: sanitize-comments) |
| 91 | + String commentJson = gson.toJson(comment); |
| 92 | + kafkaTemplate.send(SANITIZE_TOPIC, commentJson); |
54 | 93 |
|
| 94 | + // Khởi tạo CompletableFuture để chờ phản hồi từ Flask qua Kafka (topic: sanitized-comments) |
| 95 | + CompletableFuture<Comment> future = new CompletableFuture<>(); |
| 96 | + futures.put(comment.getId(), future); // Lưu CompletableFuture vào futures map |
55 | 97 |
|
56 | | - //Produce message to kafka |
| 98 | + // Đợi phản hồi từ Kafka, timeout sau 30 giây |
| 99 | + Comment sanitizedComment = future.get(30, TimeUnit.SECONDS); // Timeout 30 giây |
57 | 100 |
|
58 | | - return ResponseEntity.ok(HttpStatus.CREATED); |
| 101 | + // Lưu comment đã xử lý vào MongoDB |
| 102 | + Comment savedComment = commentRepository.save(sanitizedComment); |
| 103 | + return ResponseEntity.ok(savedComment); |
59 | 104 | } catch (Exception e) { |
60 | 105 | return ResponseEntity.internalServerError().body(e.getMessage()); |
61 | 106 | } |
62 | 107 | } |
63 | 108 |
|
64 | 109 |
|
| 110 | + |
| 111 | + |
65 | 112 | @GetMapping("/get/{id}") |
66 | 113 | public ResponseEntity<?> getComment(@PathVariable String id) { |
67 | 114 | try { |
|
0 commit comments