|
@@ -0,0 +1,67 @@
|
|
|
|
+package com.fdkankan.openApi.mq.listener;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.fdkankan.openApi.mq.dto.CallApiDTO;
|
|
|
|
+import com.fdkankan.openApi.service.system.IUserAuthService;
|
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.amqp.core.Message;
|
|
|
|
+import org.springframework.amqp.rabbit.annotation.Queue;
|
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.dao.DeadlockLoserDataAccessException;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
|
+
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 消息监听器
|
|
|
|
+ *
|
|
|
|
+ * @author XiongNeng
|
|
|
|
+ * @version 1.0
|
|
|
|
+ * @since 2018/3/1
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class CallApiListener {
|
|
|
|
+ @Autowired
|
|
|
|
+ IUserAuthService userAuthService;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * saveOrEdit
|
|
|
|
+ *
|
|
|
|
+ * @param channel
|
|
|
|
+ * @param message
|
|
|
|
+ * @throws Exception the io exception 这里异常需要处理
|
|
|
|
+ */
|
|
|
|
+ @RabbitListener(
|
|
|
|
+ queuesToDeclare = @Queue("${queue.openApi.call-api}"),
|
|
|
|
+ concurrency = "${maxThread.openApi.update-count}"
|
|
|
|
+
|
|
|
|
+ )
|
|
|
|
+ public void callApi(Channel channel, Message message) throws Exception {
|
|
|
|
+ if (ObjectUtils.isEmpty(message.getBody())) {
|
|
|
|
+ log.error("消息内容为空,退出构建,当前服务器id:{}");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
|
|
|
+ String msg = new String(message.getBody(), StandardCharsets.UTF_8);
|
|
|
|
+ String messageId = message.getMessageProperties().getMessageId();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ log.info("callApi开始,id:{},deliveryTag:{},消息体:{}", messageId, deliveryTag, msg);
|
|
|
|
+ CallApiDTO param = JSONObject.parseObject(msg, CallApiDTO.class);
|
|
|
|
+
|
|
|
|
+ int i = userAuthService.updateCallCounts(param.getAppKey());
|
|
|
|
+ log.info("修改数据量为{}", i);
|
|
|
|
+ channel.basicAck(deliveryTag, false);
|
|
|
|
+
|
|
|
|
+ } catch (DeadlockLoserDataAccessException e) {
|
|
|
|
+ log.info("修改死锁重新入队");
|
|
|
|
+ channel.basicReject(deliveryTag, true);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|