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

📄 senderthread.java

📁 彩信接入系统
💻 JAVA
字号:
/*
 * Created on 2005-2-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.rainbow.mms.gateway;


import java.util.Collection;
import java.util.LinkedList;
import java.util.List;


import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * 实现发送彩信Submit消息的线程
 * @author Rainbow MMS Group Leader —— TrWorks
 */
abstract class SenderThread extends Thread {

	/**
	 * 运行标志,当线程运行后,将该标志置为false后,线程函数将退出
	 */
	private static volatile boolean bRun = true;

	/**
	 * 发送队列,该队列存放要发送的Submit消息。 将listSend作为静态成员以供所有的发送者线程共同使用
	 */
	private static List listSend = new LinkedList();

	/**
	 * 发送所用的连接参数,企业代码
	 */
	private static String spID = null;

	/**
	 * 发送所用的连接参数,接入号码
	 */
	private static String serviceID = null;

	/**
	 * 发送所用的连接参数,连接用户名称
	 */
	private static String linkUser = null;

	/**
	 * 发送所用的连接参数,连接密码
	 */
	private static String linkPassword = null;

	/**
	 * 发送所用的连接参数,连接地址
	 */
	private static String linkUrl = null;
	
	/**
	 * 是否必要状态报告
	 */
	protected static int wantReport = 0;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(SenderThread.class);

	/**
	 * 构造函数
	 *
	 */
	public SenderThread(){
		// 加载Log4j的配置
		PropertyConfigurator.configure("conf/log4j.properties");
	}
	
	/**
	 * 将要发送的Submit队列加入到listSend中,并唤醒发送者线程,让他发送
	 * 
	 * @param Collection
	 *            o 将要发送的Submit队列加入到listSend中
	 */
	public static void addSubmitMsgToList(Collection o) {
		synchronized (listSend) {
			//System.out.println("向队列中添加Submit消息:" + o.size());
			listSend.addAll(o);
			//System.out.println("向队列中成功地添加Submit消息");
			listSend.notifyAll();
		}
	}

	/**
	 * 设置发送线程的发送连接参数
	 * 
	 * @param spID
	 *            企业代码
	 * @param serviceID
	 *            接入号码
	 * @param linkUser
	 *            连接用户名
	 * @param linkPassword
	 *            连接密码
	 * @param linkUrl
	 *            连接地址
	 */
	public static void setSenderConfig(String spID, String serviceID,
			String linkUser, String linkPassword, String linkUrl) {

		SenderThread.spID = spID;
		SenderThread.serviceID = serviceID;
		SenderThread.linkUser = linkUser;
		SenderThread.linkPassword = linkPassword;
		SenderThread.linkUrl = linkUrl;
	}

	/**
	 * 线程函数,负责从发送队列里获得Submit消息,然后发送它
	 */
	public void run() {

		log.debug("线程开始运行");
		
		while (bRun || listSend.isEmpty() == false) {

			try {
				Object m = null;

				synchronized (listSend) {
					// 如果发送队列为空,则让线程等待,直到被唤醒
					if (listSend.isEmpty() == true) {
						//log.info("发送队列为空,等待...");
						listSend.wait();
						//log.info("线程唤醒");						
					}
					
					if (listSend.isEmpty() == false){ 
						// 从队列中获得一个要发的SUBMIT消息
						m = listSend.remove(0);
						//log.info("从队列中获得一个要发的SUBMIT消息");
					}
					
				}
				
				// 将SUBMIT消息发送出去
				if (m != null) {
					//log.info("要发送的彩信");
					sendSubmitMessage(m);
				}
		
			} catch (Throwable e) {
				e.printStackTrace();
				log.error("线程函数里发生异常");
			}
		}
		
		log.debug("发送者线程终止运行");
	}

	/**
	 * 将要发送的Submit消息发送出去
	 * 
	 * @param m
	 *            要发送的Submit消息
	 */
	protected abstract void sendSubmitMessage(Object m);

	/**
	 * @return Returns the linkPassword.
	 */
	public final String getLinkPassword() {
		return linkPassword;
	}

	/**
	 * @return Returns the linkUrl.
	 */
	public final String getLinkUrl() {
		return linkUrl;
	}

	/**
	 * @return Returns the linkUser.
	 */
	public final String getLinkUser() {
		return linkUser;
	}

	/**
	 * @return Returns the serviceID.
	 */
	public final String getServiceID() {
		return serviceID;
	}

	/**
	 * @return Returns the spID.
	 */
	public final String getSpID() {
		return spID;
	}
	/**
	 * @param run The bRun to set.
	 */
	public static final void setBRun(boolean run) {
		bRun = run;
	}
	
	/**
	 * 获得发送队列的当前大小(队列中要发送Submit消息的个数)
	 * @return 队列中要发送Submit消息的个数
	 */
	public static int getSendListSize(){
		synchronized(listSend){
			return listSend.size();
		}
	}
	/**
	 * 
	 * @author Wangzhaonan
	 * 
	 * TODO 记录发送结果用的,作为Submit的应答记录使用
	 */
	protected final class SenderResult {
		private int centuryMsgID;

		private String messageID;

		private String retCode;

		private String retMessage;

		public String toString(){
			StringBuffer buffer = new StringBuffer();
			buffer.append("SendResult:\n");
			buffer.append("    CenturyMsgID: " + centuryMsgID);
			buffer.append("    MessageID: " + messageID);
			buffer.append("    RetCode: " + retCode);
			buffer.append("    RetMessage: " + retMessage);
			
			return buffer.toString();
		}
		/**
		 * @return Returns the messageID.
		 */
		public final String getMessageID() {
			return messageID;
		}

		/**
		 * @param messageID
		 *            The messageID to set.
		 */
		public final void setMessageID(String messageID) {
			this.messageID = messageID;
		}

		/**
		 * @return Returns the retCode.
		 */
		public final String getRetCode() {
			return retCode;
		}

		/**
		 * @param retCode
		 *            The retCode to set.
		 */
		public final void setRetCode(String retCode) {
			this.retCode = retCode;
		}

		/**
		 * @return Returns the retMessage.
		 */
		public final String getRetMessage() {
			return retMessage;
		}

		/**
		 * @param retMessage
		 *            The retMessage to set.
		 */
		public final void setRetMessage(String retMessage) {
			this.retMessage = retMessage;
		}

		/**
		 * @return Returns the centuryMsgID.
		 */
		public final int getCenturyMsgID() {
			return centuryMsgID;
		}

		/**
		 * @param centuryMsgID
		 *            The centuryMsgID to set.
		 */
		public final void setCenturyMsgID(int centuryMsgID) {
			this.centuryMsgID = centuryMsgID;
		}
	}

}

⌨️ 快捷键说明

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