如何在java中创建消息查询处理方法?

pgpifvop  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(170)

我正在处理一个消息查询处理程序,因此,我需要为每个案例/状态声明一些方法:enroute(如果尚未发送则执行重试)、delivered(从队列/列表中删除以使其停止发送)、expired和undelivered,具体取决于200ok响应。我已经有了过期和不可交付的方法(这会将信用返回给用户),但是我找不到合适的方法来处理“enroute”和“delivered”,因为对于“enroute”,我需要有参数来每y秒重试x次(在一个单独的类上(可能在application.properties中),对于delivered,则将其从队列中删除。我还想知道是否可以在smsreader类上管理它,但我不确定(我将在最后留下示例代码)。你知道怎么做吗?这是我正在编写的代码。

QUERY HANDLER CLASS

@Component
public class MsgQueryApiHandler {

    private static final Logger logger = LoggerFactory.getLogger(MsgQueryApiHandler.class);

    RestTemplate restTemplate = new RestTemplate();

    @Value("${smscMsgQuery.host}")
    private String smscMsgQueryHost;

    @Value("${smscMsgQuery.user}")
    private String smscMsgQueryUser;

    @Value("${smscMsgQuery.pass}")
    private String smscMsgQueryPass;

    @Autowired
    public ConfirmHttp confirmHttp;

 public void msgQueryHandle(SmsRequest smsRequest){
        String response = getMessageStatus(Integer.parseInt(smsRequest.getMessageId()));
        switch (response){
            //Perform Retries
            case"ENROUTE":
                break;

            //remove of the queue
            case"DELIVERED":

                break;

            case"EXPIRED":
            case"UNDELIVERABLE":
                refundMsg(smsRequest);
                break;
        }
    }

private void refundMsg (SmsRequest smsRequest){
        ChargeHttpRequest reqBody = new ChargeHttpRequest();
        reqBody.setOrigSystemId(smsRequest.getOrigSystemId());
        reqBody.setStatus("failed");
        Integer statusCode = confirmHttp.sendCDR(reqBody);
        logger.info("Status code from api {}", statusCode);
    }

    public String getMessageStatus(int messageId) {
        String status = "";
        try {
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(smscMsgQueryHost)
                    .queryParam("userid", smscMsgQueryUser)
                    .queryParam("password", smscMsgQueryPass)
                    .queryParam("msgid", messageId)
                    ;
            ResponseEntity<String> response = restTemplate.getForEntity(builder.toUriString(),String.class);

            if(response.getStatusCodeValue()== 200){
                String[] responseBody = response.getBody().split("\n");
                if(responseBody.length>1){
                    status = responseBody[1];
                    logger.info(status);
                }
            }
        } catch (Exception ex) {
            status = "";
            logger.error("An error ocurred on HTTP request", ex);
        }
        return status;
    }

SMSREADER CLASS 

@RestController
@RequestMapping("/smsReader")
public class SmsReaderController {

    @Autowired
    private SmsReader smsReader;

    @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE)
    public HashMap<String, Object> processSms(@RequestBody SmsRequest smsRequest) {
        HashMap<String, Object> response = new HashMap<>();
        try {
            smsReader.saveSms(smsRequest);
            response.put("response", "success");
        } catch (Exception ex) {
            response.put("response", "failed");
        }

        return response;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题