#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