多线程启动多个视频流
package org.jeecg.modules.xjgl.service;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.GetBeanUtil;
@Slf4j
public class ExecuteTSCommandThread implements Runnable {
IYsXjglService ysXjglService = (IYsXjglService) GetBeanUtil.getBean(IYsXjglService.class);
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public void run() {
log.info("主键ID=={}", id);
boolean bln = ysXjglService.executeCommand(id);
log.info("多线程==>" + Thread.currentThread().getName() + "执行" + bln);
}
}
启动多线程
@Override
public void startXj() {
QueryWrapper<YsXjgl> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("xj_ffmpeg", 1).eq("del_flag", 0);
List<YsXjgl> xjglList = ysXjglMapper.selectList(queryWrapper);
for (YsXjgl xjgl : xjglList) {
ExecuteTSCommandThread tsCommandThread = new ExecuteTSCommandThread();
tsCommandThread.setId(xjgl.getId());
new Thread(tsCommandThread).start();
}
}
多线程时Autowired自动注入问题
package org.jeecg.common.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GetBeanUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
GetBeanUtil.applicationContext = applicationContext;
}
public static Object getBeanByName(String beanName) {
if (applicationContext == null) {
return null;
}
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
}