cleaning up and proper naming
This commit is contained in:
+2
-2
@@ -5,12 +5,12 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class TopicEventProducer {
|
public class HealthRequestProducer {
|
||||||
|
|
||||||
private final RabbitTemplate rabbitTemplate;
|
private final RabbitTemplate rabbitTemplate;
|
||||||
private final String defaultRoutingKey;
|
private final String defaultRoutingKey;
|
||||||
|
|
||||||
public TopicEventProducer(
|
public HealthRequestProducer(
|
||||||
RabbitTemplate rabbitTemplate,
|
RabbitTemplate rabbitTemplate,
|
||||||
@Value("${rabbit.producer.routing-key}") String defaultRoutingKey
|
@Value("${rabbit.producer.routing-key}") String defaultRoutingKey
|
||||||
) {
|
) {
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
package de.dev089.eventproducer.web;
|
|
||||||
|
|
||||||
import de.dev089.eventproducer.producer.TopicEventProducer;
|
|
||||||
import de.dev089.eventproducer.web.dto.EventRequest;
|
|
||||||
import de.dev089.eventproducer.web.dto.MessageRequest;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.validation.annotation.Validated;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api/events")
|
|
||||||
public class EventController {
|
|
||||||
|
|
||||||
private final TopicEventProducer topicEventProducer;
|
|
||||||
|
|
||||||
public EventController(TopicEventProducer topicEventProducer) {
|
|
||||||
this.topicEventProducer = topicEventProducer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/messages")
|
|
||||||
@ResponseStatus(HttpStatus.ACCEPTED)
|
|
||||||
public void publishMessage(@Validated @RequestBody MessageRequest request) {
|
|
||||||
Map<String, Object> envelope = new HashMap<>();
|
|
||||||
envelope.put("message", request.message());
|
|
||||||
envelope.put("sentAt", Instant.now().toString());
|
|
||||||
|
|
||||||
topicEventProducer.publishToDefaultQueue(envelope);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/{routingKey}")
|
|
||||||
@ResponseStatus(HttpStatus.ACCEPTED)
|
|
||||||
public void publish(
|
|
||||||
@PathVariable String routingKey,
|
|
||||||
@Validated @RequestBody EventRequest request
|
|
||||||
) {
|
|
||||||
Map<String, Object> envelope = new HashMap<>();
|
|
||||||
envelope.put("eventType", request.eventType());
|
|
||||||
envelope.put("payload", request.payload());
|
|
||||||
envelope.put("sentAt", Instant.now().toString());
|
|
||||||
|
|
||||||
topicEventProducer.publish(routingKey, envelope);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package de.dev089.eventproducer.web;
|
||||||
|
|
||||||
|
import de.dev089.eventproducer.producer.HealthRequestProducer;
|
||||||
|
import de.dev089.eventproducer.web.dto.MessageRequest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/events")
|
||||||
|
public class RequestProducerController {
|
||||||
|
|
||||||
|
private final HealthRequestProducer healthRequestProducer;
|
||||||
|
|
||||||
|
public RequestProducerController(HealthRequestProducer healthRequestProducer) {
|
||||||
|
this.healthRequestProducer = healthRequestProducer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/populate")
|
||||||
|
@ResponseStatus(HttpStatus.ACCEPTED)
|
||||||
|
public void publishMessage(@Validated @RequestBody MessageRequest request) {
|
||||||
|
Map<String, Object> envelope = new HashMap<>();
|
||||||
|
envelope.put("message", request.message());
|
||||||
|
envelope.put("sentAt", Instant.now().toString());
|
||||||
|
|
||||||
|
healthRequestProducer.publishToDefaultQueue(envelope);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package de.dev089.eventproducer.web.dto;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public record EventRequest(
|
|
||||||
@NotBlank String eventType,
|
|
||||||
@NotNull Map<String, Object> payload
|
|
||||||
) { }
|
|
||||||
Reference in New Issue
Block a user