From 194a9a763c6b11d207e682b2f93de94475c473b4 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 12 Nov 2016 01:34:07 +0700 Subject: extracted application configuration from Mvc configuration with application specific beans; extracted bean initialization from constructor; force using properties; --- .../com/juick/configuration/DataConfiguration.java | 19 ++++-------- .../src/main/java/com/juick/util/ThreadHelper.java | 36 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/util/ThreadHelper.java (limited to 'juick-core/src/main/java/com/juick') diff --git a/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java index 62ca0f88..f31dbcdb 100644 --- a/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java +++ b/juick-core/src/main/java/com/juick/configuration/DataConfiguration.java @@ -1,11 +1,9 @@ package com.juick.configuration; import org.apache.commons.dbcp2.BasicDataSource; -import org.springframework.beans.factory.config.PlaceholderConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.Environment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @@ -21,26 +19,21 @@ import javax.sql.DataSource; */ @Configuration @EnableTransactionManagement -@PropertySource(value = {"classpath:juick.conf", "file:${user.home}/juick.conf"}) +@PropertySource(value = {"classpath:juick.conf"}) public class DataConfiguration implements TransactionManagementConfigurer { @Resource private Environment env; - @Bean - public static PlaceholderConfigurerSupport propertySourcesPlaceholderConfigurer() { - PlaceholderConfigurerSupport configurer = new PropertySourcesPlaceholderConfigurer(); - - configurer.setFileEncoding("utf-8"); - configurer.setOrder(0); - return configurer; - } - @Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); dataSource.setUrl(env.getProperty("datasource_url")); + dataSource.setUsername(env.getProperty("datasource_user", "")); + dataSource.setPassword(env.getProperty("datasource_password", "")); + + dataSource.setValidationQuery("select 1"); return dataSource; } @@ -56,7 +49,7 @@ public class DataConfiguration implements TransactionManagementConfigurer { } @Bean - public JdbcTemplate jdbc() { + public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } } diff --git a/juick-core/src/main/java/com/juick/util/ThreadHelper.java b/juick-core/src/main/java/com/juick/util/ThreadHelper.java new file mode 100644 index 00000000..7304d158 --- /dev/null +++ b/juick-core/src/main/java/com/juick/util/ThreadHelper.java @@ -0,0 +1,36 @@ +package com.juick.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Created by aalexeev on 11/11/16. + */ +public class ThreadHelper { + private ThreadHelper() { + throw new IllegalStateException(); + } + + private static Logger logger = LoggerFactory.getLogger(ThreadHelper.class); + + public static void shutdownAndAwaitTermination(ExecutorService pool) { + pool.shutdown(); // Disable new tasks from being submitted + try { + // Wait a while for existing tasks to terminate + if (!pool.awaitTermination(5, TimeUnit.SECONDS)) { + pool.shutdownNow(); // Cancel currently executing tasks + // Wait a while for tasks to respond to being cancelled + if (!pool.awaitTermination(5, TimeUnit.SECONDS)) + logger.error("Pool did not terminate"); + } + } catch (InterruptedException ie) { + // (Re-)Cancel if current thread also interrupted + pool.shutdownNow(); + // Preserve interrupt status + Thread.currentThread().interrupt(); + } + } +} -- cgit v1.2.3