<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.practicalMircorservice</groupId>
<artifactId>EventProducer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>EventProducer</name>
<description>com.practicalMircorservice </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8
</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@SpringBootApplication
@EnableBinding
@RestController
public class EventProducerApplication {
private final String Queue = "crispyBunOrder";
@Autowired
private RabbitTemplate rabbitTemplate;
public static void main(String[] args) {
SpringApplication.run(EventProducerApplication.class, args);
}
@RequestMapping(method = RequestMethod.POST, value = "/orders/{orderId}")
public
void placeOrder(@PathVariable("ord
erId") UUID orderId, @RequestParam
("itemId") Integer
itemId,@RequestParam("userName") String userName) {
CrispyBunOrder orderObject = createOrder(orderId,itemId,userName);
rabbitTemplate.convertAndSend(Queue,orderObject);
}
private CrispyBunOrder createOrder(UUID orderId,Integer itemId,
String userName){
CrispyBunOrder order = new CrispyBunOrder();
order.setItemId(itemId);
order.setOrderId(orderId);
order.setUserName(userName);
order.setOrderPlacedTime(new Date());
return order;
}
}
package com.practicalMircorservices.eventProducer;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
public class CrispyBunOrder implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6572547218488352566L;
private UUID orderId;
private Integer itemId;
private Date orderPlacedTime;
private String userName;
public UUID getOrderId() {
return orderId;
}
public void setOrderId(UUID orderId) {
this.orderId = orderId;
}
public Integer getItemId() {
return itemId;
}
public void setItemId(Integer itemId) {
this.itemId = itemId;
}
public Date getOrderPlacedTime() {
return orderPlacedTime;
}
public void setOrderPlacedTime(Date orderPlacedTime) {
this.orderPlacedTime = orderPlacedTime;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.practicalMircorservices.eventProducer;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;
@SpringBootApplication
@RabbitListener(queues = "crispyBunOrder")
@EnableAutoConfiguration
public class EventConsumerApplication {
@Bean
public Queue crispyBunOrderQueue() {
return new Queue("crispyBunOrder");
}
@RabbitHandler
public void process(@Payload CrispyBunOrder order) {
StringBuffer SB = new StringBuffer();
SB.append("New Order Received : \n");
SB.append("OrderId : " + order.getOrderId());
SB.append("\nItemId : " + order.getItemId());
SB.append("\nUserName : " + order.getUserName());
SB.append("\nDate : " + order.getOrderPlacedTime());
System.out.println(SB.toString());
}
public static void main(String[] args) throws Exception {
SpringApplication.run(EventConsumerApplication.class, args);
}
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "myQueue", durable = "true"),
exchange = @Exchange(value = "auto.exch"),
key = "orderRoutingKey")
)
curl -H "Content-Type: application/x-www-form-urlencoded" --data "itemId=
1&userName=john"
http://localhost:8080/orders/02b425c0-da2b-445d-8726-3cf4dcf4326d;