From c75416a2ebcd0159eb6beb244f75ab3bdc6678ce Mon Sep 17 00:00:00 2001 From: tamguo Date: Wed, 10 Oct 2018 11:40:18 +0800 Subject: [PATCH] fix bug --- .../tiku/model/queue/LearnChapterQueue.java | 27 +++++++++++++++++++ .../com/tamguo/web/tiku/IndexController.java | 7 +++++ .../tamguo/web/tiku/QuestionContrller.java | 11 +++++++- .../src/main/resources/templates/index.html | 4 +-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tamguo-modules-core/src/main/java/com/tamguo/modules/tiku/model/queue/LearnChapterQueue.java diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/tiku/model/queue/LearnChapterQueue.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/tiku/model/queue/LearnChapterQueue.java new file mode 100644 index 0000000..df8b6c2 --- /dev/null +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/tiku/model/queue/LearnChapterQueue.java @@ -0,0 +1,27 @@ +package com.tamguo.modules.tiku.model.queue; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +import com.tamguo.modules.tiku.model.ChapterEntity; + +public class LearnChapterQueue { + + // 章节 + private static BlockingQueue chapterQueue = new ArrayBlockingQueue(5); + + public static void add(ChapterEntity chapter) throws InterruptedException { + if(chapterQueue.size() == 5) { + chapterQueue.take(); + } + chapterQueue.put(chapter); + } + + public static BlockingQueue getChapterQueue() { + return chapterQueue; + } + + public static void setChapterQueue(BlockingQueue chapterQueue) { + LearnChapterQueue.chapterQueue = chapterQueue; + } +} diff --git a/tamguo-tms/src/main/java/com/tamguo/web/tiku/IndexController.java b/tamguo-tms/src/main/java/com/tamguo/web/tiku/IndexController.java index 4cbb201..c811502 100644 --- a/tamguo-tms/src/main/java/com/tamguo/web/tiku/IndexController.java +++ b/tamguo-tms/src/main/java/com/tamguo/web/tiku/IndexController.java @@ -1,5 +1,7 @@ package com.tamguo.web.tiku; +import java.util.concurrent.BlockingQueue; + import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -8,6 +10,9 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; +import com.tamguo.modules.tiku.model.ChapterEntity; + +import com.tamguo.modules.tiku.model.queue.LearnChapterQueue; import com.tamguo.utils.BrowserUtils; @Controller @@ -19,6 +24,8 @@ public class IndexController { public ModelAndView indexAction(ModelAndView model , HttpServletRequest request) { // request url logger.info("request url :{}" , request.getRequestURI()); + BlockingQueue chapterQueue = LearnChapterQueue.getChapterQueue(); + model.addObject("chapterList", chapterQueue.toArray()); if(BrowserUtils.isMobile(request.getHeader("user-agent"))) { model.setViewName("mobile/index"); }else { diff --git a/tamguo-tms/src/main/java/com/tamguo/web/tiku/QuestionContrller.java b/tamguo-tms/src/main/java/com/tamguo/web/tiku/QuestionContrller.java index 9d9dead..4db0ed3 100644 --- a/tamguo-tms/src/main/java/com/tamguo/web/tiku/QuestionContrller.java +++ b/tamguo-tms/src/main/java/com/tamguo/web/tiku/QuestionContrller.java @@ -1,6 +1,9 @@ package com.tamguo.web.tiku; import java.util.Arrays; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; @@ -21,6 +24,7 @@ import com.tamguo.modules.tiku.model.CourseEntity; import com.tamguo.modules.tiku.model.QuestionEntity; import com.tamguo.modules.tiku.model.SubjectEntity; import com.tamguo.modules.tiku.model.enums.QuestionTypeEnum; +import com.tamguo.modules.tiku.model.queue.LearnChapterQueue; import com.tamguo.modules.tiku.service.IChapterService; import com.tamguo.modules.tiku.service.ICourseService; import com.tamguo.modules.tiku.service.IQuestionAnswerService; @@ -44,10 +48,13 @@ public class QuestionContrller { @Autowired private IQuestionAnswerService iQuestionAnswerService; + // 章节 + BlockingQueue chapterQueue = new LinkedBlockingQueue(10); + @SuppressWarnings("unchecked") @RequestMapping(value = {"questionlist/{chapterId}-{current}-{size}.html"}, method = RequestMethod.GET) public ModelAndView questionList(@PathVariable String chapterId , @PathVariable Integer current , - @PathVariable Integer size , ModelAndView model , HttpServletRequest request){ + @PathVariable Integer size , ModelAndView model , HttpServletRequest request) throws InterruptedException{ // request url logger.info("request url :{} " , request.getRequestURI()); @@ -74,6 +81,8 @@ public class QuestionContrller { model.addObject("subjectId", course.getSubjectId()); model.addObject("courseId", course.getId()); + LearnChapterQueue.add(chapter); + if(BrowserUtils.isMobile(request.getHeader("user-agent"))) { model.setViewName("mobile/questionList"); }else { diff --git a/tamguo-tms/src/main/resources/templates/index.html b/tamguo-tms/src/main/resources/templates/index.html index e4cc873..4822ff2 100644 --- a/tamguo-tms/src/main/resources/templates/index.html +++ b/tamguo-tms/src/main/resources/templates/index.html @@ -89,12 +89,12 @@