Demo entry 6346875

123

   

Submitted by anonymous on Feb 13, 2017 at 09:10
Language: Java. Code size: 4.2 kB.

package com.jd.sem.brain.business.campaign.impl;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.jd.sem.brain.business.campaign.GenPipeLineService;
import com.jd.sem.brain.business.campaign.impl.GenTaskImpl;
import com.jd.sem.brain.business.campaign.impl.GenTaskImpl.ExpandStage;
import com.jd.sem.brain.business.campaign.impl.GenTaskImpl.LandingType;
import com.jd.sem.brain.business.gen.GenService;
import com.jd.sem.brain.business.gen.impl.GenServiceImpl;
import com.jd.sem.brain.rpc.jss.impl.JssServiceImpl;

/***
 * 
 * @author chenlong
 *
 */
public class GenPipeLineServiceImpl implements GenPipeLineService,InitializingBean{

	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private List<GenTaskImpl> taskList;
	
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	private String localTmpWorkDir;
	
	@Resource
	private JssServiceImpl jssService;
	
	@Resource
	private GenService genService;
	
	public static class TaskInfo {
		/** 指定着陆方式 */
		private LandingType landingType;
		/** 指定着陆页 */
		private String landingPage;
		/** 指定活动页 */
		private String activityUrl;
		/** 指定拓词商品数量限制 */
		private int itemLimit;
		/** 指定拓词的新词得分底线 */
		private float scoreThreshold;
		
		public String getLandingPage() {
			return this.landingPage;
		}
		public String getActivityUrl() {
			return this.activityUrl;
		}
		public LandingType getLandingType() {
			return this.landingType;
		}
		public TaskInfo(String activityUrl, String landingPage, int nLimit, float scoreThreshold){
			this.activityUrl = activityUrl;
			this.setItemLimit(nLimit);
			this.setScoreThreshold(scoreThreshold);
			if (landingPage.equals("item"))
				this.landingType = GenTaskImpl.LandingType.Item;
			else if (landingPage.equals("search"))
				this.landingType = GenTaskImpl.LandingType.Search;
			else if (landingPage.equals("act"))
				this.landingType = GenTaskImpl.LandingType.Act;
			else
				this.landingType = GenTaskImpl.LandingType.Custom;
		}
		public int getItemLimit() {
			return itemLimit;
		}
		public void setItemLimit(int itemLimit) {
			this.itemLimit = itemLimit;
		}
		public float getScoreThreshold() {
			return scoreThreshold;
		}
		public void setScoreThreshold(float scoreThreshold) {
			this.scoreThreshold = scoreThreshold;
		}
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		taskList = new ArrayList<GenTaskImpl>();
		threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
		threadPoolTaskExecutor.setCorePoolSize(10);
		threadPoolTaskExecutor.setMaxPoolSize(100);
		threadPoolTaskExecutor.initialize();
		
		this.jssService.setOvertime(24 * 3600 * 3);
	}

	public void newTask(TaskInfo taskInfo, GenServiceImpl genService) throws URISyntaxException {
		GenTaskImpl genTask = new GenTaskImpl(genService, 
				new URI(taskInfo.getActivityUrl()), 
				taskInfo.getLandingType(), taskInfo.getItemLimit(), taskInfo.getScoreThreshold());
		genTask.setThreadPoolTaskExecutor(threadPoolTaskExecutor);
		genTask.setLocalTmpWorkDir(".");
		genTask.setJssService(this.jssService);
		taskList.add(0, genTask);
		threadPoolTaskExecutor.submit(genTask);
	}

	public List<GenTaskImpl> getTaskList(int pageSize, int pageOffset) {
		if (pageOffset < 0 || pageOffset >= taskList.size())
			return new ArrayList<GenTaskImpl>();
		int pageToIndex = Math.min(pageOffset + pageSize, taskList.size());
		return taskList.subList(pageOffset, pageToIndex);
	}
	
	public int getRunningTask() {
		int cnt = 0;
		for (GenTaskImpl genTask:this.taskList) {
			if (genTask.getStage() != ExpandStage.Finished)
				cnt++;
		}
		return cnt;
	}

	public String getLocalTmpWordDir() {
		return localTmpWorkDir;
	}

	public void setLocalTmpWordDir(String localTmpWordDir) {
		this.localTmpWorkDir = localTmpWordDir;
	}

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).