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

📄 cmppapi.java

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

import com.gctech.cmpp3.msg.SubmitRequest;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.gctech.cmpp3.util.IDGenerator;
import org.apache.log4j.PropertyConfigurator;
import java.io.FileInputStream;
import java.io.InputStream;
import com.gctech.cmpp3.msg.MsgHead;

public class CmppApi {
	public String getServiceNo() {
		return spSrcId;
	}
	private String spSrcId;
	CmppCommunicator commu;
	private static CmppApi singleton;
	public static CmppApi getInstance() {
		if (singleton == null)
			singleton = new CmppApi();
		return singleton;
	}
	CmppApi() {

	}

	/**
	 * 不等待回复的发送消息。
	 * */
	public int sendMsgNoRes(SubmitRequest msg) throws IOException {
		logger.debug(msg);
		//产生序列号
		MsgHead head = msg.getHead();
		head.setSequenceId(IDGenerator.getInstance().increment());
		//计算数据长度
		int msgLength = msg.getMsgContent().getBytes().length;
		head.setTotalLength(12 + 183 + msgLength);
		msg.setMsgLength(msgLength);
		//设置真正的srcId
		msg.setMsgSrc(this.clientId);
		if (msg.getSrcId() == null)
			msg.setSrcId(this.spSrcId);
		else
			msg.setSrcId(this.spSrcId + msg.getSrcId());
		try {
			commu.send(msg.toByteArray());
			return 0;
		} catch (Exception ex) {
			ex.printStackTrace();
			return -100;
		}
	}
	private String clientId;
	/**
	 * 初试化连接。
	 * @param fileName 配置文件名。
	 * */
	public int init(Properties props) {
		//读取配置参数
		try {
			String host = props.getProperty("com.gctech.cmpp.ip");
			System.out.println("host:" + host);
			int port = Integer.parseInt(props.getProperty("com.gctech.cmpp.port"));
			System.out.println("port:" + port);
			clientId = props.getProperty("com.gctech.cmpp.clientId");
			String auth = props.getProperty("com.gctech.cmpp.sharedSecret");
			int timeout = Integer.parseInt(props.getProperty("com.gctech.cmpp.timeout"));
			spSrcId = props.getProperty("com.gctech.cmpp.spSrcId");

			//初试化通信对象
			commu = (CmppCommunicator) (Class.forName(props.getProperty("com.gctech.cmpp.communicatorClass")).newInstance());
			//连接网关
			int rt = commu.login(host, port, clientId, auth, timeout);
			//如果登录不成功,退出登录
			if (rt != 0) {
				commu.quit();
			} else {
				//启动消息接收线程
				Receiver receiver = (Receiver) (Class.forName(props.getProperty("com.gctech.cmpp.receiverClass")).newInstance());
				receiver.setCommunicator(commu);
				Thread t = new Thread(receiver, "CMPP_RECEIVER");
				logger.debug("starting CMPP_RECEIVER!");
				t.start();
				//启动检测线程
				Thread active = new Thread(new ActiveTester(commu), "ACTIVE_TESTER");
				logger.debug("starting ACTIVE_TESTER!");
				active.start();
				Runtime.getRuntime().addShutdownHook(new Thread() {
					public void run() {
						try {
							commu.quit();
							logger.debug("哈哈,成功了!");
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				});
			}

			return rt;
		} catch (Throwable ex) {
			logger.fatal(ex, ex);
			return -300;
		}
	}
	public static void main(String[] args) {
		try {
			CmppApi cmppApi = CmppApi.getInstance();
			PropertyConfigurator.configure("./conf/cmpp3cli.properties");
			Properties props = new Properties();
			InputStream in = new FileInputStream("./conf/cmpp3cli.properties");
			props.load(in);
			in.close();
			cmppApi.init(props);
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	static final Logger logger = Logger.getLogger(CmppApi.class);

}
class ActiveTester implements Runnable {
	CmppCommunicator client;
	public ActiveTester(CmppCommunicator client) {
		this.client = client;
	}
	int interval = 10 * 1000;
	public void run() {
		while (true) {
			try {
				Thread.sleep(interval);

			} 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 + -