Mercurial > hg > Members > nobuyasu > jungle-network
changeset 155:83e99eba6ec1
async thread comtext in showmessage servlet
author | tatsuki |
---|---|
date | Wed, 09 Jul 2014 19:44:47 +0900 |
parents | a6bf0369f0a6 |
children | 89999029c543 |
files | .classpath src/main/java/app/bbs/ShowMessageWithTimeStampServlet.java src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java |
diffstat | 3 files changed, 123 insertions(+), 91 deletions(-) [+] |
line wrap: on
line diff
--- a/.classpath Mon Jul 07 12:36:58 2014 +0900 +++ b/.classpath Wed Jul 09 19:44:47 2014 +0900 @@ -1,10 +1,19 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/> - <classpathentry including="**/*.java" kind="src" path="src/main/java"/> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="src" output="target/classes" path="src/main/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="var" path="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar"/> <classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="var" path="M2_REPO/junit/junit/4.7/junit-4.7.jar"/> <classpathentry kind="var" path="M2_REPO/org/eclipse/jetty/jetty-server/9.2.1.v20140609/jetty-server-9.2.1.v20140609.jar"/> <classpathentry kind="var" path="M2_REPO/org/eclipse/jetty/jetty-http/9.2.1.v20140609/jetty-http-9.2.1.v20140609.jar"/> @@ -57,5 +66,15 @@ <classpathentry kind="var" path="M2_REPO/com/github/stephenc/jamm/0.2.5/jamm-0.2.5.jar"/> <classpathentry kind="var" path="M2_REPO/com/eaio/uuid/uuid/3.3/uuid-3.3.jar"/> <classpathentry combineaccessrules="false" kind="src" path="/Alice"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath>
--- a/src/main/java/app/bbs/ShowMessageWithTimeStampServlet.java Mon Jul 07 12:36:58 2014 +0900 +++ b/src/main/java/app/bbs/ShowMessageWithTimeStampServlet.java Wed Jul 09 19:44:47 2014 +0900 @@ -2,10 +2,13 @@ import java.io.PrintWriter; +import javax.servlet.AsyncContext; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.thread.ThreadPool; + import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage; public class ShowMessageWithTimeStampServlet extends HttpServlet @@ -17,26 +20,34 @@ private final NetworkBulletinBoard bbs; private final String createBoardMessagePath; private final String editMessagePath; + private ThreadPool threadPool; private static final String PARAM_BOARD_NAME = "bname"; - public ShowMessageWithTimeStampServlet(NetworkBulletinBoard _bbs,String _createBoardMessagePath, String _editMessagePath) + public ShowMessageWithTimeStampServlet(NetworkBulletinBoard _bbs,String _createBoardMessagePath, String _editMessagePath, ThreadPool thp) { bbs = _bbs; createBoardMessagePath = _createBoardMessagePath; editMessagePath = _editMessagePath; + threadPool = thp; } public void doGet(HttpServletRequest _req,HttpServletResponse _res) { - String bname = _req.getParameter(PARAM_BOARD_NAME); - - try{ - printBoard(bname,_res.getWriter()); - }catch(Exception _e){ - _res.setStatus(500); - } - + final AsyncContext asc = _req.startAsync(); + final String bname = _req.getParameter(PARAM_BOARD_NAME); + Runnable printBoardThread = new Runnable() { + @Override + public void run() { + try{ + printBoard(bname,_res.getWriter()); + }catch(Exception _e){ + _res.setStatus(500); + } + asc.complete(); + } + }; + threadPool.execute(printBoardThread); } private void printBoard(String _bname,PrintWriter _pw) throws Exception
--- a/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java Mon Jul 07 12:36:58 2014 +0900 +++ b/src/main/java/app/bbs/codesegment/StartBBSCodeSegment.java Wed Jul 09 19:44:47 2014 +0900 @@ -6,8 +6,10 @@ import javax.servlet.Servlet; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.thread.ThreadPool; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.CreateBoardMessageServlet; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.CreateBoardServlet; @@ -26,86 +28,86 @@ import app.bbs.ShowMessageWithTimeStampServlet; public class StartBBSCodeSegment extends CodeSegment { - - int bbsPort = 8080; - Receiver host = ids.create(CommandType.PEEK); - private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$"); - private String[] args; - boolean persistentFlag = false; - - public StartBBSCodeSegment(String[] _args, int p) { - args = _args; - bbsPort = p; - host.setKey("host"); - } + + int bbsPort = 8080; + Receiver host = ids.create(CommandType.PEEK); + private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$"); + private String[] args; + boolean persistentFlag = false; + + public StartBBSCodeSegment(String[] _args, int p) { + args = _args; + bbsPort = p; + host.setKey("host"); + } + + public StartBBSCodeSegment() { + args = null; + host.setKey("host"); + } + + @Override + public void run() { + String name = host.asString(); + Matcher matcher = pattern.matcher(name); + matcher.find(); + // String type = matcher.group(1); + for(String arg: args) { + if(arg.equals("-persistent")){ + persistentFlag = true; + } + } + NetworkBulletinBoard cassaBBS = null; + if(persistentFlag) { + System.out.println("log loading..."); + cassaBBS = NetworkJungleBulletinBoard.NewPersistentJungle(name); + cassaBBS.init(); + } else { + cassaBBS = new NetworkJungleBulletinBoard(name); + cassaBBS.init(); + } - public StartBBSCodeSegment() { - args = null; - host.setKey("host"); - } - - @Override - public void run() { - String name = host.asString(); - Matcher matcher = pattern.matcher(name); - matcher.find(); -// String type = matcher.group(1); - for(String arg: args) { - if(arg.equals("-persistent")){ - persistentFlag = true; - } - } - NetworkBulletinBoard cassaBBS = null; - if(persistentFlag) { - System.out.println("log loading..."); - cassaBBS = NetworkJungleBulletinBoard.NewPersistentJungle(name); - cassaBBS.init(); - } else { - cassaBBS = new NetworkJungleBulletinBoard(name); - cassaBBS.init(); - } - - System.out.println("StartBBSCodeSegment"); - System.out.println("name : "+ name); - /* Jetty registration */ - String createBoardMessagePath = "/createBoardMessage"; - String createBoardPath = "/createBoard"; - String editMessagePath = "/editMessage"; - String showBoardMessagePath = "/showBoardMessage"; + System.out.println("StartBBSCodeSegment"); + System.out.println("name : "+ name); + /* Jetty registration */ + String createBoardMessagePath = "/createBoardMessage"; + String createBoardPath = "/createBoard"; + String editMessagePath = "/editMessage"; + String showBoardMessagePath = "/showBoardMessage"; + + + Server serv = new Server(bbsPort); + ThreadPool thp = serv.getThreadPool(); + Servlet createBoardMessage = new CreateBoardMessageServlet(cassaBBS); + Servlet createBoard = new CreateBoardServlet(cassaBBS); + Servlet editBoardMessage = new EditMessageServlet(cassaBBS); + Servlet index = new ShowBoardsServlet(cassaBBS,createBoardPath,showBoardMessagePath); + // Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath); + Servlet board = new ShowMessageWithTimeStampServlet(cassaBBS,createBoardMessagePath,editMessagePath,thp); - - Servlet createBoardMessage = new CreateBoardMessageServlet(cassaBBS); - Servlet createBoard = new CreateBoardServlet(cassaBBS); - Servlet editBoardMessage = new EditMessageServlet(cassaBBS); - Servlet index = new ShowBoardsServlet(cassaBBS,createBoardPath,showBoardMessagePath); -// Servlet board = new ShowBoardMessageServlet(cassaBBS,createBoardMessagePath,editMessagePath); - Servlet board = new ShowMessageWithTimeStampServlet(cassaBBS,createBoardMessagePath,editMessagePath); - - Server serv = new Server(bbsPort); - ServletHandler context = new ServletHandler(); - context.addServletWithMapping(new ServletHolder(createBoardMessage),createBoardMessagePath); - context.addServletWithMapping(new ServletHolder(createBoard),createBoardPath); - context.addServletWithMapping(new ServletHolder(editBoardMessage),editMessagePath); - context.addServletWithMapping(new ServletHolder(index),"/"); - context.addServletWithMapping(new ServletHolder(board),showBoardMessagePath); - /* - * For write benchmark - */ - String editMessageUseGetPath = "/editMessageUseGet"; - Servlet editMessageUseGet = new EditMessageUseGetServlet(cassaBBS); - context.addServletWithMapping(new ServletHolder(editMessageUseGet), editMessageUseGetPath); - String requestNumCheckPath = "/requestNum"; - Servlet requestNumCheckServlet = new RequestNumCheckServlet(cassaBBS); - context.addServletWithMapping(new ServletHolder(requestNumCheckServlet), requestNumCheckPath); - - - serv.setHandler(context); - try { - serv.start(); - } catch (Exception e) { - e.printStackTrace(); - } - new LogUpdateCodeSegment(); - } + ServletHandler context = new ServletHandler(); + context.addServletWithMapping(new ServletHolder(createBoardMessage),createBoardMessagePath); + context.addServletWithMapping(new ServletHolder(createBoard),createBoardPath); + context.addServletWithMapping(new ServletHolder(editBoardMessage),editMessagePath); + context.addServletWithMapping(new ServletHolder(index),"/"); + context.addServletWithMapping(new ServletHolder(board),showBoardMessagePath); + /* + * For write benchmark + */ + String editMessageUseGetPath = "/editMessageUseGet"; + Servlet editMessageUseGet = new EditMessageUseGetServlet(cassaBBS); + context.addServletWithMapping(new ServletHolder(editMessageUseGet), editMessageUseGetPath); + String requestNumCheckPath = "/requestNum"; + Servlet requestNumCheckServlet = new RequestNumCheckServlet(cassaBBS); + context.addServletWithMapping(new ServletHolder(requestNumCheckServlet), requestNumCheckPath); + + serv.setHandler(context); + try { + serv.start(); + } catch (Exception e) { + e.printStackTrace(); + } + new LogUpdateCodeSegment(); + } }