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

📄 asyncreceiverexample30.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 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.UnsupportedOperationException;
import com.bci.cmpp.event.CMPPEventAdapter;
import com.bci.cmpp.event.ReceiverExitEvent;
import com.bci.cmpp.message.*;
import com.bci.cmpp.util.ASCIIEncoding;
import com.bci.cmpp.util.AlphabetEncoding;
import com.bci.cmpp.util.GBKEncoding;
import com.bci.cmpp.util.HPRoman8Encoding;
import com.bci.cmpp.util.Latin1Encoding;
import com.bci.cmpp.util.UCS2Encoding;
import com.bci.cmpp.util.UTF16Encoding;

public class AsyncReceiverExample30 extends CMPPAPIExample {

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

	// Number of deliver_sm packets received
	private int msgCount = 0;

	public AsyncReceiverExample30() {
	}

	protected boolean reConnect() {
		try {
			Thread.sleep(1000);
			myConnection = new Connection(this.hostName, this.port, true);
			myConnection.setVersion(CMPPConstants.CMPP_VERSION_3);
			logger.info("Binding to the SMSG..");
			CMPPConnectResp resp = myConnection.bind(Connection.RECEIVER,
					this.icpId, this.password);
			if (resp != null ){
				logger.info("The SMSG version is "+resp.getVersion());
				if(resp.getCommandStatus() != 0)
				logger.info("SMSG bind failed.");
				}
		} 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.");
		// Create the observer
		AsyncExampleObserver observer = new AsyncExampleObserver();

		// set the receiver to automatically acknowledge deliver_sm and
		// enquire_link requests from the SMSG.
		myConnection.autoAckLink(true);
		myConnection.autoAckMessages(true);

		// add this example to the list of observers on the receiver connection
		myConnection.addObserver(observer);

		return true;
	}

	public void execute() {
		int count = 0;
		while (true) {
			count++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			while (!isConnected()) {
				disConnect();
				reConnect();
			}
			/*
			if (count % 30 == 0) {
				try {
					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();
				}
			}
			*/
			// 测试:接收到一条就退出
			// if (msgCount > 0)
			// break;
		}
	}

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

	private class AsyncExampleObserver extends CMPPEventAdapter {

		public AsyncExampleObserver() {
		}

