Skip to content

Commit

Permalink
mq starter demo optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
javahongxi committed Nov 27, 2018
1 parent 659df3d commit 0f7f303
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 61 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@

<jedis.version>2.9.0</jedis.version>

<lombok.version>1.18.4</lombok.version>

<activemq.version>5.15.0</activemq.version>
<rabbitmq.version>4.8.0</rabbitmq.version>
<kafka.version>0.8.0</kafka.version>
Expand Down Expand Up @@ -427,6 +429,14 @@
<version>${jedis.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>


<!-- test -->
<dependency>
<groupId>junit</groupId>
Expand Down
7 changes: 7 additions & 0 deletions whatsmars-mq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
package org.hongxi.whatsmars.mq.rocketmq.boot;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class OrderPaidEvent implements Serializable {
private String orderId;

private BigDecimal paidMoney;

public OrderPaidEvent() {}

public OrderPaidEvent(String orderId, BigDecimal paidMoney) {
this.orderId = orderId;
this.paidMoney = paidMoney;
}

public String getOrderId() {
return orderId;
}

public void setOrderId(String orderId) {
this.orderId = orderId;
}

public BigDecimal getPaidMoney() {
return paidMoney;
}

public void setPaidMoney(BigDecimal paidMoney) {
this.paidMoney = paidMoney;
}
}
private String orderId;
private BigDecimal paidMoney;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.hongxi.whatsmars.mq.rocketmq.boot.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication{

public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.hongxi.whatsmars.mq.rocketmq.boot.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.starter.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.starter.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer implements RocketMQListener<String> {
public void onMessage(String message) {
log.info("received message: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.hongxi.whatsmars.mq.rocketmq.boot.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.starter.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.starter.core.RocketMQListener;
import org.hongxi.whatsmars.mq.rocketmq.boot.OrderPaidEvent;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-2", consumerGroup = "my-consumer_test-topic-2")
public class MyConsumer2 implements RocketMQListener<OrderPaidEvent> {
public void onMessage(OrderPaidEvent orderPaidEvent) {
log.info("received orderPaidEvent: " + orderPaidEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hongxi.whatsmars.mq.rocketmq.boot;
package org.hongxi.whatsmars.mq.rocketmq.boot.producer;

import org.apache.rocketmq.spring.starter.core.RocketMQTemplate;
import org.hongxi.whatsmars.mq.rocketmq.boot.OrderPaidEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
Expand All @@ -19,9 +20,15 @@ public static void main(String[] args){
}

public void run(String... args) throws Exception {
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
rocketMQTemplate.convertAndSend("test-topic-2", new OrderPaidEvent("T_001", new BigDecimal("88.00")));
for (int i = 0; i < 20; i++) {
try {
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
rocketMQTemplate.convertAndSend("test-topic-2", new OrderPaidEvent("T_001", new BigDecimal("88.00")));
} catch (Exception e) {
e.printStackTrace();
}
}

// rocketMQTemplate.destroy(); // notes: once rocketMQTemplate be destroyed, you can not send any message again with this rocketMQTemplate
}
Expand Down

0 comments on commit 0f7f303

Please sign in to comment.