/
MessageListener.java
131 lines (121 loc) · 4.82 KB
/
MessageListener.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright 2020 Sonu Kumar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.sonus21.rqueue.test;
import com.github.sonus21.rqueue.annotation.RqueueListener;
import com.github.sonus21.rqueue.test.dto.ChatIndexing;
import com.github.sonus21.rqueue.test.dto.Email;
import com.github.sonus21.rqueue.test.dto.FeedGeneration;
import com.github.sonus21.rqueue.test.dto.Job;
import com.github.sonus21.rqueue.test.dto.Notification;
import com.github.sonus21.rqueue.test.dto.Otp;
import com.github.sonus21.rqueue.test.dto.Reservation;
import com.github.sonus21.rqueue.test.service.ConsumedMessageService;
import com.github.sonus21.rqueue.test.service.FailureManager;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional
@Slf4j
public class MessageListener {
@NonNull private ConsumedMessageService consumedMessageService;
@NonNull private FailureManager failureManager;
@RqueueListener(value = "${job.queue.name}", active = "${job.queue.active}")
public void onMessage(Job job) throws Exception {
log.info("Job: {}", job);
if (failureManager.shouldFail(job.getId())) {
throw new Exception("Failing job task to be retried" + job);
}
consumedMessageService.save(job);
}
@RqueueListener(
value = "${notification.queue.name}",
numRetries = "${notification.queue.retry.count}",
delayedQueue = "true",
active = "${notification.queue.active}")
public void onMessage(Notification notification) throws Exception {
log.info("Notification: {}", notification);
if (failureManager.shouldFail(notification.getId())) {
throw new Exception("Failing notification task to be retried" + notification);
}
consumedMessageService.save(notification);
}
@RqueueListener(
value = "${email.queue.name}",
deadLetterQueue = "${email.dead.letter.queue.name}",
numRetries = "${email.queue.retry.count}",
delayedQueue = "true",
visibilityTimeout = "${email.execution.time}",
active = "${email.queue.active}")
public void onMessage(Email email) throws Exception {
log.info("Email: {}", email);
if (failureManager.shouldFail(email.getId())) {
throw new Exception("Failing email task to be retried" + email);
}
consumedMessageService.save(email);
}
@RqueueListener(
value = "${otp.queue}",
active = "${otp.queue.active}",
priority = "critical:5,high:3, low:2")
public void onMessage(Otp otp) throws Exception {
log.info("Otp: {}", otp);
if (failureManager.shouldFail(otp.getId())) {
throw new Exception("Failing otp task to be retried" + otp);
}
consumedMessageService.save(otp);
}
@RqueueListener(
value = "${chat.indexing.queue}",
active = "${chat.indexing.queue.active}",
priority = "10",
priorityGroup = "test")
public void onMessage(ChatIndexing chatIndexing) throws Exception {
log.info("ChatIndexing: {}", chatIndexing);
if (failureManager.shouldFail(chatIndexing.getId())) {
throw new Exception("Failing chat indexing task to be retried" + chatIndexing);
}
consumedMessageService.save(chatIndexing);
}
@RqueueListener(
value = "${feed.generation.queue}",
active = "${feed.generation.queue.active}",
priority = "50",
priorityGroup = "test")
public void onMessage(FeedGeneration feedGeneration) throws Exception {
log.info("FeedGeneration: {}", feedGeneration);
if (failureManager.shouldFail(feedGeneration.getId())) {
throw new Exception("Failing feedGeneration task to be retried" + feedGeneration);
}
consumedMessageService.save(feedGeneration);
}
@RqueueListener(
value = "${reservation.queue}",
active = "${reservation.queue.active}",
priority = "100",
priorityGroup = "test2")
public void onMessage(Reservation reservation) throws Exception {
log.info("Reservation: {}", reservation);
if (failureManager.shouldFail(reservation.getId())) {
throw new Exception("Failing reservation task to be retried" + reservation);
}
consumedMessageService.save(reservation);
}
}