AdapterStartConfig
@Slf4j
@Component
public class AdapterStartConfig implements ApplicationListener<ContextRefreshedEvent> {
@Resource
LiveTvAdapter liveTvAdapter;
private static final ThreadFactory POLLING_THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("adapter-live-polling-%d").build();
public static final ExecutorService ADAPTER_HANDLER_POOL = new ThreadPoolExecutor(1, 1, 0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(2048),
POLLING_THREAD_FACTORY,
new ThreadPoolExecutor.AbortPolicy());
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
ADAPTER_HANDLER_POOL.execute(new Thread(liveTvAdapter));
}
}
TvAdapter
- 线程任务的模板,通过其他自定义类继承改类 实现自定义任务
@Slf4j
@Component
public abstract class TvAdapter implements Runnable
{
public int restTime = 5;
public boolean hasTaskRun = false;
@Value("${adapter.live.ftp.batchFileSize}")
public int batchFileSize = 10;
public abstract void fetchBatchFile();
public abstract void handleBatchFile();
public static int num = 0;
@Override
public void run()
{
boolean normal = true;
while (true) {
try {
if (normal) {
log.info("线程执行次数: {}", num++);
long beginTime = System.currentTimeMillis();
fetchBatchFile();
handleBatchFile();
long endTime = System.currentTimeMillis();
log.info("当前线程总耗费时间(毫秒): {}", endTime - beginTime);
}
} catch (Exception e) {
log.error("数据源处理出错:{}",e.getMessage());
e.printStackTrace();
}
try {
if (!hasTaskRun) {
Thread.sleep(restTime * 1000);
}
normal = true;
} catch (Exception e) {
normal = false;
log.error(e.getMessage());
e.printStackTrace();
}
}
}
}
LiveTvAdapter
@Component
@Slf4j
public class LiveTvAdapter extends TvAdapter {
@Resource
private MediaCmsPreChannelManage mediaCmsPreChannelManage;
@Resource
private MediaCmsPreScheduleBaseManage mediaCmsPreScheduleBaseManage;
@Autowired
ICmsPreChannelService cmsPreChannelService;
public ConcurrentHashMap<String, List<CmsPreScheduleBase>> channelProgramInfo = new ConcurrentHashMap<String, List<CmsPreScheduleBase>>(64);
@Value("${adapter.live.ftp.host}")
private String host;
@Value("${adapter.live.ftp.port}")
private int port;
@Value("${adapter.live.ftp.username}")
private String userName;
@Value("${adapter.live.ftp.password}")
private String passWord;
@Value("${adapter.live.ftp.path}")
private String ftpPath;
@Value("${adapter.live.ftp.importPolicy}")
private String importPolicy = "0";
@Value("${adapter.live.ftp.localPath}")
public String localPath;
@Value("${adapter.live.ftp.fileCharset}")
public String fileCharset;
private static final String WORD_CHANNEL = "Channel:";
private static final String WORD_DATE = "Date:";
public ArrayList<String> needHandle = new ArrayList<>();
@Override
public void fetchBatchFile() {
try {
needHandle.clear();
channelProgramInfo.clear();
hasTaskRun = false;
FtpConfig ftpConfig = new FtpConfig();
ftpConfig.setFtpHost(host);
ftpConfig.setFtpPort(port);
ftpConfig.setFtpUserName(userName);
ftpConfig.setFtpPwd(passWord);
FtpUtil ftpUtil = new FtpUtil(ftpConfig);
try {
boolean isConnect = ftpUtil.connect();
if (!isConnect) {
log.info("ftp连接出现异常,请检查ftp服务!!!");
return;
}
} catch (Exception e) {
log.info("ftp连接出现异常,请检查ftp服务!!!");
log.info(e.getMessage());
}
ftpUtil.downloadAndDelOriginalFile(ftpPath, ".ok", localPath, needHandle, batchFileSize);
if (needHandle.size() > 0){
hasTaskRun = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void handleBatchFile() {
long beginTime = System.currentTimeMillis();
try {
if (needHandle.size() == 0) {
return;
}
for (String fileName : needHandle) {
if (!parseTxt(fileName)) {
continue;
}
}
Set<Map.Entry<String, List<CmsPreScheduleBase>>> entries = channelProgramInfo.entrySet();
for (Map.Entry<String, List<CmsPreScheduleBase>> entry : entries) {
String channelName = entry.getKey();
CmsPreChannel channel = mediaCmsPreChannelManage.queryChannelByName(channelName);
if (null == channel) {
log.info("{}频道不存在,请进行频道的入库", channelName);
continue;
}
List<CmsPreScheduleBase> list = entry.getValue();
Map<String, List<CmsPreScheduleBase>> map = list.stream().collect(Collectors.toMap(
CmsPreScheduleBase::getPlayDate,
a -> new ArrayList<>(Arrays.asList(a)),
(a1, a2) -> {
a1.addAll(a2);
return a1;
}));
for (Map.Entry<String, List<CmsPreScheduleBase>> programInfo : map.entrySet()) {
String date = programInfo.getKey();
List<CmsPreScheduleBase> programList = programInfo.getValue();
if ("0".equals(importPolicy)) {
mediaCmsPreScheduleBaseManage.injectionCoverUpdate(date, programList, channel);
} else {
mediaCmsPreScheduleBaseManage.injectionSplitUpdate(date, programList, channel);
}
}
}
} catch (Exception e) {
log.info("节目单入库失败!!!");
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
log.info("入库总耗费时间(毫秒): {}", endTime - beginTime);
}
private boolean isCrossSky(String start, String end) {
return NumberUtils.toInt(start) > NumberUtils.toInt(end);
}
private Date timeToDateTime(String time, String playDate) {
return DateUtil.getDate(playDate + time, SHORT_DATE_FORMAT_YYYY_MM_DD_HH_SS);
}
private void addScheduleToList(String playDate, String[] arr, List<CmsPreScheduleBase> list) {
CmsPreScheduleBase schedule = new CmsPreScheduleBase();
schedule.setPlayDate(playDate);
schedule.setPlayStartDate(timeToDateTime(arr[0], playDate));
schedule.setPlayEndDate(timeToDateTime(arr[1], playDate));
schedule.setScheduleName(arr[2]);
list.add(schedule);
}
private void setScheduleInfo(CmsPreScheduleBase scheduleBase, CmsPreChannel channel) {
String uuid = UUIDUtil.getUUID();
scheduleBase.setCreateTime(new Date());
scheduleBase.setChannelOutSourceId(channel.getOutSourceId());
scheduleBase.setOutSourceId(uuid);
scheduleBase.setInnerCode(uuid);
scheduleBase.setCreateUser(JwtUtil.getUserName());
scheduleBase.setCpCode(channel.getCpCode());
scheduleBase.setDuration(DateUtils.getDurationByMinutes(scheduleBase.getPlayStartDate(), scheduleBase.getPlayEndDate()));
}
public boolean parseTxt(String fileName) {
try {
long beginTime = System.currentTimeMillis();
String saveFilename = localPath + fileName;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(saveFilename), fileCharset));
String line = null;
String playDate = "";
String channelInfo = new String();
List<CmsPreScheduleBase> list = new ArrayList<>();
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) {
continue;
}
if (line.contains(WORD_CHANNEL)) {
channelInfo = EditScheduleUtil.toScheduleDate(line);
continue;
} else if (line.contains(WORD_DATE)) {
playDate = EditScheduleUtil.toScheduleDate(line);
} else {
String[] arr = EditScheduleUtil.toScheduleData(line);
if (this.isCrossSky(arr[0], arr[1])) {
this.addScheduleToList(playDate, new String[]{arr[0], "2359", arr[2]}, list);
this.addScheduleToList(DateUtil.getAfterDateByOne(playDate),
new String[]{"0000", arr[1], arr[2]}, list);
} else {
this.addScheduleToList(playDate, arr, list);
}
}
}
channelProgramInfo.put(channelInfo, list);
long endTime = System.currentTimeMillis();
log.info("文件解析总耗费时间(毫秒): {}", endTime - beginTime);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}