Avatar
0
Tien Sy Beginner
Tien Sy Beginner
Queue In Java Spring Boot
Hiện tại em đang muốn tạo queue trong java (chỉ dành riêng cho service java), nhằm mục đích để chứa các request và xử lý lần lượt. Em đang phân vân không biết nên sử dụng một số queue của java như LinkedList, ... hay sử dụng message broker như rabbit mq, kafka, ... Mong mọi người hỗ trợ giúp em ạ
  • Answer
Remain: 5
3 Answers
Avatar
tvd12 Beginner
tvd12 Beginner
The Best Answer
Nếu như theo em nói là chỉ dùng trong 1 server thì LinkedList là ok em ạ. MQ chỉ nên dùng khi hệ thống của em rất phức tạp và có nhiều server nhận request và yêu cầu xử lý tuần tự
  • 1
  • Reply
Em mới học code được vài tháng cho nên có những phần em diễn đạt nó chưa được đúng, mong anh giúp đỡ. Em cản ơn anh nhiều.

Này là phần tạo queue bằng LinkedList

@Service

public class ProductServiceQueue {

// private final Queue queue = new LinkedList<();

private final Queue queue = new LinkedList<();

private final ProductServiceImpl productService;

public ProductServiceQueue(ProductServiceImpl productService) {

this.productService = productService;

processQueue();

}

// public void addToQueue(MultipartFile file) {

// queue.offer(file);

// }

public void addToQueue(MultipartFile file, SecurityContext securityContext) {

queue.offer(new QueuedProduct(file, securityContext));

}

private void processQueue() {

new Thread(() -> {

while (true) {

if (!queue.isEmpty()) {

// MultipartFile file = queue.poll();

QueuedProduct queuedProduct = queue.poll();

try {

// productService.addNewProduct(file);

productService.addNewProduct(queuedProduct.getFile(), queuedProduct.getSecurityContext());

} catch (IOException | URISyntaxException | InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

}

Em có một vấn đề đó là:

  • Khi sử dụng queue thì thay vì gọi trực tiếp thì em sẽ gọi đến queue để add request vào queue --> queue sẽ lấy ra và gọi đến method trong service của API đó --> một vấn đề phát sinh là những request gọi đến em đang xác thực bằng token (cụ thể là JWT)

Em đang xử lý bằng cách đưa thông tin xác thực (securityContext) vào trong queue cùng với request

SecurityContext securityContext = SecurityContextHolder.getContext();

productServiceQueue.addToQueue(file, securityContext);

Lúc gọi đến phương thức addNewProduct --> phải thêm một thêm tham số đầu vào cho nó (SecurityContext securityContext) --> và nhiều phương thức liên quan cũng bắt buộc phải thêm tham số này --> em thấy chưa ok lắm

Em muốn hỏi là luồng xử lý như thế đã đúng chưa ạ? Trong những dự án thực tế, đối với những request yêu cầu xác thực thì khi đưa vào queue thì sẽ xử lý như thế nào ạ?

Tại vì lúc ban đầu chưa sử dụng queue thì khi client gửi request thì sẽ kèm theo accessToken trong header của request luôn, thay vì phải truyền cho nó 1 param.

 –  Tien Sy 1713607858000
Em mới học code được vài tháng cho nên có những phần em diễn đạt nó chưa được đúng, mong anh giúp đỡ. Em cản ơn anh nhiều.

Này là phần tạo queue bằng LinkedList

@Service

public class ProductServiceQueue {

// private final Queue queue = new LinkedList<();

private final Queue queue = new LinkedList<();

private final ProductServiceImpl productService;

public ProductServiceQueue(ProductServiceImpl productService) {

this.productService = productService;

processQueue();

}

// public void addToQueue(MultipartFile file) {

// queue.offer(file);

// }

public void addToQueue(MultipartFile file, SecurityContext securityContext) {

queue.offer(new QueuedProduct(file, securityContext));

}

private void processQueue() {

new Thread(() -> {

while (true) {

if (!queue.isEmpty()) {

// MultipartFile file = queue.poll();

QueuedProduct queuedProduct = queue.poll();

try {

// productService.addNewProduct(file);

productService.addNewProduct(queuedProduct.getFile(), queuedProduct.getSecurityContext());

} catch (IOException | URISyntaxException | InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

}

Em có một vấn đề đó là:

  • Khi sử dụng queue thì thay vì gọi trực tiếp thì em sẽ gọi đến queue để add request vào queue --> queue sẽ lấy ra và gọi đến method trong service của API đó --> một vấn đề phát sinh là những request gọi đến em đang xác thực bằng token (cụ thể là JWT)

Em đang xử lý bằng cách đưa thông tin xác thực (securityContext) vào trong queue cùng với request

SecurityContext securityContext = SecurityContextHolder.getContext();

productServiceQueue.addToQueue(file, securityContext);

Lúc gọi đến phương thức addNewProduct --> phải thêm một thêm tham số đầu vào cho nó (SecurityContext securityContext) --> và nhiều phương thức liên quan cũng bắt buộc phải thêm tham số này --> em thấy chưa ok lắm

Em muốn hỏi là luồng xử lý như thế đã đúng chưa ạ? Trong những dự án thực tế, đối với những request yêu cầu xác thực thì khi đưa vào queue thì sẽ xử lý như thế nào ạ?

Tại vì lúc ban đầu chưa sử dụng queue thì khi client gửi request thì sẽ kèm theo accessToken trong header của request luôn, thay vì phải truyền cho nó 1 param.

 –  Tien Sy 1713607975000
Avatar
Tien Sy Beginner
Tien Sy Beginner
Em mới học code được vài tháng cho nên có những phần em diễn đạt nó chưa được đúng, mong anh giúp đỡ. Em cản ơn anh nhiều.

Này là phần tạo queue bằng LinkedList

@Service

public class ProductServiceQueue {

// private final Queue queue = new LinkedList<();

private final Queue queue = new LinkedList<();

private final ProductServiceImpl productService;

public ProductServiceQueue(ProductServiceImpl productService) {

this.productService = productService;

processQueue();

}

// public void addToQueue(MultipartFile file) {

// queue.offer(file);

// }

public void addToQueue(MultipartFile file, SecurityContext securityContext) {

queue.offer(new QueuedProduct(file, securityContext));

}

private void processQueue() {

new Thread(() -> {

while (true) {

if (!queue.isEmpty()) {

// MultipartFile file = queue.poll();

QueuedProduct queuedProduct = queue.poll();

try {

// productService.addNewProduct(file);

productService.addNewProduct(queuedProduct.getFile(), queuedProduct.getSecurityContext());

} catch (IOException | URISyntaxException | InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

}

Em có một vấn đề đó là:

Khi sử dụng queue thì thay vì gọi trực tiếp thì em sẽ gọi đến queue để add request vào queue --> queue sẽ lấy ra và gọi đến method trong service của API đó --> một vấn đề phát sinh là những request gọi đến em đang xác thực bằng token (cụ thể là JWT)

Em đang xử lý bằng cách đưa thông tin xác thực (securityContext) vào trong queue cùng với request

SecurityContext securityContext = SecurityContextHolder.getContext();

productServiceQueue.addToQueue(file, securityContext);

Lúc gọi đến phương thức addNewProduct --> phải thêm một thêm tham số đầu vào cho nó (SecurityContext securityContext) --> và nhiều phương thức liên quan cũng bắt buộc phải thêm tham số này --> em thấy chưa ok lắm

Em muốn hỏi là luồng xử lý như thế đã đúng chưa ạ? Trong những dự án thực tế, đối với những request yêu cầu xác thực thì khi đưa vào queue thì sẽ xử lý như thế nào ạ?

Tại vì lúc ban đầu chưa sử dụng queue thì khi client gửi request thì sẽ kèm theo accessToken trong header của request luôn, thay vì phải truyền cho nó 1 param.

  • 0
  • Reply
Avatar
tvd12 Beginner
tvd12 Beginner
SecurityContext nó ở tầng intercepter để chặn xác thực, vậy nên em không nên đưa nó vào tầng controller để xứ lý nội dung yêu cầu.

Em nên giữ lại hàm public void addToQueue(MultipartFile file) { và bỏ hàm public void addToQueue(MultipartFile file, SecurityContext securityContext) { đi nhé.

Về phần đa luồng xử lý upload file em có thể tham khảo tại đây nhé: https://github.com/youngmonkeys/ezyhttp/blob/master/ezyhttp-core/src/main/java/com/tvd12/ezyhttp/core/resources/ResourceUploadManager.java

Stackask hỗ trợ markdown, em sử dụng markdown để format code cho đẹp nhé.

  • 1
  • Reply