JiwonDev

#21 Servlet Context Listener

by JiwonDev

# Servlet Context Listener

์›น ์ปจํ…Œ์ด๋„ˆ์—์„œ ์›น ์•ฑ์ด ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์ข…๋ฃŒ๋˜๋Š” ์‹œ์ ์— ํŠน์ • ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

javax.servlet.ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๊ณ , web.xml์— ๋“ฑ๋กํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 


# Servlet Context Listener

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์ข…๋ฃŒ๋  ๋•Œ ํ˜ธ์ถœํ•  ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค.

์ฐธ๊ณ ๋กœ <listener> ํƒœ๊ทธ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ฉด ๋“ฑ๋กํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์€ ์Šคํƒ๊ตฌ์กฐ๋กœ ์‹คํ–‰, ์ข…๋ฃŒ๋œ๋‹ค. (๋จผ์ € ์‹œ์ž‘ -> ๋‚˜์ค‘ ์ข…๋ฃŒ)

- public void contextInitialized(ServletContextEvent sce) : ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ํ˜ธ์ถœ

- public void contextDestroyed(ServletContextEvent sce) : ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ข…๋ฃŒํ•  ๋•Œ ํ˜ธ์ถœ.

 

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋˜๊ณ , ์ข…๋ฃŒ๋  ๋•Œ ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋œ๋‹ค.

 1. javax.servlet.ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 2. web.xml ํŒŒ์ผ์— 1๋ฒˆ์—์„œ ์ž‘์„ฑํ•œ ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋กํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ์†์„ฑ๊ฐ’์„ ์ฝ”๋“œ์™€ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Java์ฝ”๋“œ์— ์‚ฌ์šฉํ•  ํŒŒ๋ผ๋ฉ”ํƒ€๋ฅผ <context-param>์œผ๋กœ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž๋ฐ”์ฝ”๋“œ์—์„œ๋Š” prop = getInitParameter("๊ฐ€์ ธ์˜ฌ param ํƒœ๊ทธ ์ด๋ฆ„")๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
 
 	<listener>
 		<listener-class>jdbc.DoDBCPInitListener</listener-class>
 	</listener>
 	
 	<context-param>
 		<param-name>connectionPoolConfiguration</param-name>
 		<param-value>
 			jdbcdriver=com.mysql.jdbc.Driver
 			jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
 			dbUser=testuser
 			dbPass=test123
 		</param-value>
 	</context-param>
 </web-app>
public class DoDBCPInitListener implements ServletContextListener{
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		String poolConfig = sce.getServletContext()
                                        .getInitParameter("connectionPoolConfiguration");
		
        // web.xml์—์„œ ๋ฐ›์•„์˜จ ์ •๋ณด๋ฅผ prop ๊ฐ์ฒด๋กœ ๋งŒ๋“ ๋‹ค.
        Properties prop = new Properties();
		try {
			prop.load(new StringReader(poolConfig));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
        
        // ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ prop ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ฐ’๋“ค์„ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
        String jdbcUrl = prop.getProperty("jdbcUrl");
        String username = prop.getProperty("dbUser");
        String pw = prop.getProperty("dbPass");
	}
}

 


 

# Servlet Context Listener๋ฅผ ์ด์šฉํ•œ DBCP Init ์ฝ”๋“œ

public class DoDBCPInitListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        String poolConfig = sce.getServletContext().getInitParameter("connectionPoolConfiguration");
        Properties prop = new Properties();
        try {
            prop.load(new StringReader(poolConfig));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        loadJDBCDriver(prop);
        initConnectionPool(prop);
    }

    private void initConnectionPool(Properties prop) {
        try {
            String jdbcUrl = prop.getProperty("jdbcUrl");
            String username = prop.getProperty("dbUser");
            String pw = prop.getProperty("dbPass");

            ConnectionFactory connFactory = new DriverManagerConnectionFactory(jdbcUrl, username, pw);

            PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, null);
            poolableConnFactory.setValidationQuery("select 1");

            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setTimeBetweenEvictionRunsMillis(1000L * 60L * 5L);
            poolConfig.setTestWhileIdle(true);
            poolConfig.setMinIdle(4);
            poolConfig.setMaxTotal(50);

            GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnFactory, poolConfig);

            poolableConnFactory.setPool(connectionPool);

            Class.forName("org.apache.commons.dbcp2.PoolingDriver");
            PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
            String poolName = prop.getProperty("poolName");
            driver.registerPool(poolName, connectionPool);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private void loadJDBCDriver(Properties prop) {
        String driverClass = prop.getProperty("jdbcdriver");
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("fail to load JDBC Driver", e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

 


# @WebListener ๋ฅผ ์ด์šฉํ•œ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก

์„œ๋ธ”๋ฆฟ ์ŠคํŽ™3.0๋ถ€ํ„ฐ ์ œ๊ณตํ•˜๋Š” @์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜๋ฉด, web.xml๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

import javax.servlet.annotation.WebListener;

@WebListener
public class TestListener implements ServletContextListener {
	...//๋‚ด์šฉ
}

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