package com.yeshi.buwan.util;
|
|
import com.yeshi.buwan.domain.ResourceVideo;
|
import com.yeshi.buwan.domain.VideoInfo;
|
import com.yeshi.buwan.domain.VideoResource;
|
import com.yeshi.buwan.domain.video.InternetSearchVideo;
|
import com.yeshi.buwan.dto.mq.*;
|
import com.yeshi.buwan.funtv.entity.FunTVAlbum2;
|
import com.yeshi.buwan.iqiyi.entity.IqiyiAlbum2;
|
import com.yeshi.buwan.pptv.entity.PPTVSeries;
|
import com.yeshi.buwan.service.imp.JobThreadExecutorServiceImpl;
|
import com.yeshi.buwan.service.imp.ResourceVideoService;
|
import com.yeshi.buwan.service.imp.VideoInfoService;
|
import com.yeshi.buwan.service.inter.juhe.FunTV2Service;
|
import com.yeshi.buwan.service.inter.juhe.Iqiyi2Service;
|
import com.yeshi.buwan.service.inter.juhe.PPTVService;
|
import com.yeshi.buwan.service.inter.juhe.YouKuService;
|
import com.yeshi.buwan.service.manager.SolrAlbumVideoDataManager;
|
import com.yeshi.buwan.service.manager.SolrInternetSearchVideoDataManager;
|
import com.yeshi.buwan.util.factory.InternetSearchVideoFactory;
|
import com.yeshi.buwan.util.mq.CMQManager;
|
import com.yeshi.buwan.youku.entity.YouKuShowDetail;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.event.ContextRefreshedEvent;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 系统初始化
|
*
|
* @author Administrator
|
*/
|
@Component
|
public class SpringContext implements ApplicationListener<ContextRefreshedEvent> {
|
|
@Resource
|
private VideoInfoService videoInfoService;
|
|
@Resource
|
private SolrAlbumVideoDataManager solrDataManager;
|
|
@Resource
|
private Iqiyi2Service iqiyi2Service;
|
|
@Resource
|
private FunTV2Service funTV2Service;
|
|
@Resource
|
private ResourceVideoService resourceVideoService;
|
|
@Resource
|
private PPTVService pptvService;
|
|
private static boolean isInited = false;
|
|
private final static Logger logger = LoggerFactory.getLogger(SpringContext.class);
|
|
public void onApplicationEvent(ContextRefreshedEvent arg0) {
|
if (arg0.getApplicationContext().getParent() != null) {
|
System.out.println(System.currentTimeMillis());
|
onApplication(arg0);
|
}
|
}
|
|
private synchronized void onApplication(ContextRefreshedEvent context) {
|
if (!isInited) {
|
isInited = true;
|
System.out.println("系统初始化成功");
|
init();
|
}
|
}
|
|
private void init() {
|
logger.error("初始化");
|
if (!Constant.JobTasker) {
|
doSolrJob();
|
doAddIqiyi2Video();
|
doAddFunTV2Video();
|
doAddPPTVVideo();
|
doDeleteVideoResource();
|
doUpdateVideoExtraInfo();
|
}
|
}
|
|
private void doSolrJob() {
|
for (int i = 0; i < 5; i++) {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
logger.info("doSolrJob");
|
List<SolrVideoMQMsg> solrMsgList = CMQManager.getInstance().consumeSolrMsg(16);
|
if (solrMsgList != null)
|
for (SolrVideoMQMsg solrVideo : solrMsgList) {
|
try {
|
VideoInfo videoInfo = videoInfoService.getVideoInfo(solrVideo.getId());
|
if (videoInfo != null) {
|
if ("1".equalsIgnoreCase(videoInfo.getShow())) {
|
List<VideoResource> resourceList = new ArrayList<>();
|
List<ResourceVideo> rvList = resourceVideoService.getResourceList(videoInfo.getId());
|
if (rvList != null)
|
for (ResourceVideo rv : rvList)
|
resourceList.add(rv.getResource());
|
videoInfo.setResourceList(resourceList);
|
solrDataManager.saveOrUpdate(videoInfo);
|
} else
|
solrDataManager.deleteById(videoInfo.getId());
|
} else {//视频已经删除
|
solrDataManager.deleteById(solrVideo.getId());
|
}
|
CMQManager.getInstance().deleteSolrMsg(solrVideo.getHandler());
|
} catch (Exception e) {
|
logger.error("添加到搜索引擎出错", e);
|
}
|
}
|
}
|
});
|
}
|
}
|
|
|
@Resource
|
private YouKuService youKuService;
|
|
@Resource
|
private SolrInternetSearchVideoDataManager solrInternetSearchVideoDataManager;
|
|
private void doInternetSearchVideoUpdateJob() {
|
for (int i = 0; i < 1; i++) {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
List<InternetSearchVideoMQMsg> solrMsgList = CMQManager.getInstance().consumeInternetSearchVideoUpdateMsg(16);
|
if (solrMsgList != null)
|
for (InternetSearchVideoMQMsg solrVideo : solrMsgList) {
|
try {
|
|
switch (solrVideo.getResourceId()) {
|
|
//优酷
|
case 20:
|
YouKuShowDetail detail = youKuService.getShowDetail(solrVideo.getId());
|
if (detail == null) {//被删除
|
solrInternetSearchVideoDataManager.deleteById(detail.getId());
|
} else {//更新
|
InternetSearchVideo video = InternetSearchVideoFactory.create(detail);
|
solrInternetSearchVideoDataManager.saveOrUpdate(video);
|
}
|
break;
|
//腾讯
|
case 17:
|
|
|
|
break;
|
|
}
|
|
|
CMQManager.getInstance().deleteSolrMsg(solrVideo.getHandler());
|
} catch (Exception e) {
|
logger.error("更新全网搜搜索引擎出错", e);
|
}
|
}
|
}
|
});
|
}
|
}
|
|
|
private void doAddIqiyi2Video() {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
logger.info("doAddIqiyi2Video");
|
List<IqiyiAlbum2MQMsg> iqiyiAlbumMsgList = CMQManager.getInstance().consumeIqiyiAlbumUpdateMsg(16);
|
if (iqiyiAlbumMsgList != null)
|
for (IqiyiAlbum2MQMsg iqiyiAlbum2MQMsg : iqiyiAlbumMsgList) {
|
try {
|
Long qikuID = iqiyiAlbum2MQMsg.getId();
|
IqiyiAlbum2 album2 = iqiyi2Service.selectAlbumById(qikuID);
|
logger.info("爱奇艺专辑:" + album2.getName());
|
if (album2 != null) {
|
iqiyi2Service.addToVideoInfo(album2);
|
}
|
CMQManager.getInstance().deleteIqiyiAlbumUpdateMsg(iqiyiAlbum2MQMsg.getHandler());
|
} catch (Exception e) {
|
logger.error("爱奇艺专辑添加到视频出错:" + e.getMessage());
|
logger.error("ID:" + iqiyiAlbum2MQMsg.getId());
|
}
|
}
|
}
|
});
|
|
}
|
|
|
private void doAddFunTV2Video() {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
List<FunTVAlbum2MQMsg> funTVAlbum2MsgList = CMQManager.getInstance().consumeFunTVAlbumUpdateMsg(16);
|
if (funTVAlbum2MsgList != null)
|
for (FunTVAlbum2MQMsg funTVAlbum2MQMsg : funTVAlbum2MsgList) {
|
try {
|
String mediaId = funTVAlbum2MQMsg.getId();
|
FunTVAlbum2 album2 = funTV2Service.getAlbumDetail(mediaId);
|
if (album2 != null) {
|
funTV2Service.processAlbum(album2);
|
}
|
CMQManager.getInstance().deleteFunTVAlbumUpdateMsg(funTVAlbum2MQMsg.getHandler());
|
} catch (Exception e) {
|
logger.error("风行专辑添加到视频出错:" + e.getMessage());
|
logger.error("ID:" + funTVAlbum2MQMsg.getId());
|
}
|
}
|
}
|
});
|
|
}
|
|
|
private void doAddPPTVVideo() {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
List<PPTVMQMsg> pptvMsgList = CMQManager.getInstance().consumePPTVSeriesUpdateMsg(16);
|
if (pptvMsgList != null)
|
for (PPTVMQMsg pptvmqMsg : pptvMsgList) {
|
try {
|
switch (pptvmqMsg.getType()) {
|
case PPTVMQMsg.TYPE_ADD_OR_UPDATE:
|
PPTVSeries pptvSeries = pptvService.getSeriesDetail(pptvmqMsg.getInfoId());
|
if (pptvSeries != null) {
|
pptvService.addToVideoInfo(pptvSeries);
|
}
|
break;
|
|
case PPTVMQMsg.TYPE_DELETE:
|
pptvService.offLineSeries(pptvmqMsg.getInfoId());
|
break;
|
}
|
CMQManager.getInstance().deletePPTVSeriesUpdateMsg(pptvmqMsg.getHandler());
|
} catch (Exception e) {
|
e.printStackTrace();
|
logger.error("PPTV添加到视频出错:" + e.getMessage());
|
logger.error("infoId:" + pptvmqMsg.getInfoId());
|
}
|
}
|
}
|
});
|
|
}
|
|
private void doDeleteVideoResource() {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
List<CMQResult> cmqMsgList = CMQManager.getInstance().consumeVideoResourceDeleteMsg(16);
|
if (cmqMsgList != null)
|
for (CMQResult msg : cmqMsgList) {
|
try {
|
String videoId = msg.getData() + "";
|
//查询资源列表
|
List<ResourceVideo> resourceVideoList = resourceVideoService.getResourceList(videoId);
|
//隐藏视频
|
if (resourceVideoList == null || resourceVideoList.size() == 0)
|
videoInfoService.hiddenVideo(videoId);
|
//更新搜索引擎
|
CMQManager.getInstance().addSolrMsg(videoId);
|
CMQManager.getInstance().deleteVideoResourceDeleteMsg(msg.getHandler());
|
} catch (Exception e) {
|
logger.error("视频资源删除处理出错:" + e.getMessage());
|
logger.error("ID:" + msg.getData());
|
}
|
}
|
}
|
});
|
|
}
|
|
|
private void doUpdateVideoExtraInfo() {
|
new JobThreadExecutorServiceImpl().run(new Runnable() {
|
@Override
|
public void run() {
|
List<CMQResult> cmqMsgList = CMQManager.getInstance().consumeUpdateVideoExtraInfoMsg(16);
|
if (cmqMsgList != null)
|
for (CMQResult msg : cmqMsgList) {
|
try {
|
VideoExtraInfoChangeMQMsg videoExtraInfoChangeMQMsg = (VideoExtraInfoChangeMQMsg) msg.getData();
|
|
if (videoExtraInfoChangeMQMsg != null) {
|
switch (videoExtraInfoChangeMQMsg.getType()) {
|
case VideoExtraInfoChangeMQMsg.TYPE_RESOURCE:
|
if (VideoExtraInfoChangeMQMsg.ACTION_DELETE.equalsIgnoreCase(videoExtraInfoChangeMQMsg.getAction())) {//删除视频源
|
CMQManager.getInstance().addVideoResourceDeleteMsg(videoExtraInfoChangeMQMsg.getVideoId());
|
}
|
break;
|
case VideoExtraInfoChangeMQMsg.TYPE_CATEGORY:
|
break;
|
}
|
|
videoInfoService.statisticVideoExtraInfo(videoExtraInfoChangeMQMsg.getVideoId());
|
}
|
CMQManager.getInstance().deleteUpdateVideoExtraInfoMsg(msg.getHandler());
|
} catch (Exception e) {
|
logger.error("视频资源删除处理出错:" + e.getMessage());
|
logger.error("ID:" + msg.getData());
|
}
|
}
|
}
|
});
|
|
}
|
|
|
}
|