dagarcos %!s(int64=2) %!d(string=hai) anos
pai
achega
e5ae0e76f0

+ 31 - 4
src/main/java/es/uv/garcosda/config/RabbitMQConfig.java

@@ -1,17 +1,44 @@
 package es.uv.garcosda.config;
 
+import org.springframework.amqp.core.AmqpAdmin;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.DirectExchange;
 import org.springframework.amqp.core.Queue;
 import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration
 public class RabbitMQConfig {
 
-	@Bean
-    public Queue createUserRegistrationQueue() {
-        return new Queue("q.new_user");
-    }
+	@Value("${rabbitmq.queue.name}")
+    private String queue;
+
+    @Value("${rabbitmq.exchange.name}")
+    private String exchange;
+
+    @Value("${rabbitmq.routing.key}")
+    private String routingKey;
+    
+    @SuppressWarnings("unused")
+	@Autowired
+    private AmqpAdmin amqpAdmin;
+	
+    
+    @Bean
+    public void Configure() {
+    	Queue q = new Queue(this.queue, true, false, false);
+    	DirectExchange e = new DirectExchange(this.exchange);
+    	amqpAdmin.declareQueue(q);
+    	amqpAdmin.declareExchange(e);
+    	amqpAdmin.declareBinding(BindingBuilder
+                                  .bind(q)
+                                  .to(e)
+                                  .with(routingKey));
+    } 
 	
 	@Bean
 	public Jackson2JsonMessageConverter converter() {

+ 8 - 1
src/main/java/es/uv/garcosda/controllers/MailerController.java

@@ -4,6 +4,7 @@ import java.util.List;
 
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -28,6 +29,12 @@ public class MailerController {
 	
 	@Autowired
 	RabbitTemplate rabitt;
+	
+	@Value("${rabbitmq.exchange.name}")
+    private String exchange;
+
+    @Value("${rabbitmq.routing.key}")
+    private String routingKey;
 		
 	@GetMapping("users")
 	public ResponseEntity<?> getUsers() {
@@ -81,7 +88,7 @@ public class MailerController {
 	@PostMapping("users")
 	public ResponseEntity<?> addUser(@RequestBody User user){
 		if(this.us.addUser(user)) {
-			this.rabitt.convertAndSend("", "q.new_user", user);
+			this.rabitt.convertAndSend(this.exchange, this.routingKey, user);
 			return new ResponseEntity<String>("User created", HttpStatus.CREATED);
 		}
 		else{

+ 17 - 0
src/main/resources/META-INF/additional-spring-configuration-metadata.json

@@ -0,0 +1,17 @@
+{"properties": [
+  {
+    "name": "rabbitmq.queue.name",
+    "type": "java.lang.String",
+    "description": "A description for 'rabbitmq.queue.name'"
+  },
+  {
+    "name": "rabbitmq.exchange.name",
+    "type": "java.lang.String",
+    "description": "A description for 'rabbitmq.exchange.name'"
+  },
+  {
+    "name": "rabbitmq.routing.key",
+    "type": "java.lang.String",
+    "description": "A description for 'rabbitmq.routing.key'"
+  }
+]}

+ 8 - 1
src/main/resources/application.properties

@@ -1 +1,8 @@
-server.port=8080
+server.port=8080
+spring.rabbitmq.host=localhost
+spring.rabbitmq.port=5672
+spring.rabbitmq.username=guest
+spring.rabbitmq.password=guest
+rabbitmq.queue.name=q.new_user
+rabbitmq.exchange.name=mailer
+rabbitmq.routing.key=k.new_user