2022-07-06 18:32:05 +08:00
|
|
|
package org.nl.start;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.nl.modules.mnt.util.DataTypeEnum;
|
|
|
|
|
import org.nl.modules.quartz.domain.QuartzJob;
|
|
|
|
|
import org.nl.modules.quartz.repository.QuartzJobRepository;
|
|
|
|
|
import org.nl.modules.quartz.utils.QuartzManage;
|
|
|
|
|
import org.nl.modules.system.service.impl.ParamServiceImpl;
|
|
|
|
|
import org.nl.wql.WQLCore;
|
2022-09-23 17:11:09 +08:00
|
|
|
import org.nl.wql.util.SpringContextHolder;
|
2022-07-06 18:32:05 +08:00
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.BeanFactory;
|
|
|
|
|
import org.springframework.beans.factory.BeanFactoryAware;
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
|
|
import org.springframework.boot.ApplicationArguments;
|
|
|
|
|
import org.springframework.boot.ApplicationRunner;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 随项目启动模块
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class Init implements ApplicationRunner, BeanFactoryAware, ApplicationContextAware {
|
|
|
|
|
private final QuartzJobRepository quartzJobRepository;
|
|
|
|
|
private final QuartzManage quartzManage;
|
|
|
|
|
|
|
|
|
|
private final DefaultListableBeanFactory beanFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void init() throws Exception {
|
|
|
|
|
//初始化WQL
|
|
|
|
|
initWql();
|
|
|
|
|
//初始化任务调度
|
|
|
|
|
initQuartz();
|
|
|
|
|
//用户岗位表【sys_users_roles】
|
|
|
|
|
System.out.println("项目启动成功!");
|
|
|
|
|
}
|
|
|
|
|
private void initOracle(){
|
|
|
|
|
String jdbcUrl = "jdbc:oracle:thin:@10.16.1.30:1521:xrrun";
|
|
|
|
|
String userName = "erp";
|
|
|
|
|
String password = "erp";
|
|
|
|
|
|
|
|
|
|
DruidDataSource druidDataSource = new DruidDataSource();
|
|
|
|
|
String className;
|
|
|
|
|
try {
|
|
|
|
|
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
if (StrUtil.isEmpty(className)) {
|
|
|
|
|
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
|
|
|
|
if (null == dataTypeEnum) {
|
|
|
|
|
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
druidDataSource.setDriverClassName(dataTypeEnum.getDriver());
|
|
|
|
|
} else {
|
|
|
|
|
druidDataSource.setDriverClassName(className);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
druidDataSource.setUrl(jdbcUrl);
|
|
|
|
|
druidDataSource.setUsername(userName);
|
|
|
|
|
druidDataSource.setPassword(password);
|
|
|
|
|
// 配置获取连接等待超时的时间
|
|
|
|
|
druidDataSource.setMaxWait(3000);
|
|
|
|
|
// 配置初始化大小、最小、最大
|
|
|
|
|
druidDataSource.setInitialSize(5);
|
|
|
|
|
druidDataSource.setMinIdle(5);
|
|
|
|
|
druidDataSource.setMaxActive(10);
|
|
|
|
|
|
|
|
|
|
// 如果链接出现异常则直接判定为失败而不是一直重试
|
|
|
|
|
druidDataSource.setBreakAfterAcquireFailure(true);
|
|
|
|
|
try {
|
|
|
|
|
druidDataSource.init();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.error("Exception during pool initialization", e);
|
|
|
|
|
throw new RuntimeException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
System.out.println("oracle连接成功");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initDataSource1() {
|
|
|
|
|
String IS_CONNECT_ERP = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("IS_CON_ERP_ORACLE").getValue();
|
|
|
|
|
if (!StrUtil.equals("1", IS_CONNECT_ERP)) return;
|
|
|
|
|
String jdbcUrl = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_URL").getValue();
|
|
|
|
|
String userName = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_USER").getValue();
|
|
|
|
|
String password = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_PWD").getValue();
|
|
|
|
|
|
|
|
|
|
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DruidDataSource.class);
|
|
|
|
|
String className;
|
|
|
|
|
try {
|
|
|
|
|
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
if (StrUtil.isEmpty(className)) {
|
|
|
|
|
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
|
|
|
|
if (null == dataTypeEnum) {
|
|
|
|
|
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
builder.addPropertyValue("driverClassName", dataTypeEnum.getDriver());
|
|
|
|
|
} else {
|
|
|
|
|
builder.addPropertyValue("driverClassName", className);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.addPropertyValue("url", jdbcUrl);
|
|
|
|
|
builder.addPropertyValue("username", userName);
|
|
|
|
|
builder.addPropertyValue("password", password);
|
|
|
|
|
// 配置获取连接等待超时的时间
|
|
|
|
|
builder.addPropertyValue("maxWait", "3000");
|
|
|
|
|
// 配置初始化大小、最小、最大
|
|
|
|
|
builder.addPropertyValue("initialSize", "5");
|
|
|
|
|
builder.addPropertyValue("minIdle", "5");
|
|
|
|
|
builder.addPropertyValue("maxActive", "10");
|
|
|
|
|
// 如果链接出现异常则直接判定为失败而不是一直重试
|
|
|
|
|
builder.addPropertyValue("breakAfterAcquireFailure", "true");
|
|
|
|
|
|
|
|
|
|
beanFactory.registerBeanDefinition("dataSource1", builder.getBeanDefinition());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void initDataSource2() {
|
|
|
|
|
String IS_CONNECT_ERP = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("IS_CON_ERP_SQLSERVER").getValue();
|
|
|
|
|
if (!StrUtil.equals("1", IS_CONNECT_ERP)) return;
|
|
|
|
|
String jdbcUrl = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_URL").getValue();
|
|
|
|
|
String userName = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_USER").getValue();
|
|
|
|
|
String password = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_PWD").getValue();
|
|
|
|
|
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DruidDataSource.class);
|
|
|
|
|
|
|
|
|
|
String className;
|
|
|
|
|
try {
|
|
|
|
|
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
if (StrUtil.isEmpty(className)) {
|
|
|
|
|
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
|
|
|
|
if (null == dataTypeEnum) {
|
|
|
|
|
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
|
|
|
|
}
|
|
|
|
|
builder.addPropertyValue("driverClassName", dataTypeEnum.getDriver());
|
|
|
|
|
} else {
|
|
|
|
|
builder.addPropertyValue("driverClassName", className);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.addPropertyValue("url", jdbcUrl);
|
|
|
|
|
builder.addPropertyValue("username", userName);
|
|
|
|
|
builder.addPropertyValue("password", password);
|
|
|
|
|
// 配置获取连接等待超时的时间
|
|
|
|
|
builder.addPropertyValue("maxWait", "3000");
|
|
|
|
|
// 配置初始化大小、最小、最大
|
|
|
|
|
builder.addPropertyValue("initialSize", "5");
|
|
|
|
|
builder.addPropertyValue("minIdle", "5");
|
|
|
|
|
builder.addPropertyValue("maxActive", "10");
|
|
|
|
|
// 如果链接出现异常则直接判定为失败而不是一直重试
|
|
|
|
|
builder.addPropertyValue("breakAfterAcquireFailure", "true");
|
|
|
|
|
beanFactory.registerBeanDefinition("dataSource2", builder.getBeanDefinition());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initQuartz() {
|
|
|
|
|
log.info("--------------------注入定时任务---------------------");
|
|
|
|
|
List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
|
|
|
|
|
quartzJobs.forEach(quartzManage::addJob);
|
|
|
|
|
log.info("--------------------定时任务注入完成---------------------");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initWql() throws Exception {
|
|
|
|
|
WQLCore.ROOT = "org.nl";
|
|
|
|
|
WQLCore.init();
|
|
|
|
|
log.info("WQL初始化成功!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run(ApplicationArguments args) throws Exception {
|
|
|
|
|
this.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setBeanFactory(BeanFactory beanFactory1) throws BeansException {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|