创建类ApplicationBeanFactory
package com.xxx.comm.ApplicationBeanFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationBeanFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (ApplicationBeanFactory.applicationContext == null) {
ApplicationBeanFactory.applicationContext = applicationContext;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
}
配置在启动类中配置@Import(ApplicationBeanFactory.class)
import com.xxx.comm.ApplicationBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@Import(ApplicationBeanFactory.class)
@EnableTransactionManagement
public class DatafactoryHiveServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DatafactoryHiveServiceApplication.class, args);
}
}
在线程中调用service类来访问数据库
package com.xxx.util;
import com.xxx.comm.ApplicationBeanFactory;
import com.xxx.model.bo.HiveLogBO;
import com.xxx.service.HiveService;
import org.apache.hive.jdbc.HivePreparedStatement;
import java.sql.PreparedStatement;
import java.util.List;
public class LogRunnable implements Runnable {
private final HivePreparedStatement hvePreparedStatement;
private final HiveService hiveService;
private final StringBuffer stringBuffer;
private String hiveLogId;
public LogRunnable(PreparedStatement preparedStatement, String hiveLogId) {
this.hiveService = (HiveService) ApplicationBeanFactory.getBean("hiveService");
this.hvePreparedStatement = (HivePreparedStatement) preparedStatement;
this.stringBuffer = new StringBuffer();
this.hiveLogId = hiveLogId;
}
@Override
public void run() {
try {
HiveLogBO bo = new HiveLogBO();
bo.setId(Long.valueOf(hiveLogId));
while (hvePreparedStatement.hasMoreLogs()) {
updateQueryLog(bo);
Thread.sleep(3000L);
}
} catch (InterruptedException e) {
e.getStackTrace();
}
}
private void updateQueryLog(HiveLogBO bo) {
try {
List<String> queryLogs = hvePreparedStatement.getQueryLog();
for (String queryLog : queryLogs) {
System.out.println(queryLog);
if (queryLog.contains("INFO : The url to track the job:")) {
String applicationId = queryLog.substring(queryLog.indexOf("INFO : The url to track the job:") + 34);
bo.setApplicationId(applicationId);
hiveService.updateHiveLogApplicationId(bo);
}
stringBuffer.append(queryLog);
stringBuffer.append("\n");
}
bo.setContent(stringBuffer.toString());
hiveService.updateHiveLog(bo);
} catch (Exception e) {
e.printStackTrace();
}
}
}