⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 synctransceiverexample30.java

📁 短信发送的程序。JAVA编写
💻 JAVA
字号:
package com.bci.cmpp.example;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.bci.cmpp.AlreadyBoundException;
import com.bci.cmpp.BadCommandIDException;
import com.bci.cmpp.CMPPConstants;
import com.bci.cmpp.Connection;
import com.bci.cmpp.InvalidOperationException;
import com.bci.cmpp.UnsupportedOperationException;
import com.bci.cmpp.message.*;
import com.bci.cmpp.util.*;

public class SyncTransceiverExample30 extends CMPPAPIExample {

	private Log logger = LogFactory.getLog(SyncTransceiverExample30.class);

	public SyncTransceiverExample30() {
	}

	protected boolean reConnect(int type) {
		try {
			Thread.sleep(1000);
			myConnection = new Connection(this.hostName, this.port);
			myConnection.autoAckLink(true);
			myConnection.autoAckMessages(true);
			myConnection.setVersion(CMPPConstants.CMPP_VERSION_3);
			logger.info("Binding to the SMSG..");
			CMPPConnectResp resp = myConnection.bind(type, this.icpId,
					this.password);
			if (resp != null) {
				logger.info("The SMSG version is " + resp.getVersion());
				if (resp.getCommandStatus() != 0)
					logger.info("SMSG bind failed.");
				else {
					new Thread(new Transmitter(myConnection)).start();
					new Thread(new Receiver(myConnection)).start();
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		} catch (InvalidParameterValueException e) {
			e.printStackTrace();
			return false;
		} catch (AlreadyBoundException e) {
			e.printStackTrace();
			return false;
		} catch (CMPPProtocolException e) {
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		logger.info("Bind successful.");
		return true;
	}

	public void execute() {
		// 发送短信 使用同步模式
		while (true) {
			while (!isConnected())
				reConnect(Connection.TRANSCEIVER); // 每30秒发一次ActiveTest
			try {
				Thread.sleep(1000 * 30);
				CMPPActiveTest at = (CMPPActiveTest) myConnection
						.newInstance(CMPPConstants.CMPP_ACTIVE_TEST);
				CMPPActiveTestResp atr = (CMPPActiveTestResp) myConnection
						.sendRequest(at);
				if (atr != null && atr.getCommandStatus() == 0)
					logger.info("active test seq:" + atr.getSequenceNum());
			} catch (BadCommandIDException e) {
				e.printStackTrace();
				disConnect();
			} catch (SocketTimeoutException e) {
				e.printStackTrace();
				disConnect();
			} catch (AlreadyBoundException e) {
				e.printStackTrace();
				disConnect();
			} catch (CMPPProtocolException e) {
				e.printStackTrace();
				disConnect();
			} catch (UnsupportedOperationException e) {
				e.printStackTrace();
				disConnect();
			} catch (IOException e) {
				e.printStackTrace();
				disConnect();
			} catch (InterruptedException e) {
				e.printStackTrace();
				disConnect();
			}
		}
	}

	class Transmitter implements Runnable {

		private Connection conn = null;

		public Transmitter(Connection conn) {
			this.conn = conn;
		}

		public void run() {
			boolean stop = false;
			while (!stop) {
				// try {
				// Thread.sleep(1000);
				// } catch (InterruptedException e) {
				// e.printStackTrace();
				// }
				// Submit a simple message
				CMPPSubmit30 sm = null;
				try {
					sm = (CMPPSubmit30) conn
							.newInstance(CMPPConstants.CMPP_SUBMIT);
				} catch (BadCommandIDException e1) {
					e1.printStackTrace();
				}
				// sm.setSequenceNum(0);
				sm.setSpCode(getProperty("spCode"));
				sm.setServiceId(getProperty("serviceId"));
				sm.setSourceAddr(getProperty("sourceAddr"));
				sm.setFeeTerminalId(getProperty("feeTerm"));
				sm.setDestTerminalId(getProperty("destTerm"));
				sm.setFeeUserType(3);
				sm.setRegistered(1);
				sm.setFeeType(getProperty("feeType"));
				sm.setFeeCode(getProperty("feeCode"));
				sm.setLinkId(getProperty("linkId"));
				// sm.setAtTime(new Date(System.currentTimeMillis() + 1000));
				// sm.setAtTime((Date)null);
				try {
					sm.setEncoding(UCS2Encoding.getInstance());
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				sm.setMsgCoding(8);
				sm.setMsgText("短信发送测试:"
						+ (sm.getAtTime() == null ? "null" : sm.getAtTime()
								.toString()));
				// sm.setMessage("注意:关于短信群发的问题,若SP对于群发消息不要求状态报告的回送时".getBytes());
				// logger.info(encodeHex(sm.getBytes()));
				logger.info("submit message:" + sm.getFeeType() + "|"
						+ sm.getFeeCode() + "|" + sm.getDestTerminalId()[0]
						+ "|" + sm.getMsgText());
				try {
					CMPPSubmitResp30 smr = (CMPPSubmitResp30) conn
							.sendRequest(sm);
					if (smr == null)
						logger.info("Submitted message returned null.");
					else if (smr.getCommandStatus() == 0) {
						logger.info("Submitted message ID: "
								+ smr.getMsgIdStr());
						stop = true;
					} else
						logger.info("Submit message returned error:"
								+ smr.getCommandStatus());
				} catch (SocketTimeoutException e1) {
					e1.printStackTrace();
					stop = true;
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
					stop = true;
				} catch (CMPPProtocolException e1) {
					e1.printStackTrace();
					stop = true;
				} catch (UnsupportedOperationException e1) {
					e1.printStackTrace();
					stop = true;
				} catch (IOException e1) {
					e1.printStackTrace();
					stop = true;

				}
			}
			logger.info("Transmitter thread stoped.");
		}

	}

	class Receiver implements Runnable {

		private Connection conn = null;

		public Receiver(Connection conn) {
			this.conn = conn;
		}

		public void run() {
			boolean stop = false;
			while (!stop) {
				try {
					// Wait a while, see if the SMSC delivers anything to us...
					if (conn.packetAvailable() != 2) {
						try {
							Thread.sleep(1);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						continue;
					}
					CMPPPacket p = conn.readNextPacket();
					if (p == null)
						continue;
					int id = p.getCommandId();
					switch (id) {
					case CMPPConstants.CMPP_DELIVER:
						CMPPDeliver30 dm = (CMPPDeliver30) p;
						if (conn.isAckingMessages()) {
							conn.ackDeliverSm(dm);
						}
						deliverSM(dm);
						break;
					case CMPPConstants.CMPP_ACTIVE_TEST:
						if (conn.isAckingLinks())
							conn.ackEnquireLink((CMPPActiveTest) p);
						break;
					default:
						logger.info("Received a packet ,the id is:" + id);
						break;
					}

					// API should be automatically acking deliver_sm and
					// enquire_link packets...
					// ah well...
				} catch (SocketTimeoutException x) {
					x.printStackTrace();
					stop = true;
				} catch (CMPPProtocolException e) {
					e.printStackTrace();
					stop = true;
				} catch (InvalidOperationException e) {
					e.printStackTrace();
					stop = true;
				} catch (IOException e) {
					e.printStackTrace();
					stop = true;
				}
			}
			logger.info("Receive thread stoped.");
		}
	}

	private void deliverSM(CMPPDeliver30 dm) {
		if (dm.getRegisteredDelivery() == 1)
			logger.info("Received a stat report:" + dm.getMsgIdStr() + "|"
					+ dm.getSrcTerminalId().trim() + "|"
					+ dm.getDestTerminalId().trim() + "|" + dm.getStat().trim()
					+ "|" + dm.getStatusMsgIdStr());
		else
			logger.info("Received a deliver message:" + dm.getMsgIdStr() + "|"
					+ dm.getSrcTerminalId().trim() + "|"
					+ dm.getDestId().trim() + "|" + dm.getMsgText());
	}

	public static void main(String[] arg) {
		PropertyConfigurator.configure("config/log4j.properties");
		SyncTransceiverExample30 st = new SyncTransceiverExample30();
		try {
			st.configure("config/hwMtConf30.properties");
			st.execute();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -