From 765f0c13bbf262f139df5316e15b4f8ca9103c2f Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 18 Jul 2013 15:36:24 +0800 Subject: [PATCH] refactoring --- .../bench4q/master/api/AgentController.java | 18 ----- .../master/api/RecordPortController.java | 14 ++++ .../master/api/TestPlanController.java | 65 +++++++++++++++---- .../org/bench4q/master/entity/db/Port.java | 31 +++++++++ .../bench4q/master/service/AgentHelper.java | 59 ----------------- .../bench4q/master/service/AgentService.java | 7 +- .../master/service/PortPoolService.java | 34 +++++----- .../bench4q/master/test/TestPlanTester.java | 2 +- 8 files changed, 117 insertions(+), 113 deletions(-) create mode 100644 src/main/java/org/bench4q/master/entity/db/Port.java delete mode 100644 src/main/java/org/bench4q/master/service/AgentHelper.java diff --git a/src/main/java/org/bench4q/master/api/AgentController.java b/src/main/java/org/bench4q/master/api/AgentController.java index f74e3271..d0c8b540 100644 --- a/src/main/java/org/bench4q/master/api/AgentController.java +++ b/src/main/java/org/bench4q/master/api/AgentController.java @@ -13,10 +13,8 @@ import org.bench4q.master.api.model.AgentResponseModel; import org.bench4q.master.communication.AgentStateService; import org.bench4q.master.communication.HttpRequester; import org.bench4q.master.communication.HttpRequester.HttpResponse; -import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.communication.agent.TestBriefStatusModel; import org.bench4q.master.entity.SyncAgent; -import org.bench4q.master.service.AgentHelper; import org.bench4q.master.service.AgentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -28,7 +26,6 @@ import org.springframework.web.bind.annotation.RequestParam; @RequestMapping("/agentManage") public class AgentController extends BaseController { private AgentService agentPoolService = new AgentService(); - private AgentHelper agentHelper = new AgentHelper(); private HttpRequester httpRequester = new HttpRequester(); private AgentStateService agentStateService = new AgentStateService(); private static final Object AGENT_LOCK = new Object(); @@ -60,15 +57,6 @@ public class AgentController extends BaseController { this.httpRequester = httpRequester; } - public AgentHelper getAgentHelper() { - return agentHelper; - } - - @Autowired - public void setAgentHelper(AgentHelper agentHelper) { - this.agentHelper = agentHelper; - } - @RequestMapping(value = "/AddAgentToPool", method = RequestMethod.POST) public AgentResponseModel addAgentToPool(@RequestParam SyncAgent syncAgent) { synchronized (AGENT_LOCK) { @@ -138,12 +126,6 @@ public class AgentController extends BaseController { } } - public RunScenarioResultModel sendScriptContentToAgent( - String hostNameString, int port, String scriptContent) { - return this.getAgentHelper().sendScriptContentToAgent(hostNameString, - port, scriptContent); - } - public int getLivingAgentCount() { int livingCount = 0; if (this.getAgentPoolService().getAgentPool().isEmpty()) { diff --git a/src/main/java/org/bench4q/master/api/RecordPortController.java b/src/main/java/org/bench4q/master/api/RecordPortController.java index 5ba3df6f..a941d2c5 100644 --- a/src/main/java/org/bench4q/master/api/RecordPortController.java +++ b/src/main/java/org/bench4q/master/api/RecordPortController.java @@ -2,6 +2,8 @@ package org.bench4q.master.api; import org.bench4q.master.api.model.OrganizeRecordPortResponseModel; import org.bench4q.master.entity.Constant; +import org.bench4q.master.service.PortPoolService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -11,6 +13,18 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/RecordPort") public class RecordPortController extends BaseController { + + private PortPoolService portPoolService = new PortPoolService(); + + public PortPoolService getPortPoolService() { + return portPoolService; + } + + @Autowired + private void setPortPoolService(PortPoolService portPoolService) { + this.portPoolService = portPoolService; + } + @RequestMapping(value = "/AddPortToPortPool", method = RequestMethod.GET) @ResponseBody public OrganizeRecordPortResponseModel addPortToPortPool( diff --git a/src/main/java/org/bench4q/master/api/TestPlanController.java b/src/main/java/org/bench4q/master/api/TestPlanController.java index e031278c..7a001169 100644 --- a/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -1,14 +1,21 @@ package org.bench4q.master.api; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.UUID; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + import org.bench4q.master.communication.AgentStateService; +import org.bench4q.master.communication.HttpRequester; +import org.bench4q.master.communication.HttpRequester.HttpResponse; import org.bench4q.master.communication.agent.RunScenarioResultModel; import org.bench4q.master.entity.SyncAgent; -import org.bench4q.master.service.AgentHelper; import org.bench4q.master.service.AgentService; import org.bench4q.master.service.ScriptService; import org.springframework.beans.factory.annotation.Autowired; @@ -22,8 +29,8 @@ import org.springframework.web.bind.annotation.RequestParam; public class TestPlanController extends BaseController { private ScriptService scriptService = new ScriptService(); private AgentService agentPoolService = new AgentService(); - private AgentHelper agentHelper = new AgentHelper(); private AgentStateService agentStateService = new AgentStateService(); + private HttpRequester httpRequester = new HttpRequester(); public ScriptService getScriptService() { return scriptService; @@ -38,15 +45,6 @@ public class TestPlanController extends BaseController { return agentPoolService; } - public AgentHelper getAgentHelper() { - return agentHelper; - } - - @Autowired - public void setAgentHelper(AgentHelper agentHelper) { - this.agentHelper = agentHelper; - } - @Autowired public void setAgentPoolService(AgentService agentPoolService) { this.agentPoolService = agentPoolService; @@ -61,6 +59,15 @@ public class TestPlanController extends BaseController { this.agentStateService = agentStateService; } + public HttpRequester getHttpRequester() { + return httpRequester; + } + + @Autowired + public void setHttpRequester(HttpRequester httpRequester) { + this.httpRequester = httpRequester; + } + @RequestMapping(value = "/runTestPlanWithScriptId", method = RequestMethod.GET) public void runTestPlanWithScriptId(@RequestParam int scriptId, @RequestParam int requireLoad) { @@ -88,9 +95,8 @@ public class TestPlanController extends BaseController { } if (syncAgent.getRemainLoad() > requireLoad) { RunScenarioResultModel runScenarioResultModel = this - .getAgentHelper().sendScriptContentToAgent( - syncAgent.getHostName(), syncAgent.getPort(), - scriptContentString); + .sendScriptContentToAgent(syncAgent.getHostName(), + syncAgent.getPort(), scriptContentString); if (!runScenarioResultModel.getRunId().equals(null)) { map.put(syncAgent.getHostName(), runScenarioResultModel.getRunId()); @@ -100,4 +106,35 @@ public class TestPlanController extends BaseController { } return map; } + + public RunScenarioResultModel sendScriptContentToAgent( + String hostNameString, int port, String scriptContent) { + RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); + try { + HttpResponse httpResponse = this.getHttpRequester().sendPostXml( + hostNameString + ":" + port + "/test/run", scriptContent); + + runScenarioResultModel = extractRunSenarioResultModel(httpResponse + .getContent()); + + return runScenarioResultModel; + } catch (IOException e) { + e.printStackTrace(); + return runScenarioResultModel; + } catch (JAXBException e) { + e.printStackTrace(); + return runScenarioResultModel; + } + } + + private RunScenarioResultModel extractRunSenarioResultModel( + String responseContent) throws JAXBException { + RunScenarioResultModel resultModel = new RunScenarioResultModel(); + + Unmarshaller unmarshaller = JAXBContext.newInstance( + resultModel.getClass()).createUnmarshaller( ); + resultModel = (RunScenarioResultModel) unmarshaller + .unmarshal(new ByteArrayInputStream(responseContent.getBytes())); + return resultModel; + } } diff --git a/src/main/java/org/bench4q/master/entity/db/Port.java b/src/main/java/org/bench4q/master/entity/db/Port.java new file mode 100644 index 00000000..edce96fa --- /dev/null +++ b/src/main/java/org/bench4q/master/entity/db/Port.java @@ -0,0 +1,31 @@ +package org.bench4q.master.entity.db; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +@Entity +@Table(name = "port") +public class Port { + private int id; + private int port; + + @Column(name = "id", nullable = false) + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Column(name = "port", nullable = false) + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + +} diff --git a/src/main/java/org/bench4q/master/service/AgentHelper.java b/src/main/java/org/bench4q/master/service/AgentHelper.java deleted file mode 100644 index 9555474d..00000000 --- a/src/main/java/org/bench4q/master/service/AgentHelper.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.bench4q.master.service; - -import java.io.ByteArrayInputStream; -import java.io.IOException; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; - -import org.bench4q.master.communication.HttpRequester; -import org.bench4q.master.communication.HttpRequester.HttpResponse; -import org.bench4q.master.communication.agent.RunScenarioResultModel; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class AgentHelper { - private HttpRequester httpRequester = new HttpRequester(); - - public HttpRequester getHttpRequester() { - return httpRequester; - } - - @Autowired - public void setHttpRequester(HttpRequester httpRequester) { - this.httpRequester = httpRequester; - } - - public RunScenarioResultModel sendScriptContentToAgent( - String hostNameString, int port, String scriptContent) { - RunScenarioResultModel runScenarioResultModel = new RunScenarioResultModel(); - try { - HttpResponse httpResponse = this.getHttpRequester().sendPostXml( - hostNameString + ":" + port + "/test/run", scriptContent); - - runScenarioResultModel = extractRunSenarioResultModel(httpResponse - .getContent()); - - return runScenarioResultModel; - } catch (IOException e) { - e.printStackTrace(); - return runScenarioResultModel; - } catch (JAXBException e) { - e.printStackTrace(); - return runScenarioResultModel; - } - } - - private RunScenarioResultModel extractRunSenarioResultModel( - String responseContent) throws JAXBException { - RunScenarioResultModel resultModel = new RunScenarioResultModel(); - - Unmarshaller unmarshaller = JAXBContext.newInstance( - resultModel.getClass()).createUnmarshaller(); - resultModel = (RunScenarioResultModel) unmarshaller - .unmarshal(new ByteArrayInputStream(responseContent.getBytes())); - return resultModel; - } -} diff --git a/src/main/java/org/bench4q/master/service/AgentService.java b/src/main/java/org/bench4q/master/service/AgentService.java index ce3664e5..7e836e5f 100644 --- a/src/main/java/org/bench4q/master/service/AgentService.java +++ b/src/main/java/org/bench4q/master/service/AgentService.java @@ -16,20 +16,15 @@ import org.springframework.stereotype.Component; @Component public class AgentService { private SessionHelper sessionHelper = new SessionHelper(); - private Map agentPool = new HashMap(); + private static Map agentPool = new HashMap(); public AgentService() { - this.setAgentPool(new HashMap()); } public Map getAgentPool() { return agentPool; } - private void setAgentPool(Map agentPool) { - this.agentPool = agentPool; - } - private SessionHelper getSessionHelper() { return sessionHelper; } diff --git a/src/main/java/org/bench4q/master/service/PortPoolService.java b/src/main/java/org/bench4q/master/service/PortPoolService.java index 169dde6b..251e2a70 100644 --- a/src/main/java/org/bench4q/master/service/PortPoolService.java +++ b/src/main/java/org/bench4q/master/service/PortPoolService.java @@ -2,31 +2,35 @@ package org.bench4q.master.service; import java.util.Vector; +import org.springframework.stereotype.Component; + +@Component public class PortPoolService { - private Vector ScriptPortPool = new Vector(); + private static Vector ScriptPortPool = new Vector(); + private static Object syncObject = new Object(); - public PortPoolService() { - this.setScriptPortPool(new Vector()); + public static Object getSyncObject() { + return syncObject; } public Vector getScriptPortPool() { - return ScriptPortPool; + return PortPoolService.ScriptPortPool; } - private void setScriptPortPool(Vector scriptPortPool) { - ScriptPortPool = scriptPortPool; + public boolean addPortToPool(int port) { + synchronized (PortPoolService.syncObject) { + if (this.getScriptPortPool().contains(port)) { + return false; + } + this.getScriptPortPool().add(port); + } + return true; } - public void addPortToPool(int port) - { - this.getScriptPortPool().add(port); - } - - public void removePortFromPool(int port) - { - if(this.getScriptPortPool().contains(port)){ - + public void removePortFromPool(int port) { + if (this.getScriptPortPool().contains(port)) { + } } } diff --git a/src/test/java/org/bench4q/master/test/TestPlanTester.java b/src/test/java/org/bench4q/master/test/TestPlanTester.java index 49a5c8a5..ea488216 100644 --- a/src/test/java/org/bench4q/master/test/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/TestPlanTester.java @@ -84,7 +84,7 @@ public class TestPlanTester { hostNameString = iterator.next(); TestBriefStatusModel testBriefStatusModel = (new AgentController()).getBriefStatusModelFromAgent( hostNameString, 6565, vector.get(hostNameString)); - System.out.println(testBriefStatusModel); + System.out.println(testBriefStatusModel.getAverageResponseTime()); } }