SendMsg.java
/** * Created on 2018/3/28. * Description: * 统一命令入口 */ public class SendMsg { /** * @param args * * type=sendSMS: [sendSMS] [content] [phone number] * type=sendEspace: [sendEspace] [content] [job number] * type=sendEmail: [sendEmail] [subject] [content] [email] [Cc(Optional)] [Bcc(Optional)] */ public static void main(String[] args) { checkParameter(args); String result = dispatchCmd(args); if (!CommonUtil.checkCloudPagerResult(result)) { CommonUtil.exitWithErrorMsg("send message error, details:" + result); } System.exit(CommonConstants.RET_SUCCESS); } private static String dispatchCmd(String[] args) { String result = null; MsgType msgType = MsgType.valueOf(args[0]); try { if (MsgType.sendSMS == msgType) { result = SendMsgUtil.sendSMS(args[2], args[1]); } else if(MsgType.sendEspace == msgType) { result = SendMsgUtil.SendMulEspace(args[2], args[1]); } else if(MsgType.sendEmail == msgType){ String ccAddr = null; String bccAddr = null; if (args.length == 5) { ccAddr = args[4]; } if (args.length == 6) { ccAddr = args[4]; bccAddr = args[5]; } result = SendMsgUtil.sendEmail(args[3], ccAddr, bccAddr, args[1], args[2]); } } catch (Exception e) { CommonUtil.exitWithErrorMsg("send sms exception, details:" + e.getLocalizedMessage()); } return result; } private static void checkParameter(String[] args) { if (null == args || args.length < 3) { showHelp(); System.exit(CommonConstants.RET_FAIL); } String type = args[0]; if (null == MsgType.valueOf(type)) { CommonUtil.exitWithErrorMsg("unsupported message type[sendSMS/sendEspace/sendEmail]"); } if (MsgType.sendEmail == MsgType.valueOf(type)) { if (args.length < 4) { showHelp(); System.exit(CommonConstants.RET_FAIL); } } } private static void showHelp() { String lineSeparator = System.lineSeparator(); StringBuffer sb = new StringBuffer("Usage:"); sb.append(lineSeparator).append("script need at least three parameters, format: [type] [] [].. ").append(lineSeparator); sb.append("1 type=sendSMS: [sendSMS] [content] [phone number(separate by \",\")] ").append(lineSeparator); sb.append("2 type=sendEspace: [sendEspace] [content] [job number(separate by \",\")] ").append(lineSeparator); sb.append("3 type=sendEmail: [sendEmail] [subject] [content] [email(separate by \",\")] [Cc(Optional)] [Bcc(Optional)] ").append(lineSeparator); System.out.println(sb.toString()); } }