ÎļþÃû´Ó ruoyi-admin/src/main/java/com/ruoyi/web/component/RabbitMqReceiver.java ÐÞ¸Ä |
| | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.rabbitmq.client.Channel; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.enums.ServiceFromEnum; |
| | | import com.ruoyi.common.utils.RSAPublicKeyExample; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.web.task.PhoneTask; |
| | | import com.smartor.common.SendService; |
| | | import com.smartor.config.PhoneUtils; |
| | | import com.smartor.config.RabbitMqCallPhoneConfig; |
| | | import com.smartor.config.RobotPhoneUtils; |
| | | import com.smartor.domain.*; |
| | | import com.smartor.mapper.IvrTaskMapper; |
| | | import com.smartor.mapper.IvrTaskSingleMapper; |
| | | import com.smartor.service.IIvrTaskTemplateService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.amqp.core.Message; |
| | | import org.springframework.amqp.rabbit.annotation.RabbitListener; |
| | | import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; |
| | | import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| | | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.data.redis.connection.Message; |
| | | import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; |
| | | import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | |
| | | @Slf4j |
| | | @Component//ç嬿¤éå |
| | | public class RabbitMqReceiver { |
| | | public class RedisMqReceiver extends KeyExpirationEventMessageListener { |
| | | |
| | | @Value("${phonePath}") |
| | | private String phonePath; |
| | |
| | | |
| | | private static RedisCache redisCache; |
| | | |
| | | private static RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig; |
| | | |
| | | private static RobotPhoneUtils robotPhoneUtils; |
| | | |
| | | |
| | | // å建åºå®å¤§å°ççº¿ç¨æ± |
| | | private static final ExecutorService executorService = Executors.newFixedThreadPool(10); |
| | | |
| | | public RedisMqReceiver(RedisMessageListenerContainer listenerContainer) { |
| | | super(listenerContainer); |
| | | } |
| | | |
| | | |
| | | @Autowired |
| | | public void setIIvrTaskTemplateService(IIvrTaskTemplateService ivrTaskTemplateService) { |
| | | RabbitMqReceiver.ivrTaskTemplateService = ivrTaskTemplateService; |
| | | RedisMqReceiver.ivrTaskTemplateService = ivrTaskTemplateService; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setRobotPhoneUtilsService(RobotPhoneUtils robotPhoneUtils) { |
| | | RabbitMqReceiver.robotPhoneUtils = robotPhoneUtils; |
| | | RedisMqReceiver.robotPhoneUtils = robotPhoneUtils; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setSendService(SendService sendService) { |
| | | RabbitMqReceiver.sendService = sendService; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setRabbitMqCallPhoneConfig(RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig) { |
| | | RabbitMqReceiver.rabbitMqCallPhoneConfig = rabbitMqCallPhoneConfig; |
| | | RedisMqReceiver.sendService = sendService; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setIvrTaskcallMapper(IvrTaskSingleMapper ivrTaskcallMapper) { |
| | | RabbitMqReceiver.ivrTaskcallMapper = ivrTaskcallMapper; |
| | | RedisMqReceiver.ivrTaskcallMapper = ivrTaskcallMapper; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setIvrTaskMapper(IvrTaskMapper ivrTaskMapper) { |
| | | RabbitMqReceiver.ivrTaskMapper = ivrTaskMapper; |
| | | RedisMqReceiver.ivrTaskMapper = ivrTaskMapper; |
| | | } |
| | | |
| | | @Autowired |
| | | public void setRedisCache(RedisCache redisCache) { |
| | | RabbitMqReceiver.redisCache = redisCache; |
| | | RedisMqReceiver.redisCache = redisCache; |
| | | } |
| | | |
| | | /** |
| | | * ä»»å¡é访 |
| | | * concurrency = "50" å¹¶åæ°ä¸º50 |
| | | */ |
| | | @RabbitListener(queues = "task_queue", concurrency = "50") |
| | | public void phoneVisit(String content, Message message, Channel channel) throws IOException { |
| | | System.out.println("æ¶æ¯è¿æ¥äº----------------"); |
| | | @Override |
| | | public void onMessage(Message message, byte[] pattern) { |
| | | log.info("çå¬Redis keyè¿æï¼keyï¼{}ï¼channelï¼{}", message.toString(), new String(pattern)); |
| | | String content = message.toString(); |
| | | IvrTaskcallMQ ivrTaskcallMQ = null; |
| | | try { |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | |
| | | IvrTask ivrTask1 = ivrTaskMapper.selectIvrTaskByTaskid(ivrTaskcallMQ.getTaskid()); |
| | | if (ivrTask1.getStopState() != ivrTaskcallMQ.getStopState()) { |
| | | //å°æ¶æ¯ä»éåä¸åé¤ |
| | | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | return; |
| | | } |
| | | |
| | | if (StringUtils.isNotEmpty(ivrTaskcallMQ.getPreachform())) { |
| | | //å¦ä½ä»»å¡åéæ¹å¼ä¸ä¸ºç©º |
| | | String[] split = ivrTaskcallMQ.getPreachform().split(","); |
| | | System.out.println("splitçå¼ä¸ºï¼" + split); |
| | | for (String serviceFrom : split) { |
| | | String descByCode = ServiceFromEnum.getDescByCode(Integer.valueOf(serviceFrom)); |
| | | //è¿éå¯ä»¥èèç¨çç¥æ¨¡å¼ä¼åä¸ä¸ï¼ä¸ç¶å¤ªé¾çäº |
| | |
| | | break; |
| | | } |
| | | //éè¿å¤çº¿ç¨çæ¹å¼å»æçµè¯ |
| | | executorService.submit(new PhoneTask(ivrTaskcall1, ivrTaskTemplateVO1, redisCache, rabbitMqCallPhoneConfig, message, sendService, phonePath, robotPhoneUtils)); |
| | | executorService.submit(new PhoneTask(ivrTaskcall1, ivrTaskTemplateVO1, redisCache, sendService, phonePath, robotPhoneUtils)); |
| | | } |
| | | } else if (descByCode.equals("å¤åªä½")) { |
| | | //å¤åªä½ |
| | |
| | | } |
| | | } |
| | | //éç¥ MQ æ¶æ¯å·²è¢«æ¥æ¶,å¯ä»¥ACK(ä»éåä¸å é¤)äº ï¼è¿ä¸ªéè¦æ ¹æ®ä¸å¡åå»å¤çACKï¼ |
| | | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | Integer integer = redisCache.getCacheObject(ivrTaskcallMQ.getTaskid().toString()); |
| | | if (integer != null && integer == 2) { |
| | | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | //å°æ¶æ¯ä»éåä¸å é¤ |
| | | } else if (integer == null) { |
| | | redisCache.setCacheObject(ivrTaskcallMQ.getTaskid().toString(), 1, 120, TimeUnit.MINUTES); |
| | | } else { |
| | |
| | | |
| | | log.error("============æ¶è´¹å¤±è´¥,å°è¯æ¶æ¯è¡¥å忬¡æ¶è´¹!=============="); |
| | | log.error(e.getMessage()); |
| | | channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); |
| | | redisCache.setCacheObject(message.toString(), message.toString(), 60, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æºå¨äººç¬¬ä¸å¥è¯è¯é³ |
| | | * concurrency = "50" å¹¶åæ°ä¸º50 |
| | | */ |
| | | @RabbitListener(queues = "ob_queue", concurrency = "50") |
| | | public void obVisit(String content, Message message, Channel channel) throws IOException { |
| | | try { |
| | | IvrTaskcallPhoneMQ ivrTaskcallMQ = null; |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | ivrTaskcallMQ = mapper.readValue(content, IvrTaskcallPhoneMQ.class); |
| | | //夿æ£è
æ¯å¦å·²ç»æ¥çµè¯ |
| | | String str = redisCache.getCacheObject(ivrTaskcallMQ.getUuid() + "state_id"); |
| | | System.out.println("-----------------" + str); |
| | | if (StringUtils.isNotEmpty(str) && str.equals("0")) { |
| | | //æ£è
å·²ç»æ¥å¬äºçµè¯ |
| | | new PhoneUtils().ttsPlayback(ivrTaskcallMQ.getScript(), ivrTaskcallMQ.getUuid()); |
| | | } else if (StringUtils.isNotEmpty(str) && str.equals("-10")) { |
| | | //è¿ä¸ªè¯´æ,å·²ç»æäºæå®éæ°è¿æ¯æ²¡æäººæ¥ï¼å¯ä»¥å°æ¶æ¯ä»éåä¸ç§»é¤äº |
| | | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | return; |
| | | } else { |
| | | //è¿è¡éåå°¾é¨ï¼è¿è¡ä¸ä¸æ¬¡æ ¡éª |
| | | // channel.basicRecover(false); |
| | | channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); |
| | | System.out.println("++++++++++++++++++++++" + str); |
| | | return; |
| | | } |
| | | //éç¥ MQ æ¶æ¯å·²è¢«æ¥æ¶,å¯ä»¥ACK(ä»éåä¸å é¤)äº ï¼è¿ä¸ªéè¦æ ¹æ®ä¸å¡åå»å¤çACKï¼ |
| | | channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | } catch (Exception e) { |
| | | log.error("============æ¶è´¹å¤±è´¥,å°è¯æ¶æ¯è¡¥å忬¡æ¶è´¹!=============="); |
| | | log.error(e.getMessage()); |
| | | channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); |
| | | } |
| | | } |
| | | // /** |
| | | // * æºå¨äººç¬¬ä¸å¥è¯è¯é³ |
| | | // * concurrency = "50" å¹¶åæ°ä¸º50 |
| | | // */ |
| | | // @RabbitListener(queues = "ob_queue", concurrency = "50") |
| | | // public void obVisit(String content, Message message, Channel channel) throws IOException { |
| | | // try { |
| | | // IvrTaskcallPhoneMQ ivrTaskcallMQ = null; |
| | | // ObjectMapper mapper = new ObjectMapper(); |
| | | // ivrTaskcallMQ = mapper.readValue(content, IvrTaskcallPhoneMQ.class); |
| | | // //夿æ£è
æ¯å¦å·²ç»æ¥çµè¯ |
| | | // String str = redisCache.getCacheObject(ivrTaskcallMQ.getUuid() + "state_id"); |
| | | // System.out.println("-----------------" + str); |
| | | // if (StringUtils.isNotEmpty(str) && str.equals("0")) { |
| | | // //æ£è
å·²ç»æ¥å¬äºçµè¯ |
| | | // new PhoneUtils().ttsPlayback(ivrTaskcallMQ.getScript(), ivrTaskcallMQ.getUuid()); |
| | | // } else if (StringUtils.isNotEmpty(str) && str.equals("-10")) { |
| | | // //è¿ä¸ªè¯´æ,å·²ç»æäºæå®éæ°è¿æ¯æ²¡æäººæ¥ï¼å¯ä»¥å°æ¶æ¯ä»éåä¸ç§»é¤äº |
| | | // channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | // return; |
| | | // } else { |
| | | // //è¿è¡éåå°¾é¨ï¼è¿è¡ä¸ä¸æ¬¡æ ¡éª |
| | | //// channel.basicRecover(false); |
| | | // channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); |
| | | // System.out.println("++++++++++++++++++++++" + str); |
| | | // return; |
| | | // } |
| | | // //éç¥ MQ æ¶æ¯å·²è¢«æ¥æ¶,å¯ä»¥ACK(ä»éåä¸å é¤)äº ï¼è¿ä¸ªéè¦æ ¹æ®ä¸å¡åå»å¤çACKï¼ |
| | | // channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); |
| | | // } catch (Exception e) { |
| | | // log.error("============æ¶è´¹å¤±è´¥,å°è¯æ¶æ¯è¡¥å忬¡æ¶è´¹!=============="); |
| | | // log.error(e.getMessage()); |
| | | // channel.basicReject(message.getMessageProperties().getDeliveryTag(), true); |
| | | // } |
| | | // } |
| | | |
| | | |
| | | @Bean |
| | | public SimpleRabbitListenerContainerFactory jsonContainerFactory(ConnectionFactory connectionFactory) { |
| | | SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); |
| | | factory.setConnectionFactory(connectionFactory); |
| | | factory.setMessageConverter(new Jackson2JsonMessageConverter()); |
| | | return factory; |
| | | } |
| | | } |