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

📄 cngpapi.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.cngphb;

import java.util.Properties;
import java.io.FileInputStream;
import java.io.*;
import org.apache.log4j.Logger;
import com.gctech.cngphb.msg.CNGPSubmitMessage;
import com.gctech.cngphb.util.SequenceManager;
import com.gctech.cngphb.msg.CNGPSubmitMessageResp;
import com.gctech.cngphb.msg.CNGPDeliverMessage;

/**
 * <p>Title: CNGP API</p>
 * <p>Description: 固网短信SP API</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: GCTECH</p>
 * @author 王红宝
 * @version $Id: CngpApi.java,v 1.3 2004/08/25 07:44:33 lijz Exp $
 */

public class CngpApi {
	public String getServiceNo() {
		return spSrcId;
	}
	public int getGateway() {
		return gateWay;
	}

	private String spSrcId;
	private int gateWay;
	CngpCommunicator commu;
	private static CngpApi singleton;
	public static CngpApi getInstance() {
		if (singleton == null)
			singleton = new CngpApi();
		return singleton;
	}
	CngpApi() {
		//PropertyConfigurator.configureAndWatch("./conf/log4j.properties");
	}

	/**
	 * 不等待回复的发送消息。
	 * */
	public int cngpSendMsgNoRes(CNGPSubmitMessage msg) throws IOException {
		logger.debug(msg);
		//产生序列号
		msg.nSequenceID = SequenceManager.nextSeq();
		//设置真正的srcId
		if (msg.sSrcId == null)
			msg.sSrcId = this.spSrcId;
		//test.lijz
		//    else
		//      msg.sSrcId = this.spSrcId + msg.sSrcId;
		return commu.submit(msg);
	}

	/**
	 * 初试化连接。
	 * 使用缺省文件名 "./conf/cngp.ini"
	 * */
	public int initCngpApi() {
		return initCngpApi("./conf/cngp.ini");
	}
	/**
	 * 初试化连接。
	 * @param fileName 配置文件名。
	 * */
	public int initCngpApi(String fileName) {
		//读取配置参数
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(fileName));
			String host = props.getProperty("com.gctech.cngp.ip");
			int port = Integer.parseInt(props.getProperty("com.gctech.cngp.port"));
			String clientId = props.getProperty("com.gctech.cngp.clientId");
			String auth = props.getProperty("com.gctech.cngp.sharedSecret");
			byte loginMode = Byte.parseByte(props.getProperty("com.gctech.cngp.loginMode"));
			byte version = 0x10;
			spSrcId = props.getProperty("com.gctech.cngp.spSrcId");
			//得到网关号,如果没有,则认为是中国网通一级网关(2);//update by lijz;
			gateWay = Integer.parseInt(props.getProperty("com.gctech.cngp.gateway", "2"));

			//初试化通信对象
			commu =
				(CngpCommunicator) (Class
					.forName(props.getProperty("com.gctech.cngp.communicatorClass"))
					.newInstance());
			//连接网关
			int rt = commu.login(host, port, clientId, auth, loginMode, version);
			//如果登录不成功,退出登录
			if (rt != 0) {
				commu.quit();
			} else {
				//启动消息接收线程
				ReceiveDataHandler receiver =
					(ReceiveDataHandler) (Class
						.forName(props.getProperty("com.gctech.cngp.receiverClass"))
						.newInstance());
				receiver.setCommunicator(commu);
				Thread t = new Thread(receiver, "CNGP_RECEIVER");
				logger.debug("starting CNGP_RECEIVER!");
				t.start();
				//启动检测线程
				Thread active = new Thread(new ActiveTester(commu), "ACTIVE_TESTER");
				logger.debug("starting ACTIVE_TESTER!");
				active.start();
			}
			return rt;
		} catch (Throwable ex) {
			logger.fatal(ex, ex);
			return -300;
		}
	}

	static final Logger logger = Logger.getLogger(CngpApi.class);
	class ActiveTester implements Runnable {
		CngpCommunicator client;
		public ActiveTester(CngpCommunicator client) {
			this.client = client;
		}
		int interval = 10 * 1000;
		public void run() {
			while (true) {
				try {
					Thread.sleep(interval);
					logger.debug("active test");
				} catch (InterruptedException ex) {
					ex.printStackTrace();
				}
				try {
					client.activeTest(1);
				} catch (Exception ex) {
					System.out.println("Error test ,try relogin " + ex.getMessage());
					try {
						client.relogin();
					} catch (Exception exe) {
						System.out.println("Error relogin EXIST " + ex.getMessage());
						System.exit(0);
					}

				}

			}
		}
	}
}

⌨️ 快捷键说明

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