`
yinhj
  • 浏览: 73961 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

RequestWoker.java

    博客分类:
  • IMTI
阅读更多
package com.msp.smg;

import java.util.List;

import org.apache.log4j.Logger;
import com.logica.smpp.Connection;
import com.logica.smpp.Data;
import com.logica.smpp.Receiver;
import com.logica.smpp.Session;
import com.logica.smpp.TCPIPConnection;
import com.logica.smpp.pdu.BindRequest;
import com.logica.smpp.pdu.BindResponse;
import com.logica.smpp.pdu.BindTransmitter;
import com.logica.smpp.pdu.Response;
import com.logica.smpp.pdu.SubmitSM;
import com.logica.smpp.pdu.UnbindResp;
import com.msp.smg.Queue;

public class RequestWorker extends Thread {
	private final static Logger logger = Logger.getLogger(RequestWorker.class);
	private boolean isRuning = true;
	private boolean isConnectSMSC = false;
	private boolean isBound = false;
	private Session session = null;
	private String sysId = "";
	private String syspwd = "";
	private String smscIP = "";
	private int smscPort = 0;
	private Receiver receiver = null;
	ResponseWorker responseWorker = null;
	private List<SendSM> sendsmList;

	/**
	 * 连接到短信中心
	 */
	private void connectSMSC() {
		try {
			Connection conn = new TCPIPConnection(smscIP, smscPort);
			session = new Session(conn);
			isConnectSMSC = true;
		} catch (Exception ex) {
			isConnectSMSC = false;
			ex.printStackTrace();
		}

	}

	/**
	 * 实现绑定到短信中心的功能
	 */
	private void bindSMSC() {
		if (isConnectSMSC && session != null) {
			try {
				BindRequest bindReq = new BindTransmitter();
				bindReq.setSystemId(sysId);
				bindReq.setPassword(syspwd);
				BindResponse bindResponse = null;

				bindResponse = session.bind(bindReq);

				if (bindResponse.getCommandStatus() == Data.ESME_ROK) {
					receiver = session.getReceiver();
					if (responseWorker == null) {
						responseWorker = new ResponseWorker();
					}
					responseWorker.setReceiver(receiver);
					responseWorker.start();
					isBound = true;
				} else {
					isBound = false;
				}

			} catch (Exception ex) {
				isBound = false;
				ex.printStackTrace();
			}
		} else {
			logger.info("连接到短信中心的connection失效!");
		}
	}

	public SendSM getSendSM() {
		SendSM sendsm = null;
		try {
			if (sendsmList != null && sendsmList.size() > 0) {
				sendsm = (SendSM) sendsmList.remove(0);
			}
		} catch (Exception e) {
			sendsm = null;
		}
		return sendsm;
	}

	public void run() {
		SendSM sendsm = null;
		while (isRuning) {
			try {

				// System.out.println("smscIP=" + smscIP + ",smscPort=" +
				// smscPort
				// + ",sysId=" + sysId + ",syspwd=" + syspwd);
				/*
				 * while (!isConnectSMSC) { this.connectSMSC(); } if (!isBound) {
				 * this.bindSMSC(); } while (isBound && this.getSendSM() !=
				 * null) { sendShortMessage(sendsm); } ; this.unBind();
				 */
			} finally {
				isRuning = true;

			}
		}

	}

	/*
	 * 发送短信
	 */

	public void sendShortMessage(SendSM sendsm) {
		SubmitSM request = new SubmitSM();
		try {
			request.setSourceAddr(sendsm.getSourceAddr());
			request.setDestAddr(sendsm.getDescAddr());
			request.setShortMessage(sendsm.getShortMessage());
			session.submit(request);
			// getStatus(response);
		} catch (Exception e) {
			logger.info(e.getMessage(), e);
		}
	}

	public void unBind() {
		try {
			UnbindResp response = session.unbind();
			getStatus(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getStatus(Response response) {
		if (response.getCommandStatus() == Data.ESME_ROK) {
			//
		} else {

		}
	}

	public void setSmscIP(String smscIP) {
		this.smscIP = smscIP;
	}

	public void setSmscPort(int smscPort) {
		this.smscPort = smscPort;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public void setSyspwd(String syspwd) {
		this.syspwd = syspwd;
	}
}

 

分享到:
评论
2 楼 yinhj 2009-03-09  
ypluck 写道

你好,能向你请教吗?公司有个项目近来也要开发短信方面的,我不大懂~



不好意思,最近太忙。没上线。

有什么问题吗?
1 楼 ypluck 2008-12-08  
你好,能向你请教吗?公司有个项目近来也要开发短信方面的,我不大懂~

相关推荐

Global site tag (gtag.js) - Google Analytics