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

📄 fidsclient.java

📁 短信发送
💻 JAVA
字号:
/**
 * Created at Dec 4, 2008
 */
package com.jdev.app.db;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import com.jdev.db.ConnectPool;
import com.jdev.net.config.ServerCfg;
import com.jdev.net.connector.Connection;
import com.jdev.net.connector.ConnectionFactory;
import com.jdev.net.event.EventAdapter;
import com.jdev.net.event.Notifier;
import com.jdev.net.queue.Request;
import com.jdev.net.queue.Response;
import com.jdev.util.Debug;

/**
 * <p>Title: FidsClient</p>
 * <p>Description: </p>
 * @author Lawrence
 * @version 1.0
 */
public class FidsClient extends EventAdapter implements Runnable {
	private FidsQueueHelper fidsDataHelper;

	public final static int DEF = 0;
	public final static int SMS = 1;
	public final static int EMAIL = 2;
	public final static int FIDS = 3;
	private java.sql.Connection conn;
	private ConnectPool connectionManager;
	private String fids;
	private ConnectionFactory connFactory = ConnectionFactory
			.getInstance(ConnectionFactory.CLIENT);

	private final static String module = DbClient.class.getName();
	protected Notifier notifier;
	private Connection client;
	private String appType = "fids";
	private FidsPush fidsPush;
	private boolean bStartFidsPush = false;

	/**
	 * 
	 */
	public FidsClient() {
		// 获取事件触发器
		notifier = Notifier.getNotifier();
		connectionManager = ConnectPool.getInstance();
		fidsDataHelper = new FidsQueueHelper(ConnectionFactory.CLIENT);
		fidsPush = new FidsPush();
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.event.EventAdapter#onRead(com.jdev.net.queue.Request)
	 */
	@Override
	public void onRead(Request request) throws Exception {
//		super.onRead(request);

		String recv = new String(request.getDataInput());
		if(recv.equalsIgnoreCase("ok")) {
			// 回复收到信息,略过
			return;
		}
		if (appType.equalsIgnoreCase("fids")) {
			if(!bStartFidsPush) {
				Thread s = new Thread(fidsPush);
				s.start();
			}
			fidsDataHelper.writeSend(recv);
			bStartFidsPush = true;
		}
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.event.EventAdapter#onWrite(com.jdev.net.queue.Request, com.jdev.net.queue.Response)
	 */
	@Override
	public void onWrite(Request request, Response response) throws Exception {
		if (request == null)
			return;
 
		response.setDataInput(fidsDataHelper.readRecv().getBytes());
//		super.onWrite(request, response);
	}

	public void openDb(String DriverName, String connString, String userName,
			String password) throws Exception {
		// // 1. 注册驱动
		// try {
		// Class.forName(DriverName);
		// } catch (ClassNotFoundException e) {
		// e.printStackTrace();
		// }// Mysql 的驱动
		//		
		// try {
		// // 2. 获取数据库的连接
		// conn = DriverManager.getConnection(
		// connString,userName,password);
		//			
		// } catch (Exception e) {
		// Debug.logError(e, module);
		// throw new Exception(e);
		// }

		conn = connectionManager.getConnection("test");
		InputStream is = getClass().getClassLoader().getResourceAsStream(
				"db.properties");
		Properties dbProps = new Properties();
		try {
			dbProps.load(is);
		} catch (Exception e) {
			Debug.logError("不能读取属性文件. " + "请确保db.properties在CLASSPATH指定的路径中");
		}
		fids = dbProps
				.getProperty(
						"fids",
						"SELECT * FROM MV_SMS_Receiver where receivestate = '0' and id < (select min(id) from mv_sms_receiver where receivestate = '0')+10");
	}

	public void close() {
		try {

		} catch (Exception e) {
			Debug.logError(e, module);
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		try {
			client = connFactory.getConnection();
			ServerCfg cfg = new ServerCfg();
//			appType = cfg.getAppType();
			client.open(cfg.getUrl(), cfg.getTcpPort());
			fidsDataHelper.writeRecv(appType);

		} catch (Exception e) {
			notifier.fireOnError("-->Error occured in Connect to Server: "
					+ e.getMessage());

		}

		try {
			this.openDb("oracle.jdbc.driver.OracleDriver",
					"jdbc:oracle:thin:@192.168.168.121:1521:GWTWOC", "gwtexpo",
					"gwtexpo");

		} catch (Exception e) {
			notifier.fireOnError("-->Error occured in DbClient openDb: "
					+ e.getMessage());
		}
		// 监听
		while (!Thread.interrupted()) {
			try {
				Thread.sleep(1000);

				if (conn == null || conn.isClosed()) {
					Thread.sleep(10000);
					try {
						this
								.openDb(
										"oracle.jdbc.driver.OracleDriver",
										"jdbc:oracle:thin:@192.168.168.121:1521:GWTWOC",
										"gwtexpo", "gwtexpo");

					} catch (Exception e) {
						notifier.fireOnError("-->Error occured in DbClient openDB: "
								+ e.getMessage());
					}

					continue;
				}

			} catch (Exception e) {
				notifier.fireOnError("-->Error occured in DbClient: "
						+ e.getMessage());
				continue;

			}
		}

	}

	public static void main(String[] args) throws Exception {
		try {
			FidsClient d = new FidsClient();
			Notifier notifier = Notifier.getNotifier();
			notifier.addListener(d);
			Thread s = new Thread(d);
			s.start();
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	/* (non-Javadoc)
	 * @see com.jdev.net.event.EventAdapter#onStatus(java.lang.String, int)
	 */
	@Override
	public void onStatus(String key, int status) {
//		super.onStatus(key, status);
		
		java.sql.Statement stmt = null;
		try {
			fidsDataHelper.writeRecv(appType);
			// 3. 获取表达式
			stmt = conn.createStatement();
			if (status == 0) {
				StringBuilder strUpdate = new StringBuilder(
						"update TB_SMS_Receiver set RECEIVESTATE='2' where CLIENTINFOID in (");
				strUpdate.append(key);
				strUpdate.append(")");

				// 读取sms
				int len = stmt.executeUpdate(strUpdate.toString());
			} else {
				StringBuilder strUpdate = new StringBuilder(
						"update TB_SMS_Receiver set RECEIVESTATE=" + status + " where CLIENTINFOID in (");
				strUpdate.append(key);
				strUpdate.append(")");

				// 读取sms
				int len = stmt.executeUpdate(strUpdate.toString());

			}

			// 6. 释放资源
			stmt.close();
		} catch (Exception e) {
			notifier.fireOnError("-->Error occured in DbClient onStatus: "
					+ e.getMessage());
			try {
				stmt.close();
			} catch (SQLException e1) {
				notifier.fireOnError("-->Error occured in DbClient stmt.close: "
						+ e.getMessage());
				
			}
		}
	}

}

⌨️ 快捷键说明

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