refactoring

This commit is contained in:
coderfengyun 2013-07-18 15:36:24 +08:00
parent 5ab657f721
commit 765f0c13bb
8 changed files with 117 additions and 113 deletions

View File

@ -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()) {

View File

@ -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(

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -16,20 +16,15 @@ import org.springframework.stereotype.Component;
@Component
public class AgentService {
private SessionHelper sessionHelper = new SessionHelper();
private Map<String, SyncAgent> agentPool = new HashMap<String, SyncAgent>();
private static Map<String, SyncAgent> agentPool = new HashMap<String, SyncAgent>();
public AgentService() {
this.setAgentPool(new HashMap<String, SyncAgent>());
}
public Map<String, SyncAgent> getAgentPool() {
return agentPool;
}
private void setAgentPool(Map<String, SyncAgent> agentPool) {
this.agentPool = agentPool;
}
private SessionHelper getSessionHelper() {
return sessionHelper;
}

View File

@ -2,31 +2,35 @@ package org.bench4q.master.service;
import java.util.Vector;
import org.springframework.stereotype.Component;
@Component
public class PortPoolService {
private Vector<Integer> ScriptPortPool = new Vector<Integer>();
private static Vector<Integer> ScriptPortPool = new Vector<Integer>();
private static Object syncObject = new Object();
public PortPoolService() {
this.setScriptPortPool(new Vector<Integer>());
public static Object getSyncObject() {
return syncObject;
}
public Vector<Integer> getScriptPortPool() {
return ScriptPortPool;
return PortPoolService.ScriptPortPool;
}
private void setScriptPortPool(Vector<Integer> 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)) {
}
}
}

View File

@ -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());
}
}