首页 > 代码库 > 关于做一些随机文章评论的纪录(假数据)
关于做一些随机文章评论的纪录(假数据)
事情是这样的,近期我们有这么个需求,因为网站访问量一般,所以他们要求发文章之后,要自动随机时间(5分钟内)、随机用户、给文章评论,点赞、增高阅读数,来显得有人气。
我们的想法是,让运营给我们一些准备好的用户头像、昵称,然后我们用来生成账号,再给我们准备一份随机评论的内容列表。之后我们写一个工具,在发文章后,5分钟内,随机创建5-20个点赞和评论,随机取用户和评论内容。
具体实现肯定每个项目都不一样,只是思路可以参考一下。
首先要生成用户 当然了,也可以直接写SQL 但是好像有点麻烦
/** * Controller - 生成用户 本地 * * @author fr Team * @version 3.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; }
关于做一些随机文章评论的纪录(假数据)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。