|
1 | 1 | package org.baeldung.spring.amqp; |
2 | 2 |
|
3 | | -import java.util.stream.Stream; |
4 | | - |
5 | | -import org.baeldung.spring.amqp.DestinationsConfig.DestinationInfo; |
6 | | -import org.slf4j.Logger; |
7 | | -import org.slf4j.LoggerFactory; |
8 | | -import org.springframework.amqp.AmqpException; |
9 | | -import org.springframework.amqp.core.AmqpAdmin; |
10 | | -import org.springframework.amqp.core.AmqpTemplate; |
11 | | -import org.springframework.amqp.core.Binding; |
12 | | -import org.springframework.amqp.core.BindingBuilder; |
13 | | -import org.springframework.amqp.core.Exchange; |
14 | | -import org.springframework.amqp.core.ExchangeBuilder; |
15 | | -import org.springframework.amqp.core.Queue; |
16 | | -import org.springframework.amqp.core.QueueBuilder; |
17 | | -import org.springframework.beans.factory.annotation.Autowired; |
18 | | -import org.springframework.boot.CommandLineRunner; |
19 | 3 | import org.springframework.boot.SpringApplication; |
20 | 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
21 | 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
22 | | -import org.springframework.context.annotation.Bean; |
23 | | -import org.springframework.http.MediaType; |
24 | | -import org.springframework.http.ResponseEntity; |
25 | | -import org.springframework.web.bind.annotation.GetMapping; |
26 | | -import org.springframework.web.bind.annotation.PathVariable; |
27 | | -import org.springframework.web.bind.annotation.PostMapping; |
28 | | -import org.springframework.web.bind.annotation.RequestBody; |
29 | | -import org.springframework.web.bind.annotation.RestController; |
30 | | - |
31 | | -import reactor.core.publisher.Flux; |
32 | | -import reactor.core.publisher.Mono; |
33 | | -import reactor.core.scheduler.Schedulers; |
34 | 6 |
|
35 | 7 | @SpringBootApplication |
36 | 8 | @EnableConfigurationProperties(DestinationsConfig.class) |
37 | | -@RestController |
38 | 9 | public class SpringWebfluxAmqpApplication { |
39 | 10 |
|
40 | | - private static Logger log = LoggerFactory.getLogger(SpringWebfluxAmqpApplication.class); |
41 | | - |
42 | | - @Autowired |
43 | | - private AmqpTemplate amqpTemplate; |
44 | | - |
45 | | - @Autowired |
46 | | - private AmqpAdmin amqpAdmin; |
47 | | - |
48 | | - @Autowired |
49 | | - private DestinationsConfig destinationsConfig; |
50 | | - |
51 | | - |
52 | 11 | public static void main(String[] args) { |
53 | 12 | SpringApplication.run(SpringWebfluxAmqpApplication.class, args); |
54 | 13 | } |
55 | | - |
56 | | - @Bean |
57 | | - public CommandLineRunner setupQueueDestinations(AmqpAdmin amqpAdmin,DestinationsConfig destinationsConfig) { |
58 | | - |
59 | | - return (args) -> { |
60 | | - |
61 | | - log.info("[I48] Creating Destinations..."); |
62 | | - |
63 | | - destinationsConfig.getQueues() |
64 | | - .forEach((key, destination) -> { |
65 | | - |
66 | | - log.info("[I54] Creating directExchange: key={}, name={}, routingKey={}", key, destination.getExchange(), destination.getRoutingKey()); |
67 | | - |
68 | | - Exchange ex = ExchangeBuilder |
69 | | - .directExchange(destination.getExchange()) |
70 | | - .durable(true) |
71 | | - .build(); |
72 | | - |
73 | | - amqpAdmin.declareExchange(ex); |
74 | | - |
75 | | - Queue q = QueueBuilder |
76 | | - .durable(destination.getRoutingKey()) |
77 | | - .build(); |
78 | | - |
79 | | - amqpAdmin.declareQueue(q); |
80 | | - |
81 | | - Binding b = BindingBuilder.bind(q) |
82 | | - .to(ex) |
83 | | - .with(destination.getRoutingKey()) |
84 | | - .noargs(); |
85 | | - amqpAdmin.declareBinding(b); |
86 | | - |
87 | | - log.info("[I70] Binding successfully created."); |
88 | | - |
89 | | - }); |
90 | | - |
91 | | - }; |
92 | | - |
93 | | - } |
94 | | - |
95 | | - @Bean |
96 | | - public CommandLineRunner setupTopicDestinations(AmqpAdmin amqpAdmin, DestinationsConfig destinationsConfig) { |
97 | | - |
98 | | - return (args) -> { |
99 | | - |
100 | | - // For topic each consumer will have its own Queue, so no binding |
101 | | - destinationsConfig.getTopics() |
102 | | - .forEach((key, destination) -> { |
103 | | - |
104 | | - log.info("[I98] Creating TopicExchange: name={}, exchange={}", key, destination.getExchange()); |
105 | | - |
106 | | - Exchange ex = ExchangeBuilder.topicExchange(destination.getExchange()) |
107 | | - .durable(true) |
108 | | - .build(); |
109 | | - |
110 | | - amqpAdmin.declareExchange(ex); |
111 | | - |
112 | | - log.info("[I107] Topic Exchange successfully created."); |
113 | | - |
114 | | - }); |
115 | | - }; |
116 | | - } |
117 | | - |
118 | | - @PostMapping(value = "/queue/{name}") |
119 | | - public Mono<ResponseEntity<?>> sendMessageToQueue(@PathVariable String name, @RequestBody String payload) { |
120 | | - |
121 | | - // Lookup exchange details |
122 | | - final DestinationInfo d = destinationsConfig.getQueues() |
123 | | - .get(name); |
124 | | - if (d == null) { |
125 | | - // Destination not found. |
126 | | - return Mono.just(ResponseEntity.notFound().build()); |
127 | | - } |
128 | | - |
129 | | - return Mono.fromCallable(() -> { |
130 | | - |
131 | | - log.info("[I51] sendMessageToQueue: queue={}, routingKey={}", d.getExchange(), d.getRoutingKey()); |
132 | | - amqpTemplate.convertAndSend(d.getExchange(), d.getRoutingKey(), payload); |
133 | | - |
134 | | - return ResponseEntity.accepted().build(); |
135 | | - |
136 | | - }); |
137 | | - |
138 | | - } |
139 | | - |
140 | | - |
141 | | - /** |
142 | | - * Receive messages for the given queue |
143 | | - * @param name |
144 | | - * @return |
145 | | - */ |
146 | | - @GetMapping(value = "/queue/{name}", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
147 | | - public Flux<?> receiveMessagesFromQueue(@PathVariable String name) { |
148 | | - |
149 | | - final DestinationInfo d = destinationsConfig.getQueues().get(name); |
150 | | - |
151 | | - if (d == null) { |
152 | | - return Flux.just(ResponseEntity.notFound().build()); |
153 | | - } |
154 | | - |
155 | | - Stream<String> s = Stream.generate(() -> { |
156 | | - String queueName = d.getRoutingKey(); |
157 | | - |
158 | | - log.info("[I137] Polling {}", queueName); |
159 | | - |
160 | | - Object payload = amqpTemplate.receiveAndConvert(queueName,5000); |
161 | | - if ( payload == null ) { |
162 | | - payload = "No news is good news..."; |
163 | | - } |
164 | | - |
165 | | - return payload.toString(); |
166 | | - }); |
167 | | - |
168 | | - |
169 | | - return Flux |
170 | | - .fromStream(s) |
171 | | - .subscribeOn(Schedulers.elastic()); |
172 | | - |
173 | | - } |
174 | | - |
175 | | - /** |
176 | | - * send message to a given topic |
177 | | - * @param name |
178 | | - * @param payload |
179 | | - * @return |
180 | | - */ |
181 | | - @PostMapping(value = "/topic/{name}") |
182 | | - public Mono<ResponseEntity<?>> sendMessageToTopic(@PathVariable String name, @RequestBody String payload) { |
183 | | - |
184 | | - // Lookup exchange details |
185 | | - final DestinationInfo d = destinationsConfig.getTopics().get(name); |
186 | | - if (d == null) { |
187 | | - // Destination not found. |
188 | | - return Mono.just(ResponseEntity.notFound().build()); |
189 | | - } |
190 | | - |
191 | | - return Mono.fromCallable(() -> { |
192 | | - |
193 | | - log.info("[I51] sendMessageToTopic: topic={}, routingKey={}", d.getExchange(), d.getRoutingKey()); |
194 | | - amqpTemplate.convertAndSend(d.getExchange(), d.getRoutingKey(), payload); |
195 | | - |
196 | | - return ResponseEntity.accepted().build(); |
197 | | - |
198 | | - }); |
199 | | - } |
200 | | - |
201 | | - |
202 | | - @GetMapping(value = "/topic/{name}", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
203 | | - public Flux<?> receiveMessagesFromTopic(@PathVariable String name) { |
204 | | - |
205 | | - DestinationInfo d = destinationsConfig.getTopics().get(name); |
206 | | - |
207 | | - if (d == null) { |
208 | | - return Flux.just(ResponseEntity.notFound().build()); |
209 | | - } |
210 | | - |
211 | | - final Queue topicQueue = createTopicQueue(d); |
212 | | - |
213 | | - Stream<String> s = Stream.generate(() -> { |
214 | | - String queueName = topicQueue.getName(); |
215 | | - |
216 | | - log.info("[I137] Polling {}", queueName); |
217 | | - |
218 | | - try { |
219 | | - Object payload = amqpTemplate.receiveAndConvert(queueName,5000); |
220 | | - if ( payload == null ) { |
221 | | - payload = "No news is good news..."; |
222 | | - } |
223 | | - |
224 | | - return payload.toString(); |
225 | | - } |
226 | | - catch(AmqpException ex) { |
227 | | - log.warn("[W247] Received an AMQP Exception: {}", ex.getMessage()); |
228 | | - return null; |
229 | | - } |
230 | | - }); |
231 | | - |
232 | | - |
233 | | - return Flux.fromStream(s) |
234 | | - .doOnCancel(() -> { |
235 | | - log.info("[I250] doOnCancel()"); |
236 | | - amqpAdmin.deleteQueue(topicQueue.getName()); |
237 | | - }) |
238 | | - .subscribeOn(Schedulers.elastic()); |
239 | | - |
240 | | - |
241 | | - } |
242 | | - |
243 | | - |
244 | | - private Queue createTopicQueue(DestinationInfo destination) { |
245 | | - |
246 | | - Exchange ex = ExchangeBuilder.topicExchange(destination.getExchange()) |
247 | | - .durable(true) |
248 | | - .build(); |
249 | | - |
250 | | - amqpAdmin.declareExchange(ex); |
251 | | - |
252 | | - // Create a durable queue |
253 | | - Queue q = QueueBuilder |
254 | | - .durable() |
255 | | - .build(); |
256 | | - |
257 | | - amqpAdmin.declareQueue(q); |
258 | | - |
259 | | - Binding b = BindingBuilder.bind(q) |
260 | | - .to(ex) |
261 | | - .with(destination.getRoutingKey()) |
262 | | - .noargs(); |
263 | | - |
264 | | - amqpAdmin.declareBinding(b); |
265 | | - |
266 | | - return q; |
267 | | - } |
268 | | - |
269 | 14 |
|
270 | 15 | } |
0 commit comments