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

📄 cjscsessionpool.java

📁 java 实现DOMINO远程连接
💻 JAVA
字号:
package com.cjsc.it.auth;

import java.util.HashMap;
import java.util.Vector;

import lotus.domino.NotesFactory;
import lotus.domino.Session;

/**
 * 
 * @author 张阿强 <br>
 *         <b>功能描述 <b>: <br>
 *         <b>实现思路 <b>: <br>
 *         <b>用例说明 <b>: <br>
 * @email ikhelon@gmail.com <br>
 * @time 2008-12-26
 */
public class CjscSessionPool {

	// 没有用过的连接池,用vector实现同步
	private Vector freeSessionPool = new Vector();
	// 没有用过的连接池
	private HashMap busySessionPool = new HashMap();
	// 正在死亡状态的连接池

	private String host = "10.0.16.5";
	private String userName = "tongji";
	private String password = "password";
	// 默认为10个连接池
	private int MAX_POOL = 5;

	// singleTon 设计模式
	public CjscSessionPool(String host, String name, String password, int max) {
		this.host = host;
		this.userName = name;
		this.password = password;
		this.MAX_POOL = max;

		// 初始化连接
		for (int i = 0; i < max; i++) {
			try {
				Session session = createSession(host, userName, password);
				freeSessionPool.add(session);
				System.out.println("##########初始创建##########");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// public boolean a = false;

	// 通过连接池获得真正的链接
	public synchronized Session getSession() throws Exception {
		// synchronized (freeSessionPool)
		{
			// if (a) {
			// throw new Exception("模拟服务器宕机");
			// }

			while (freeSessionPool.size() > 0
					&& freeSessionPool.size() + busySessionPool.size() <= MAX_POOL) {
				Session session = (Session) freeSessionPool.firstElement();
				freeSessionPool.remove(session);
				if (session == null || !session.isValid()) {
					// 或获取的连接已失效,重新创建一个

					System.out.println("##########临时创建##########");
					session = createSession(host, userName, password);
				}
				busySessionPool.put(session, session);
				// System.out.println("@@空闲连接数:" + freeSessionPool.size() +
				// "忙连接数:"
				// + busySessionPool.size());
				return session;
			}
		}
		return null;
		// 连接池没有链接了, 重新创建一个--这功能个不用了。
		// Session session = createSession(host, userName, password);
		// System.out.println("##########临时创建##########");
		// busySessionPool.put(session, session);
		// return session;
	}

	// 如果连接池没有链接了,就需要产生一个链接
	private Session createSession(String host, String userName, String password)
			throws Exception {
		Session session = NotesFactory.createSession(host, userName, password);
		return session;
	}

	public synchronized void releaseSession(Session session, String poolName) {
		// synchronized (freeSessionPool)
		{
			if (session != null) {
				busySessionPool.remove(session);
				freeSessionPool.add(session);
			}
			// System.out.println(poolName + "空闲连接数=" + freeSessionPool.size()
			// + "忙连接数=" + busySessionPool.size());

			// if (freeSessionPool.size() + busySessionPool.size() < MAX_POOL) {
			// busySessionPool.remove(session);
			// freeSessionPool.add(session);
			// } else {
			// session.recycle();
			// }
		}

	}

}

⌨️ 快捷键说明

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