商城商店步骤学习完成
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package cn.mayiming.Consumer;
|
||||
|
||||
import cn.mayiming.Mapper.OrderMapper;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@RocketMQMessageListener(
|
||||
topic = "pay_success_topic",
|
||||
consumerGroup = "order-pay-success-consumer"
|
||||
)
|
||||
public class PaySuccessConsumer implements RocketMQListener<Map<String, Object>> {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void onMessage(Map<String, Object> msg) {
|
||||
try {
|
||||
String orderNo = msg.get("orderNo").toString();
|
||||
System.out.println("========== 订单服务接收支付成功消息 ==========");
|
||||
System.out.println("订单号:" + orderNo);
|
||||
System.out.println("支付金额:" + msg.get("payAmount"));
|
||||
System.out.println("===========================================");
|
||||
|
||||
// 核心:更新订单状态为「已支付」(假设1=已支付)
|
||||
int updateResult = orderMapper.updateOrderStatus(orderNo, 0, 1);
|
||||
if (updateResult == 0) {
|
||||
throw new RuntimeException("更新订单状态失败,订单号:" + orderNo);
|
||||
}
|
||||
System.out.println("订单" + orderNo + "已更新为「已支付」状态");
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("消费支付成功消息失败:" + e.getMessage());
|
||||
// 生产环境:抛出异常触发MQ重试,确保订单状态最终一致
|
||||
throw new RuntimeException("消费失败,触发重试", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,4 +78,8 @@ public interface OrderMapper {
|
||||
// 简单生成规则:时间戳 + 6位随机数
|
||||
return System.currentTimeMillis() + "" + (int)(Math.random() * 900000 + 100000);
|
||||
}
|
||||
|
||||
// OrderMapper补充更新订单状态方法
|
||||
@Update("UPDATE t_order SET order_status = #{newStatus}, update_time = CURRENT_TIMESTAMP WHERE order_no = #{orderNo} AND order_status = #{oldStatus}")
|
||||
int updateOrderStatus(@Param("orderNo") String orderNo, @Param("oldStatus") Integer oldStatus, @Param("newStatus") Integer newStatus);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user