半桶水开发 CMPP2.0 with Spring + JPA 2

阅读更多
  • 半桶水开发 CMPP2.0 with Spring + JPA  1
  • 半桶水开发 CMPP2.0 with Spring + JPA  2
  • 半桶水开发 CMPP2.0 with Spring + JPA  3


接上,这贴一下自定义的发送代理

CMPPProxy.java
public class CMPPProxy extends SMProxy {
   //保存下发短信的队列
    private static BlockingQueue deliveMsgs = new LinkedBlockingQueue(15);
/*
*  返回一个实例
* @return CMPPProx 返回一个实例
*/
    public static CMPPProxy getInstance() {
        if (instance == null) {
            instance = new CMPPProxy();
        }
        return instance;
    }
/*
* 获取保存下发短信的队列
* @return BlockingQueue 
*/
    public BlockingQueue getDeliveMsgs() {
        return deliveMsgs;
    }

    protected CMPPProxy() {
        super(arg);
    }

    public void OnTerminate() {
        System.out.println("Connection have been breaked! ");
    }
/*
*  收到下发短信触发的事件 如果需要返回回执的话就返回一个 CMPPMessage到网关中
*  @return CMPPMessage
*/
    @Override
    public CMPPMessage onDeliver(CMPPDeliverMessage msg) {
        byte msgId[] = msg.getMsgId();
        if (msg.getRegisteredDeliver() == 1) { //是否要回执
            if (String.valueOf(msg.getStat()).equalsIgnoreCase("DELIVRD")) {
                System.out.println(String.valueOf(String.valueOf((new StringBuffer("\t\treceived DELIVRD message msgid=[")).append(msg.getMsgId()).append("]"))));
//                long submitMsgId = TypeConvert.byte2long(msg.getStatusMsgId());
                try {
                    CMPPDeliverRepMessage cmppdeliverrepmessage = new CMPPDeliverRepMessage(msgId, 0);
                    return cmppdeliverrepmessage;
                } catch (Exception ex) {
                    CMPPDeliverRepMessage cmppdeliverrepmessage1 = new CMPPDeliverRepMessage(msgId, 9);
                    return cmppdeliverrepmessage1;
                }
            } else {
                return new CMPPDeliverRepMessage(msgId, 9);
            }
        } else {
            System.out.println("Get a Deliver..");
            try {
                deliveMsgs.put(msg);  //把收到的非回执短信保存到队列中待处理
            } catch (InterruptedException ex) {
                Logger.getLogger(CMPPProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Total [" + deliveMsgs.size() + "]DeliverMessages");
            System.out.println(String.valueOf(String.valueOf((new StringBuffer("\t\treceived non DELIVRD message msgid=[")).append(msg.getMsgId()).append("]"))));
            return new CMPPDeliverRepMessage(msgId, 9);
        }
    }
/*
* 封装过的发送函数
* @param CMPPSubmitMessage 短信
* @return boolean 是否发送成功
*/
    public boolean send(CMPPSubmitMessage msg) {
        if (msg == null) {
            return false;
        }
        CMPPSubmitRepMessage reportMsg = null;
        try {
            reportMsg = (CMPPSubmitRepMessage) super.send(msg);

            if (reportMsg == null) {
                return false;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
/*
*发送短信,带响应内容
* @param CMPPSubmitMessage 
* @return CMPPSubmiReptMessage 响应内容
*/
    public CMPPSubmitRepMessage sendWithRep(CMPPSubmitMessage msg) {
        CMPPSubmitRepMessage reportMsg = null;
        try {
            reportMsg = (CMPPSubmitRepMessage) super.send(msg);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return reportMsg;
    }
    private static Args arg = Env.getConfig().getArgs("CMPPConnect");
    public static final String service_Id = Env.getConfig().get("CMPPSubmitMessage/service_Id", "WebSM");
    public static final String msg_Src = Env.getConfig().get("CMPPSubmitMessage/msg_Src", "WebSMS");
    public static final String connectCode = Env.getConfig().get("CMPPSubmitMessage/src_Terminal_Id", "");
    private static CMPPProxy instance;
}

你可能感兴趣的:(JPA,Spring,Blog)