12 changed files with 373 additions and 317 deletions
@ -1,109 +1,106 @@ |
|||||
//package com.ccsens.game.mq;
|
package com.ccsens.game.mq; |
||||
//
|
|
||||
//import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil; |
||||
//import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil; |
||||
//import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON; |
||||
//import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject; |
||||
//import com.ccsens.game.bean.dto.ClientDto;
|
import com.ccsens.game.bean.dto.ClientDto; |
||||
//import com.ccsens.game.bean.dto.message.GameMessageCountOut;
|
import com.ccsens.game.bean.dto.message.GameMessageCountOut; |
||||
//import com.ccsens.game.bean.dto.message.GameMessageWithChangeStatusOut;
|
import com.ccsens.game.util.GameConstant; |
||||
//import com.ccsens.game.netty.ChannelManager;
|
import com.ccsens.util.RedisUtil; |
||||
//import com.ccsens.game.util.GameConstant;
|
import com.ccsens.util.bean.message.common.InMessage; |
||||
//import com.ccsens.util.JacksonUtil;
|
import com.ccsens.util.bean.message.common.OutMessage; |
||||
//import com.ccsens.util.RedisUtil;
|
import com.ccsens.util.bean.message.common.OutMessageSet; |
||||
//import com.ccsens.util.bean.message.common.InMessage;
|
import com.ccsens.util.config.RabbitMQConfig; |
||||
//import com.ccsens.util.bean.message.common.OutMessage;
|
import lombok.extern.slf4j.Slf4j; |
||||
//import com.ccsens.util.bean.message.common.OutMessageSet;
|
import org.springframework.amqp.core.AmqpTemplate; |
||||
//import com.ccsens.util.config.RabbitMQConfig;
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler; |
||||
//import lombok.extern.slf4j.Slf4j;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener; |
||||
//import org.springframework.amqp.core.AmqpTemplate;
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
import org.springframework.data.redis.core.ZSetOperations; |
||||
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.stereotype.Component; |
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
|
||||
//import org.springframework.data.redis.core.ZSetOperations;
|
import java.io.IOException; |
||||
//import org.springframework.stereotype.Component;
|
import java.util.ArrayList; |
||||
//
|
import java.util.HashSet; |
||||
//import java.io.IOException;
|
import java.util.List; |
||||
//import java.util.ArrayList;
|
import java.util.Set; |
||||
//import java.util.HashSet;
|
|
||||
//import java.util.List;
|
/** |
||||
//import java.util.Set;
|
* @description: |
||||
//
|
* @author: whj |
||||
///**
|
* @time: 2020/6/5 14:30 |
||||
// * @description:
|
*/ |
||||
// * @author: whj
|
@Slf4j |
||||
// * @time: 2020/6/5 14:30
|
@Component |
||||
// */
|
@RabbitListener(queues = RabbitMQConfig.GAME_SCORE) |
||||
//@Slf4j
|
public class GameScoreListener { |
||||
//@Component
|
|
||||
//@RabbitListener(queues = RabbitMQConfig.GAME_SCORE)
|
@Autowired |
||||
//public class GameScoreListener {
|
private RedisUtil redisUtil; |
||||
//
|
@Autowired |
||||
// @Autowired
|
private AmqpTemplate rabbitTemplate; |
||||
// private RedisUtil redisUtil;
|
|
||||
// @Autowired
|
@RabbitHandler |
||||
// private AmqpTemplate rabbitTemplate;
|
public void process(String messageJson) throws IOException { |
||||
//
|
log.info("计算分数 {}",messageJson); |
||||
// @RabbitHandler
|
OutMessageSet outMessageSet = JSONObject.parseObject(messageJson, OutMessageSet.class); |
||||
// public void process(String messageJson) throws IOException {
|
Set<OutMessage> messageSet = outMessageSet.getMessageSet(); |
||||
// log.info("计算分数 {}",messageJson);
|
if (CollectionUtil.isEmpty(messageSet)) { |
||||
// OutMessageSet outMessageSet = JSONObject.parseObject(messageJson, OutMessageSet.class);
|
return; |
||||
// Set<OutMessage> messageSet = outMessageSet.getMessageSet();
|
} |
||||
// if (CollectionUtil.isEmpty(messageSet)) {
|
messageSet.forEach(outMessage -> { |
||||
// return;
|
|
||||
// }
|
InMessage.To to = JSONObject.parseObject(outMessage.getFrom(), InMessage.To.class); |
||||
// messageSet.forEach(outMessage -> {
|
Long userId = to.getId(); |
||||
//
|
GameDto.GameMsg gameMsg = JSONObject.parseObject(outMessage.getData(), GameDto.GameMsg.class); |
||||
// InMessage.To to = JSONObject.parseObject(outMessage.getFrom(), InMessage.To.class);
|
Long recordId = gameMsg.getRecordId(); |
||||
// Long userId = to.getId();
|
log.info("统计分数:{},{}",userId, recordId); |
||||
// GameDto.GameMsg gameMsg = JSONObject.parseObject(outMessage.getAuthMessage(), GameDto.GameMsg.class);
|
GameMessageCountOut gameMessageCountOut = clientAddTimes(userId,recordId); |
||||
// Long recordId = gameMsg.getRecordId();
|
|
||||
// log.info("统计分数:{},{}",userId, recordId);
|
// 发送给消息系统
|
||||
// GameMessageCountOut gameMessageCountOut = clientAddTimes(userId,recordId);
|
InMessage inMessage = new InMessage(); |
||||
//
|
Set<String> tos = new HashSet<>(); |
||||
// // 发送给消息系统
|
tos.add(JSONObject.toJSONString(new InMessage.To(userId))); |
||||
// InMessage inMessage = new InMessage();
|
inMessage.setTos(tos); |
||||
// Set<String> tos = new HashSet<>();
|
inMessage.setData(JSONObject.toJSONString(gameMessageCountOut)); |
||||
// tos.add(JSONObject.toJSONString(new InMessage.To(userId)));
|
rabbitTemplate.convertAndSend(RabbitMQConfig.MESSAGE_QUEUE_NAME, inMessage); |
||||
// inMessage.setTos(tos);
|
}); |
||||
// inMessage.setData(JSONObject.toJSONString(gameMessageCountOut));
|
|
||||
// rabbitTemplate.convertAndSend(RabbitMQConfig.GAME_SCORE, inMessage);
|
} |
||||
// });
|
|
||||
//
|
public GameMessageCountOut clientAddTimes(Long userId, Long recordId) { |
||||
// }
|
GameMessageCountOut gameMessageCountOut = new GameMessageCountOut(); |
||||
//
|
log.info("userId:{}", userId); |
||||
// public GameMessageCountOut clientAddTimes(Long userId, Long recordId) {
|
if (userId == null || recordId == null) { |
||||
// GameMessageCountOut gameMessageCountOut = new GameMessageCountOut();
|
return gameMessageCountOut; |
||||
// log.info("userId:{}", userId);
|
} |
||||
// if (userId == null || recordId == null) {
|
|
||||
// return gameMessageCountOut;
|
String gameUserKey = GameConstant.generateGameKey(recordId); |
||||
// }
|
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1); |
||||
//
|
|
||||
// String gameUserKey = GameConstant.generateGameKey(recordId);
|
if (CollectionUtil.isEmpty(typedTuples)) { |
||||
// Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisUtil.zsRevGetWithScore(gameUserKey, 0, -1);
|
return gameMessageCountOut; |
||||
//
|
} |
||||
// if (CollectionUtil.isEmpty(typedTuples)) {
|
for(ZSetOperations.TypedTuple<Object> type : typedTuples) { |
||||
// return gameMessageCountOut;
|
ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class); |
||||
// }
|
if (user.getUserId().longValue() == userId.longValue()) { |
||||
// for(ZSetOperations.TypedTuple<Object> type : typedTuples) {
|
int score = type.getScore().intValue(); |
||||
// ClientDto.RedisUser user = JSON.parseObject((String)type.getValue(), ClientDto.RedisUser.class);
|
String userStatus = GameConstant.generateGameStatusKey(recordId); |
||||
// if (user.getUserId().longValue() == userId.longValue()) {
|
String gameStausObj = (String)redisUtil.get(userStatus); |
||||
// int score = type.getScore().intValue();
|
if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))){ |
||||
// String userStatus = GameConstant.generateGameStatusKey(recordId);
|
gameMessageCountOut = new GameMessageCountOut(score/100, score); |
||||
// String gameStausObj = (String)redisUtil.get(userStatus);
|
return gameMessageCountOut; |
||||
// if (StrUtil.isBlank(gameStausObj) || gameStausObj.equals(String.valueOf(GameConstant.GAME_COMPLETED))){
|
} |
||||
// gameMessageCountOut = new GameMessageCountOut(score/100, score);
|
score += 100; |
||||
// return gameMessageCountOut;
|
redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score); |
||||
// }
|
gameMessageCountOut.getData().setTotalScore(score); |
||||
// score += 100;
|
gameMessageCountOut.getData().setTotalTimes(score/100); |
||||
// redisUtil.zsSet(gameUserKey, JSON.toJSONString(user), score);
|
return gameMessageCountOut; |
||||
// gameMessageCountOut.getData().setTotalScore(score);
|
} |
||||
// gameMessageCountOut.getData().setTotalTimes(score/100);
|
}; |
||||
// return gameMessageCountOut;
|
|
||||
// }
|
return gameMessageCountOut; |
||||
// };
|
} |
||||
//
|
|
||||
// return gameMessageCountOut;
|
} |
||||
// }
|
|
||||
//
|
|
||||
//}
|
|
||||
|
|||||
@ -1,29 +1,29 @@ |
|||||
package com.ccsens.tall.rabbitMQ; |
//package com.ccsens.tall.rabbitMQ;
|
||||
|
//
|
||||
|
//
|
||||
import com.ccsens.util.config.RabbitMQConfig; |
//import com.ccsens.util.config.RabbitMQConfig;
|
||||
import org.slf4j.Logger; |
//import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory; |
//import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; |
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener; |
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component; |
//import org.springframework.stereotype.Component;
|
||||
|
//
|
||||
|
//
|
||||
@Component |
//@Component
|
||||
@RabbitListener(queues = RabbitMQConfig.TALL_MESSAGE_2) |
//@RabbitListener(queues = RabbitMQConfig.TALL_MESSAGE_2)
|
||||
public class RabbitController { |
//public class RabbitController {
|
||||
private Logger logger = LoggerFactory.getLogger(RabbitController.class); |
// private Logger logger = LoggerFactory.getLogger(RabbitController.class);
|
||||
|
//
|
||||
@RabbitHandler |
// @RabbitHandler
|
||||
public void process(String messageJson) { |
// public void process(String messageJson) {
|
||||
logger.info("Rabbit Received: {}",messageJson); |
// logger.info("Rabbit Received: {}",messageJson);
|
||||
try { |
// try {
|
||||
System.out.println("Rabbit Received: " + messageJson); |
// System.out.println("Rabbit Received: " + messageJson);
|
||||
|
//
|
||||
}catch (Exception e){ |
// }catch (Exception e){
|
||||
e.printStackTrace(); |
// e.printStackTrace();
|
||||
} |
// }
|
||||
|
//
|
||||
} |
// }
|
||||
|
//
|
||||
} |
//}
|
||||
|
|||||
@ -1,33 +1,33 @@ |
|||||
package com.ccsens.util.mq; |
//package com.ccsens.util.mq;
|
||||
|
//
|
||||
import com.ccsens.util.config.RabbitMQConfig; |
//import com.ccsens.util.config.RabbitMQConfig;
|
||||
import com.rabbitmq.client.Channel; |
//import com.rabbitmq.client.Channel;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; |
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener; |
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.support.AmqpHeaders; |
//import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Headers; |
//import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.Payload; |
//import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Component; |
//import org.springframework.stereotype.Component;
|
||||
|
//
|
||||
import java.io.IOException; |
//import java.io.IOException;
|
||||
import java.util.Date; |
//import java.util.Date;
|
||||
import java.util.Map; |
//import java.util.Map;
|
||||
|
//
|
||||
/** |
///**
|
||||
* @description: |
// * @description:
|
||||
* @author: wuHuiJuan |
// * @author: wuHuiJuan
|
||||
* @create: 2019/12/27 15:40 |
// * @create: 2019/12/27 15:40
|
||||
*/ |
// */
|
||||
@Component |
//@Component
|
||||
public class DelayConsumer { |
//public class DelayConsumer {
|
||||
@RabbitListener(queues = RabbitMQConfig.GAME_STATUS_DELAY_SEND_QUEUE) |
// @RabbitListener(queues = RabbitMQConfig.GAME_STATUS_DELAY_SEND_QUEUE)
|
||||
@RabbitHandler |
// @RabbitHandler
|
||||
public void receive(@Payload Object msg, @Headers Map<String, Object> headers, Channel channel ) throws IOException { |
// public void receive(@Payload Object msg, @Headers Map<String, Object> headers, Channel channel ) throws IOException {
|
||||
System.out.println("接收到的消息:"+msg +"||" + new Date()); |
// System.out.println("接收到的消息:"+msg +"||" + new Date());
|
||||
|
//
|
||||
//ACK 手工签收,通知rabbitMQ,消费端消费成功
|
// //ACK 手工签收,通知rabbitMQ,消费端消费成功
|
||||
Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG); |
// Long deliveryTag = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
|
||||
channel.basicAck(deliveryTag,false); |
// channel.basicAck(deliveryTag,false);
|
||||
} |
// }
|
||||
|
//
|
||||
} |
//}
|
||||
|
|||||
@ -1,54 +1,54 @@ |
|||||
package com.ccsens.util.mq; |
//package com.ccsens.util.mq;
|
||||
|
//
|
||||
import com.ccsens.util.config.RabbitMQConfig; |
//import com.ccsens.util.config.RabbitMQConfig;
|
||||
import lombok.extern.slf4j.Slf4j; |
//import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData; |
//import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component; |
//import org.springframework.stereotype.Component;
|
||||
|
//
|
||||
/** |
///**
|
||||
* @description: |
// * @description:
|
||||
* @author: wuHuiJuan |
// * @author: wuHuiJuan
|
||||
* @create: 2019/12/27 15:16 |
// * @create: 2019/12/27 15:16
|
||||
*/ |
// */
|
||||
@Slf4j |
//@Slf4j
|
||||
@Component |
//@Component
|
||||
public class DelayProducer { |
//public class DelayProducer {
|
||||
@Autowired |
// @Autowired
|
||||
private RabbitTemplate rabbitTemplate; |
// private RabbitTemplate rabbitTemplate;
|
||||
|
//
|
||||
//消息发送后的回调函数
|
// //消息发送后的回调函数
|
||||
/** |
// /**
|
||||
* 生产者回调函数:confirm确认消息投递成功 |
// * 生产者回调函数:confirm确认消息投递成功
|
||||
*/ |
// */
|
||||
final RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> { |
// final RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> {
|
||||
|
//
|
||||
String messageId = correlationData.getId(); |
// String messageId = correlationData.getId();
|
||||
if (ack) { |
// if (ack) {
|
||||
System.out.println("消息发送成功" + correlationData); |
// System.out.println("消息发送成功" + correlationData);
|
||||
log.info("消息投递成功,{}",messageId); |
// log.info("消息投递成功,{}",messageId);
|
||||
//进行消息记录的数据库更新
|
// //进行消息记录的数据库更新
|
||||
|
//
|
||||
}else{ |
// }else{
|
||||
log.info("消息投递失败"); |
// log.info("消息投递失败");
|
||||
} |
// }
|
||||
|
//
|
||||
}; |
// };
|
||||
|
//
|
||||
/** |
// /**
|
||||
* 通过延迟消息插件发动延迟消息 |
// * 通过延迟消息插件发动延迟消息
|
||||
* @param msg |
// * @param msg
|
||||
* @param expiration |
// * @param expiration
|
||||
*/ |
// */
|
||||
public void sendDelayMessage(Object msg, Long expiration){ |
// public void sendDelayMessage(Object msg, Long expiration){
|
||||
|
//
|
||||
//绑定异步监听回调函数
|
// //绑定异步监听回调函数
|
||||
rabbitTemplate.setConfirmCallback(confirmCallback); |
// rabbitTemplate.setConfirmCallback(confirmCallback);
|
||||
|
//
|
||||
rabbitTemplate.convertAndSend(RabbitMQConfig.GAME_STATUS_DELAY_SEND_EXCHANGE,RabbitMQConfig.GAME_STATUS_DELAY_SEND_ROUTING_KEY, msg,(message)->{ |
// rabbitTemplate.convertAndSend(RabbitMQConfig.GAME_STATUS_DELAY_SEND_EXCHANGE,RabbitMQConfig.GAME_STATUS_DELAY_SEND_ROUTING_KEY, msg,(message)->{
|
||||
message.getMessageProperties().setHeader("x-delay", expiration);//设置延迟时间
|
// message.getMessageProperties().setHeader("x-delay", expiration);//设置延迟时间
|
||||
return message; |
// return message;
|
||||
}, new CorrelationData("123")); |
// }, new CorrelationData("123"));
|
||||
} |
// }
|
||||
} |
//}
|
||||
|
|||||
Loading…
Reference in new issue