BAEL-3457 Apache RocketMQ
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.rocketmq.consumer;
|
||||
|
||||
import com.baeldung.rocketmq.event.CartItemEvent;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CartEventConsumer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CartEventConsumer.class, args);
|
||||
}
|
||||
|
||||
@Service
|
||||
@RocketMQMessageListener(topic = "cart-item-add-topic", consumerGroup = "cart-consumer_cart-item-add-topic")
|
||||
public class CardItemAddConsumer implements RocketMQListener<CartItemEvent> {
|
||||
public void onMessage(CartItemEvent addItemEvent) {
|
||||
System.out.println("Adding item: " + addItemEvent);
|
||||
// logic
|
||||
}
|
||||
}
|
||||
|
||||
@Service
|
||||
@RocketMQMessageListener(topic = "cart-item-removed-topic", consumerGroup = "cart-consumer_cart-item-removed-topic")
|
||||
public class CardItemRemoveConsumer implements RocketMQListener<CartItemEvent> {
|
||||
public void onMessage(CartItemEvent removeItemEvent) {
|
||||
System.out.println("Removing item: " + removeItemEvent);
|
||||
// logic
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.rocketmq.event;
|
||||
|
||||
public class CartItemEvent {
|
||||
private String itemId;
|
||||
private int quantity;
|
||||
|
||||
public CartItemEvent(String itemId, int quantity) {
|
||||
this.itemId = itemId;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public void setItemId(String itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CartItemEvent{" + "itemId='" + itemId + '\'' + ", quantity=" + quantity + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.rocketmq.producer;
|
||||
|
||||
|
||||
import com.baeldung.rocketmq.event.CartItemEvent;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CartEventProducer implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CartEventProducer.class, args);
|
||||
}
|
||||
|
||||
public void run(String... args) throws Exception {
|
||||
rocketMQTemplate.convertAndSend("cart-item-add-topic", new CartItemEvent("bike", 1));
|
||||
rocketMQTemplate.convertAndSend("cart-item-add-topic", new CartItemEvent("computer", 2));
|
||||
rocketMQTemplate.convertAndSend("cart-item-removed-topic", new CartItemEvent("bike", 1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user