關於做一些隨機文章評論的紀錄(假數據)
阿新 • • 發佈:2017-07-11
頭像 閱讀 文件 pat nbc 列表 lin for 文件內容
事情是這樣的,近期我們有這麽個需求,因為網站訪問量一般,所以他們要求發文章之後,要自動隨機時間(5分鐘內)、隨機用戶、給文章評論,點贊、增高閱讀數,來顯得有人氣。
我們的想法是,讓運營給我們一些準備好的用戶頭像、昵稱,然後我們用來生成賬號,再給我們準備一份隨機評論的內容列表。之後我們寫一個工具,在發文章後,5分鐘內,隨機創建5-20個點贊和評論,隨機取用戶和評論內容。
具體實現肯定每個項目都不一樣,只是思路可以參考一下。
首先要生成用戶 當然了,也可以直接寫SQL 但是好像有點麻煩
/** * Controller - 生成用戶 本地 * * @author fr Team * @version3.0 */ @Controller("createMemberController") @RequestMapping("/createMember") public class CreateMemberController extends BaseController { private static String PATH = "D:\\worknbc2\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\website-server\\resources\\shop\\commentList\\username.txt"; @Resource(name= "memberServiceImpl") private MemberService memberService; /** 會員等級服務 */ @Resource(name = "memberRankServiceImpl") private MemberRankService memberRankService; /** * 列表 */ @RequestMapping(value = "/create", method = RequestMethod.GET) public void create() {
//讀取txt裏的隨機昵稱 String[] usernames= ReadFileUtil.readTxtFile(PATH); for (int i = 0; i < usernames.length - 1; i++) { String image = "http://www.nbcyl.com/resources/shop/headimg/"+i+".jpg"; Member member = new Member(); member.setUsername("testMember"+i); member.setPassword(DigestUtils.md5Hex("123456")); member.setAccountType(Member.AccountType.normal); member.setAddress("北京市朝陽區"); member.setPicture(image); member.setPetName(usernames[i]); member.setGender(Member.Gender.male); member.setEmail("member_"+i+"@nbcyl.com"); member.setPlatformType(PlatformType.normal); member.setAmount(new BigDecimal(0)); member.setBalance(new BigDecimal(0)); member.setIsEnabled(true); member.setIsLocked(false); member.setLoginFailureCount(0); member.setLockedDate(null); member.setRegisterIp("127.0.0.1"); member.setLoginIp("127.0.0.1"); member.setLoginDate(new Date()); member.setSafeKey(null); member.setFavoriteProducts(null); member.setPoint(0L); member.setMemberRank(memberRankService.findDefault()); memberService.save(member); } } }
有了這些用戶後,就可以開始啦。
/** * CommentTimerUtil - 評論時間觸發器 * * @author fr Team * @version 3.0 */ @Service("commentTimerUtil") public class CommentTimerUtil { private static Log log = LogFactory.getLog(CommentTimerUtil.class); private static Integer MIN_DATA_TIME = 5; private static Integer MAX_DATA_TIME = 20; //這是你生成用戶後他們的ID 起始ID和結束ID private static Integer MEMBER_START_ID = 142; private static Integer MEMBER_END_ID = 241; private static Integer MIN_TIME = 1000; private static Integer MAX_TIME = 1000 *60 *5; private static String THREAD_NAME = "commentAndPraiseTask"; public static void main(String[] args) throws IOException { } private static String PATH = "/resources/shop/commentList/comment.txt"; //txt裏放著是隨機的評論內容
//具體評論的那個方法 文章 圖片 視頻 public static void commentAndPraise(String type, Long dataId, int i) { MemberArticleService memberArticleService = SpringUtils.getBean("memberArticleServiceImpl", MemberArticleService.class); MemberService memberService = SpringUtils.getBean("memberServiceImpl", MemberService.class); MemberCommentService memberCommentService = SpringUtils.getBean("memberCommentServiceImpl", MemberCommentService.class); MemberPhotoService memberPhotoService = SpringUtils.getBean("memberPhotoServiceImpl", MemberPhotoService.class); MemberVideoService memberVideoService = SpringUtils.getBean("memberVideoServiceImpl", MemberVideoService.class); Long id = dataId; String commentType = type; //隨機一個評論 String[] sourceStrArray = ReadFileUtil.readTxtFile(StringUtils.sourFolder + PATH); int commentNum = CommentTimerUtil.random(0, sourceStrArray.length); String comment = sourceStrArray[commentNum]; //隨機一個用戶id 外網 4710-4809 內網 142-241 int memberNum = CommentTimerUtil.random(MEMBER_START_ID, MEMBER_END_ID); if ("article".equals(commentType)) { MemberArticle article = memberArticleService.find(id); Member member = memberService.find((long)memberNum); MemberComment memberComment = new MemberComment(); memberComment.setMember(member); memberComment.setMemberArticle(article); memberComment.setContent(comment); memberComment.setFlag(false); memberCommentService.save(memberComment); article.setCommentNum(article.getCommentNum() == null ? 1 : article.getCommentNum()+1); memberArticleService.update(article); }else if ("photo".equals(commentType)) { MemberPhoto photo = memberPhotoService.find(id); Member member = memberService.find((long)memberNum); MemberComment memberComment = new MemberComment(); memberComment.setMember(member); memberComment.setMemberPhoto(photo); memberComment.setContent(comment); memberComment.setFlag(false); memberCommentService.save(memberComment); photo.setCommentNum(photo.getCommentNum() == null ? 1 : photo.getCommentNum()+1); memberPhotoService.update(photo); }else if ("video".equals(commentType)) { MemberVideo video = memberVideoService.find(id); Member member = memberService.find((long)memberNum); MemberComment memberComment = new MemberComment(); memberComment.setMember(member); memberComment.setMemberVideo(video); memberComment.setContent(comment); memberComment.setFlag(false); memberCommentService.save(memberComment); } log.info("隨機評論和點贊成功!"); } /** * 隨機評論和點贊 * @param dataId * 對象id */ public static void commentAndPraiseTask(Long dataId, String type) { String threadName = THREAD_NAME; Thread current = findThread(threadName); CommentAndPraiseThread cpt = (CommentAndPraiseThread) findThread(threadName); if (current == null) { cpt = new CommentAndPraiseThread(); cpt.setName(threadName); // cpt.setDataId(dataId); // cpt.setType(type); cpt.setTimer(new Timer()); cpt.setBeginTimeMillis(System.currentTimeMillis()); cpt.setLastTimeMillis(MAX_TIME + 1000); setCommentJobs(dataId, type, cpt.getTimer()/*, cpt.getTaskList()*/); cpt.start(); } else { long lastTimeMillis = cpt.getLastTimeMillis() + (MAX_TIME + 1000); cpt.setLastTimeMillis(lastTimeMillis); // cpt.setDataId(dataId); // cpt.setType(type); cpt.setBeginTimeMillis(System.currentTimeMillis()); if (cpt.getTimer() == null) { cpt.setTimer(new Timer()); } setCommentJobs(dataId, type, cpt.getTimer()/*, cpt.getTaskList()*/); } } /** * 通過線程組獲得線程 * * @param name * @return */ public static Thread findThread(String name) { ThreadGroup group = Thread.currentThread().getThreadGroup(); while(group != null) { Thread[] threads = new Thread[(int)(group.activeCount() * 1.2)]; int count = group.enumerate(threads, true); for(int i = 0; i < count; i++) { if(name.equals(threads[i].getName())) { return threads[i]; } } group = group.getParent(); } return null; } public static int random(int min, int max) { Random random = new Random(); return random.nextInt(max)%(max-min+1) + min; } /** * 設置隨機評論 * @param dataId * @param type * @param timer * @param taskList */ private static void setCommentJobs(Long dataId, String type, Timer timer/*, List<CommentJob> taskList*/) { // List<CommentJob> taskList = new ArrayList<CommentJob>(); int times = random(MIN_DATA_TIME, MAX_DATA_TIME); int minTime = MIN_TIME; int maxTime = MAX_TIME; log.info("隨機評論啟動次數:"+times); for (int i = 0; i < times; i++) { CommentJob timerJob = new CommentJob(); int randomTime = random(minTime, maxTime); log.info("隨機評論啟動時間:"+(randomTime/1000)+"秒"); timerJob.setI(i); timerJob.setType(type); timerJob.setDataId(dataId); timer.schedule(timerJob, randomTime); // taskList.add(timerJob); } log.info("已經添加任務調度表"); // lastTimeMillis += (MAX_TIME + 1000); } private static class CommentAndPraiseThread extends Thread{ // String type =""; // Long dataId = null; Timer timer = null; long beginTimeMillis = 0L; long lastTimeMillis = 0L; // List<CommentJob> taskList = new ArrayList<CommentJob>();; public void run(){ log.info("隨機評論定時器已啟動"); while(true){//這個是用來停止此任務的,否則就一直循環執行此任務 //時間過去5分鐘,直接退出循環! if (System.currentTimeMillis() >= (beginTimeMillis + lastTimeMillis)) { timer.cancel();//使用這個方法退出任務 break; } // boolean isrunning = false; // for (CommentJob commentTask : taskList) { // if (commentTask.getIsRunning()) { // isrunning = true; // } // } // if(!isrunning){ // timer.cancel();//使用這個方法退出任務 // taskList.clear(); // break; // } } log.info("隨機評論定時器任務結束..."); } // public String getType() { // return type; // } // // public void setType(String type) { // this.type = type; // } // // public Long getDataId() { // return dataId; // } // // public void setDataId(Long dataId) { // this.dataId = dataId; // } public Timer getTimer() { return timer; } public void setTimer(Timer timer) { this.timer = timer; } public long getBeginTimeMillis() { return beginTimeMillis; } public void setBeginTimeMillis(long beginTimeMillis) { this.beginTimeMillis = beginTimeMillis; } // public List<CommentJob> getTaskList() { // return taskList; // } // // public void setTaskList(List<CommentJob> taskList) { // this.taskList = taskList; // } public long getLastTimeMillis() { return lastTimeMillis; } public void setLastTimeMillis(long lastTimeMillis) { this.lastTimeMillis = lastTimeMillis; } } private static class CommentJob extends java.util.TimerTask{ Log log = LogFactory.getLog(CommentJob.class); //評論類型 文章/圖片/視頻 String type = ""; Boolean isRunning = true; //當前次數 int i = 0; //評論文章/圖片/視頻 的ID Long dataId; @Override public void run() { // isRunning = true; /* * 業務 */ // CommentTimerUtil commentTimerUtil = new CommentTimerUtil(); CommentTimerUtil.commentAndPraise(type, dataId,i); log.info(new Date() + " ==== end"); // isRunning = false; } // public Boolean getIsRunning() { // return isRunning; // } // // public void setIsRunning(Boolean isRunning) { // this.isRunning = isRunning; // } public Long getDataId() { return dataId; } public void setDataId(Long dataId) { this.dataId = dataId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getI() { return i; } public void setI(int i) { this.i = i; } } }
還有工具類。
/** * 讀取txt文件 * @param filePath * txt文件路徑 */ public static String[] readTxtFile(String filePath){ String lineTxt = ""; List<String> list = new ArrayList<String>(); try { String encoding="UTF-8"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判斷文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); while((lineTxt = bufferedReader.readLine()) != null){ list.add(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } int size = list.size(); String[] arr = (String[])list.toArray(new String[size]);//使用了第二種接口,返回值和參數均為結果 return arr; }
關於做一些隨機文章評論的紀錄(假數據)