From 3230a6bebeda7d21d636e8671e108461a66b0e01 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Fri, 5 Jul 2013 11:35:31 +0800 Subject: [PATCH] hdfs storage tested. jar package configuration added. --- descriptor.xml | 25 ++++++++++++++++ pom.xml | 26 ++++++++++++++++ .../java/org/bench4q/agent/AgentServer.java | 5 ++-- .../agent/scenario/ScenarioEngine.java | 30 +++++++++++++------ .../{HdfsService.java => HdfsStorage.java} | 4 ++- .../agent/config}/application-context.xml | 0 .../bench4q/agent/test/RunScenarioTest.java | 6 ++-- 7 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 descriptor.xml rename src/main/java/org/bench4q/agent/storage/{HdfsService.java => HdfsStorage.java} (92%) rename src/main/resources/{ => org/bench4q/agent/config}/application-context.xml (100%) diff --git a/descriptor.xml b/descriptor.xml new file mode 100644 index 00000000..276d32a3 --- /dev/null +++ b/descriptor.xml @@ -0,0 +1,25 @@ + + + jar-with-dependencies + + jar + + false + + + / + false + false + runtime + + + + + ${project.build.outputDirectory} + / + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5e033447..af774e3f 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,32 @@ + + + maven-assembly-plugin + + + descriptor.xml + + false + + + org.bench4q.agent.Main + true + + + + + + make-assembly + package + + assembly + + + + + bench4q-agent \ No newline at end of file diff --git a/src/main/java/org/bench4q/agent/AgentServer.java b/src/main/java/org/bench4q/agent/AgentServer.java index f1940cd1..bfd0ac52 100644 --- a/src/main/java/org/bench4q/agent/AgentServer.java +++ b/src/main/java/org/bench4q/agent/AgentServer.java @@ -40,8 +40,9 @@ public class AgentServer { ServletContextHandler servletContextHandler = new ServletContextHandler(); ServletHolder servletHolder = servletContextHandler.addServlet( DispatcherServlet.class, "/"); - servletHolder.setInitParameter("contextConfigLocation", - "classpath*:/application-context.xml"); + servletHolder + .setInitParameter("contextConfigLocation", + "classpath*:/org/bench4q/agent/config/application-context.xml"); this.getServer().setHandler(servletContextHandler); this.getServer().start(); return true; diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index db59fec3..5103a7e4 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -1,7 +1,6 @@ package org.bench4q.agent.scenario; -import java.io.File; -import java.io.FileWriter; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -17,6 +16,7 @@ import javax.xml.bind.Marshaller; import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.PluginManager; +import org.bench4q.agent.storage.HdfsStorage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -24,6 +24,7 @@ import org.springframework.stereotype.Component; public class ScenarioEngine { private PluginManager pluginManager; private Map runningTests; + private HdfsStorage hdfsStorage; public ScenarioEngine() { this.setRunningTests(new HashMap()); @@ -38,6 +39,15 @@ public class ScenarioEngine { this.pluginManager = pluginManager; } + private HdfsStorage getHdfsStorage() { + return hdfsStorage; + } + + @Autowired + private void setHdfsStorage(HdfsStorage hdfsStorage) { + this.hdfsStorage = hdfsStorage; + } + public Map getRunningTests() { return runningTests; } @@ -147,7 +157,7 @@ public class ScenarioEngine { } public String getPath() { - return System.getProperty("user.dir"); + return "hdfs://133.133.12.21:9000/home/bench4q/results"; } public void saveTestResults(UUID runId) { @@ -185,20 +195,22 @@ public class ScenarioEngine { } private void doSaveTestResults(UUID runId, TestResult testResult) { - FileWriter fileWriter = null; + StringWriter stringWriter = null; try { Marshaller marshaller = JAXBContext.newInstance( testResult.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - fileWriter = new FileWriter(new File(this.getPath() + "/" - + runId.toString() + ".xml")); - marshaller.marshal(testResult, fileWriter); + stringWriter = new StringWriter(); + String fileName = this.getPath() + "/" + runId.toString() + ".xml"; + marshaller.marshal(testResult, stringWriter); + String content = stringWriter.toString(); + this.getHdfsStorage().writeFile(content, fileName); } catch (Exception e) { e.printStackTrace(); } finally { - if (fileWriter != null) { + if (stringWriter != null) { try { - fileWriter.close(); + stringWriter.close(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/org/bench4q/agent/storage/HdfsService.java b/src/main/java/org/bench4q/agent/storage/HdfsStorage.java similarity index 92% rename from src/main/java/org/bench4q/agent/storage/HdfsService.java rename to src/main/java/org/bench4q/agent/storage/HdfsStorage.java index fcb33f84..4d3b6c69 100644 --- a/src/main/java/org/bench4q/agent/storage/HdfsService.java +++ b/src/main/java/org/bench4q/agent/storage/HdfsStorage.java @@ -16,9 +16,11 @@ import org.apache.hadoop.io.IOUtils; import org.springframework.stereotype.Component; @Component -public class HdfsService { +public class HdfsStorage { private FileSystem getFileSystem() throws IOException { Configuration conf = new Configuration(); + conf.set("mapred.jop.tracker", "hdfs://133.133.12.21:9001"); + conf.set("fs.default.name", "hdfs://133.133.12.21:9000"); return FileSystem.get(conf); } diff --git a/src/main/resources/application-context.xml b/src/main/resources/org/bench4q/agent/config/application-context.xml similarity index 100% rename from src/main/resources/application-context.xml rename to src/main/resources/org/bench4q/agent/config/application-context.xml diff --git a/src/test/java/org/bench4q/agent/test/RunScenarioTest.java b/src/test/java/org/bench4q/agent/test/RunScenarioTest.java index 518e6afd..e50d9bb5 100644 --- a/src/test/java/org/bench4q/agent/test/RunScenarioTest.java +++ b/src/test/java/org/bench4q/agent/test/RunScenarioTest.java @@ -44,7 +44,7 @@ public class RunScenarioTest { getUserBehaviorModel.setParameters(new ArrayList()); ParameterModel parameterModelOne = new ParameterModel(); parameterModelOne.setKey("url"); - parameterModelOne.setValue("http://localhost:6565"); + parameterModelOne.setValue("http://Bench4Q-Agent-1:6565"); getUserBehaviorModel.getParameters().add(parameterModelOne); runScenarioModel.getUserBehaviors().add(getUserBehaviorModel); UserBehaviorModel timerUserBehaviorModel = new UserBehaviorModel(); @@ -64,7 +64,7 @@ public class RunScenarioTest { .setParameters(new ArrayList()); ParameterModel parameterModelThree = new ParameterModel(); parameterModelThree.setKey("url"); - parameterModelThree.setValue("http://localhost:6565"); + parameterModelThree.setValue("http://Bench4Q-Agent-1:6565"); postUserBehaviorModel.getParameters().add(parameterModelThree); ParameterModel parameterModelFour = new ParameterModel(); parameterModelFour.setKey("content"); @@ -75,7 +75,7 @@ public class RunScenarioTest { runScenarioModel.getClass()).createMarshaller(); StringWriter stringWriter = new StringWriter(); marshaller.marshal(runScenarioModel, stringWriter); - String url = "http://localhost:6565/test/run"; + String url = "http://Bench4Q-Agent-1:6565/test/run"; String content = stringWriter.toString(); System.out.println(content); URL target = new URL(url);