		// Handle message delivery. This method does not need to acknowledge the
		// deliver_sm message as we set the Connection object to
		// automatically acknowledge them.
		public void deliverSM(Connection source, CMPPDeliver dm) {
			++msgCount;
			// logger.info("Deliver SM:\n" + encodeHex(dm.getBytes()));
			// logger.info(new String(dm.getBuffer()));
			int dc = dm.getMsgCoding();
			AlphabetEncoding alphabetEncoding = ASCIIEncoding.getInstance();
			try {
				if (dc == (UCS2Encoding.getInstance().getDataCoding()))
					alphabetEncoding = UCS2Encoding.getInstance();
				else if (dc == (GBKEncoding.getInstance().getDataCoding()))
					alphabetEncoding = GBKEncoding.getInstance();
				else if (dc == (HPRoman8Encoding.getInstance().getDataCoding()))
					alphabetEncoding = HPRoman8Encoding.getInstance();
				else if (dc == (Latin1Encoding.getInstance().getDataCoding()))
					alphabetEncoding = Latin1Encoding.getInstance();
				else if (dc == (UTF16Encoding.getInstance().getDataCoding()))
					alphabetEncoding = UTF16Encoding.getInstance();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			logger.info(dm.getMsgIdStr() + "|" + dm.getRegisteredDelivery()
					+ "|" + dm.getDestTerminalId() + "|" + dm.getDestId() + "|"
					+ dm.getSrcTerminalId() + "|" + dm.getStatusMsgIdStr()
					+ "|" + dm.getServiceId());

			logger.info("isReport:" + dm.getRegisteredDelivery());
			if (dm.getRegisteredDelivery() == 1) {
				logger.info("msgid=" + dm.getMsgIdStr().trim());
				logger.info("statusMsgId=" + dm.getStatusMsgIdStr().trim());
				logger.info("stat=" + dm.getStat());
			} else {
				logger.info("msgId=" + dm.getMsgIdStr());
				logger.info("srcAddress=" + dm.getSrcTerminalId());
				logger.info("destAddress=" + dm.getDestId());
				String content = dm.getMsgText(alphabetEncoding);
				logger.info("content: \"" + content + "\"");
			}
		}

		public void deliverSM(Connection source, CMPPDeliver30 dm) {
			++msgCount;
			// logger.info("Deliver SM:\n" + encodeHex(dm.getBytes()));
			// logger.info(new String(dm.getBuffer()));
			int dc = dm.getMsgCoding();
			AlphabetEncoding alphabetEncoding = ASCIIEncoding.getInstance();
			try {
				if (dc == (UCS2Encoding.getInstance().getDataCoding()))
					alphabetEncoding = UCS2Encoding.getInstance();
				else if (dc == (GBKEncoding.getInstance().getDataCoding()))
					alphabetEncoding = GBKEncoding.getInstance();
				else if (dc == (HPRoman8Encoding.getInstance().getDataCoding()))
					alphabetEncoding = HPRoman8Encoding.getInstance();
				else if (dc == (Latin1Encoding.getInstance().getDataCoding()))
					alphabetEncoding = Latin1Encoding.getInstance();
				else if (dc == (UTF16Encoding.getInstance().getDataCoding()))
					alphabetEncoding = UTF16Encoding.getInstance();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			logger.info(dm.getMsgIdStr() + "|" + dm.getRegisteredDelivery()
					+ "|" + dm.getDestTerminalId() + "|" + dm.getDestId() + "|"
					+ dm.getSrcTerminalId() + "|" + dm.getStatusMsgIdStr()
					+ "|" + dm.getServiceId());

			logger.info("isReport:" + dm.getRegisteredDelivery());
			if (dm.getRegisteredDelivery() == 1) {
				logger.info("msgid=" + dm.getMsgIdStr().trim());
				logger.info("statusMsgId=" + dm.getStatusMsgIdStr().trim());
				logger.info("stat=" + dm.getStat());
			} else {
				logger.info("msgId=" + dm.getMsgIdStr());
				logger.info("srcAddress=" + dm.getSrcTerminalId());
				logger.info("destAddress=" + dm.getDestId());
				String content = dm.getMsgText(alphabetEncoding);
				logger.info("content: \"" + content + "\"");
			}
		}
		// Called when a bind response packet is received.
		public void bindResponse(Connection source, CMPPConnectResp br) {
			if (br.getCommandStatus() == 0)
				logger.info("Successfully bound. Awaiting messages..");
			else {
				logger.info("Bind did not succeed!");
				try {
					myConnection.closeLink();
				} catch (IOException x) {
					logger.info("IOException closing link:\n" + x.toString());
				}
				synchronized (AsyncReceiverExample30.this) {
					// on exiting this block, we're sure that
					// the main thread is now sitting in the wait
					// call, awaiting the unbind request.
				}
			}
		}

		// This method is called when the SMSG sends an unbind request to our
		// receiver. We must acknowledge it and terminate gracefully..
		public void unbind(Connection source, CMPPTerminate ubd) {
			logger.info("SMSG requested unbind. Acknowledging..");

			try {
				// SMSG requests unbind..
				CMPPTerminateResp ubr = new CMPPTerminateResp(ubd);
				myConnection.sendResponse(ubr);
			} catch (IOException x) {
				logger
						.info("IOException while acking unbind.\n"
								+ x.toString());
			}
		}

		// This method is called when the SMSG responds to an unbind request we
		// sent
		// to it..it signals that we can shut down the network connection and
		// terminate our application..
		public void unbindResponse(Connection source, CMPPTerminateResp ubr) {
			int st = ubr.getCommandStatus();

			if (st != 0) {
				logger.info("Unbind response: !Error! status = " + st);
			} else {
				logger.info("Successfully unbound.");
			}
		}

		// this method is called when the receiver thread is exiting normally.
		public void receiverExit(Connection source, ReceiverExitEvent ev) {
			if (ev.getReason() == ReceiverExitEvent.BIND_TIMEOUT) {
				logger.info("Bind timed out waiting for response.");
			}

			logger.info("Receiver thread has exited.");
			synchronized (AsyncReceiverExample30.this) {
				AsyncReceiverExample30.this.notify();
			}
		}

		// this method is called when the receiver thread exits due to an
		// exception
		// in the thread...
		public void receiverExitException(Connection source,
				ReceiverExitEvent ev) {
			logger
					.info("Receiver thread exited abnormally. The following"
							+ " exception was thrown:\n"
							+ ev.getException().toString());
			synchronized (AsyncReceiverExample30.this) {
				AsyncReceiverExample30.this.notify();
			}
		}

	}

}

⌨️ 快捷键说明

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