`
piziwang
  • 浏览: 242724 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Proxool采用Listener加载

    博客分类:
  • J2EE
阅读更多

摘自:wangtao的spring+proxool解决方法:

package cn.wt.listener;

import java.io.File;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;

/**
 * @author wangtao
 */

public class ProxoolListener implements ServletContextListener
{
	private static final Log LOG = LogFactory.getLog(ProxoolListener.class);
    
	private static final String XML_FILE_PROPERTY = "xmlFile";

    private static final String PROPERTY_FILE_PROPERTY = "propertyFile";

    private static final String AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
    
    @SuppressWarnings("unused")
	private boolean autoShutdown = true;
	
    public void contextDestroyed(ServletContextEvent arg0)
	{
		System.out.println("destroy database pool....");
	}

	public void contextInitialized(ServletContextEvent contextEvent)
	{
		ServletContext context = contextEvent.getServletContext(); //对应servlet的init方法中ServletConfig.getServletContext()
		String appDir = contextEvent.getServletContext().getRealPath("/");
        Properties properties = new Properties();

        Enumeration names = context.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = context.getInitParameter(name);

            if (name.equals(XML_FILE_PROPERTY)) {
                try {
                    File file = new File(value);
                    if (file.isAbsolute()) {
                        JAXPConfigurator.configure(value, false);
                    } else {
                        JAXPConfigurator.configure(appDir + File.separator + value, false);
                    }
                } catch (ProxoolException e) {
                    LOG.error("Problem configuring " + value, e);
                }
            } else if (name.equals(PROPERTY_FILE_PROPERTY)) {
                try {
                    File file = new File(value);
                    if (file.isAbsolute()) {
                        PropertyConfigurator.configure(value);
                    } else {
                        PropertyConfigurator.configure(appDir + File.separator + value);
                    }
                } catch (ProxoolException e) {
                    LOG.error("Problem configuring " + value, e);
                }
            } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
                autoShutdown = Boolean.valueOf(value).booleanValue();
            } else if (name.startsWith("jdbc")) { //此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样
                properties.setProperty(name, value);
            }
        }

        if (properties.size() > 0) {
            try {
                PropertyConfigurator.configure(properties);
            } catch (ProxoolException e) {
                LOG.error("Problem configuring using init properties", e);
            }
        }
	}

}
 
分享到:
评论

相关推荐

    proxool listener

    标题“proxool listener”指的是Proxool监听器,这是一个与数据库连接池相关的概念。Proxool是Apache软件基金会的一个开源项目,它提供了一个轻量级的数据库连接池解决方案。监听器是Proxool中用于监控数据库连接池...

    Proxool-0.9.1

    <listener-class>org.logicalcobwebs.proxool.configuration.ListenerConfigurator</listener-class> 与spring配合使用时,本加载需要放在spring前面 <listener> <listener-class>org.logicalcobwebs.proxool....

    proxool配置说明

    还需要在项目的`web.xml`文件中配置监听器,以便在应用启动时加载`proxool.xml`配置文件。示例如下: ```xml xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation=...

    基于全注解方式的SSH基础框架

    1. **Log4jConfigListener**: 负责加载日志配置文件,在应用启动时就配置好日志输出。 2. **ProxoolListener**: 监听器用于初始化Proxool数据库连接池,Proxool是一个高性能的数据库连接池实现,支持多种数据库。 3....

    JSF+Spring+Hibernate

    - **Spring上下文加载监听器**:通过配置`ContextLoaderListener`,实现Spring应用上下文的初始化。 - `<context-param>`:指定Spring配置文件的位置。 - `<listener>`:注册`ContextLoaderListener`。 - **JSF...

Global site tag (gtag.js) - Google Analytics