refactor and add all about release session

This commit is contained in:
rzs840707 2014-06-27 10:11:28 +08:00
parent 5080115875
commit 559ee106c3
7 changed files with 579 additions and 554 deletions

View File

@ -2,6 +2,7 @@ package org.bench4q.agent.plugin.basic.MongoDB;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
@ -78,14 +79,14 @@ public class MongoDBPlugin {
String propertyValue = row.getCell("propertyValue");
query.append(propertyName, propertyValue);
}
System.out.println(query);
DBCursor queryResult = table.find(query);
for (DBObject item : queryResult.toArray()) {
System.out.println(item);
}
return new MongoDBReturn(true);
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(this.getClass()).info(e, e);
return new MongoDBReturn(false);
} finally {
if (mongo != null) {

View File

@ -32,9 +32,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_MongoDBPlugin extends TestBase {
private UUID testId = UUID.randomUUID();
//localhost", 10000
@Test
public void test_Insert() {
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("localhost", 27017,
MongoDBPlugin mongoDBPlugin = new MongoDBPlugin("m001.node.com", 27017,
"temp");
MongoDBReturn result = mongoDBPlugin
.insert(this.testId.toString(),
@ -52,7 +54,7 @@ public class Test_MongoDBPlugin extends TestBase {
mongoDB.setId(pluginId);
mongoDB.setName("MongoDBPlugin");
mongoDB.setParameters(Arrays.asList(
ParameterModel.createParameter("hostName", "localhost"),
ParameterModel.createParameter("hostName", "m001.node.com"),
ParameterModel.createParameter("port", "27017"),
ParameterModel.createParameter("dbName", "temp")));
scenarioModel.getUsePlugins().add(mongoDB);

View File

@ -35,7 +35,7 @@ public class Test_Scenario extends TestBase {
@Test
public void test_RealScenario() throws IOException {
BehaviorModel[] behaviors = new BehaviorModel[28];
BehaviorModel[] behaviors = new BehaviorModel[29];
for (int i = 0; i < 9; i++) {
behaviors[3 * i] = BehaviorModel.ControlBehaviorBuilder(3 * i,
"Next", "UUID_0", Collections.<ParameterModel> emptyList());
@ -51,15 +51,21 @@ public class Test_Scenario extends TestBase {
ParameterModel.createParameter("key",
"${UUID_0:var}"),
ParameterModel
.createParameter("value",
"asdfasdfasfasdfasdfgagasdfasdfasdfagasdgasdgasdf")));
.createParameter(
"value",
"828731929090399688663513844419156517791913487136162075054396888608979390802698967398774976538053071265086303536432973027547398485897859650637839199967916419278859657562410371550970990533755543895317958490879810955359859803793616914752497927102101107393602107374624265703167902037562601756290624400217677905796841586506540830996528039667809341170671731917392828543425909887469350859806312812589702773082295018742634842159778009485901428286815715514440370336211621120027294867866046842579395402354849290925687231979896816952130161564546780855710443002842992917011602246580128072986737704758180050405202976240171827695507627799312781883033823819663102843159141022325482803101497005554269652897365593399081219143594918151989844361363454642099858085072470136971359171934346214072016627940215525657780835264287095014748952849011967941296078931973149033644269161629346531549579584063826798825958153903463232155174301997795504543310963155667642944030315802981522872371957377604512253881055347955240594154853114826089693318722619283530276507503")));
}
behaviors[27] = BehaviorModel.UserBehaviorBuilder(27, "Query",
"MongoDBPlugin_0",
Arrays.asList(ParameterModel.createParameter("properties",
"propertyName=key|propertyValue=${Context_0:var}")));
behaviors[28] = BehaviorModel.TimerBehaviorBuilder(28, "Sleep",
"Timer_0",
Arrays.asList(ParameterModel.createParameter("time", "10")));
RunScenarioModel runScenarioModel = buildRunScenarioModelWith(
Arrays.asList(
new UsePluginModel("Timer_0", "ConstantTimer",
Collections.<ParameterModel> emptyList()),
new UsePluginModel("UUID_0", "UUID", null),
new UsePluginModel("Context_0", "Context", null),
new UsePluginModel("MongoDBPlugin_0", "MongoDBPlugin",

View File

@ -1,172 +1,174 @@
package org.bench4q.master.domain.repository;
import java.util.Collection;
import java.util.List;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
@Component
public class AgentRepository extends AbstractRepositoty {
private final Object addDeleteLock = new Object();
public Object getAddDeleteLock() {
return addDeleteLock;
}
public boolean attach(Agent agentWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session
.createCriteria(Agent.class)
.add(Restrictions.eq("hostName",
agentWithoutId.getHostName())).uniqueResult();
if (agent != null) {
return false;
}
session.merge(agentWithoutId);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
@Override
protected void guardOtherUniqueConditionForEntity(
String uniquePropertyName, String value)
throws EntityUniqueAlReadyExistException {
Agent agent = this.getAgentBy(value);
if (agent != null) {
throw new EntityUniqueAlReadyExistException(
"Agent with the hostName " + agent.getHostName()
+ " already Exist");
}
}
public boolean detach(int id) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session.get(Agent.class, id);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
}
}
public boolean detach(String hostName) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = getAgentBy(hostName, session);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
public boolean update(Agent agentForUpdate) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.update(agentForUpdate);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public boolean Update(Collection<Agent> poolToUpdate) {
if (poolToUpdate.size() == 0) {
return true;
}
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
for (Agent agent : poolToUpdate) {
session.update(agent);
}
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public Agent getAgentBy(String hostName) {
Session session = this.getSessionHelper().openSession();
try {
Agent agent = getAgentBy(hostName, session);
return agent;
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
private Agent getAgentBy(String hostName, Session session) {
return (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
}
public Agent getEntity(int id) {
Session session = this.getSessionHelper().openSession();
try {
return (Agent) session.get(Agent.class, id);
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<Agent> loadEntities() {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(Agent.class)
.addOrder(Order.desc("remainLoad")).list();
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
}
package org.bench4q.master.domain.repository;
import java.util.Collection;
import java.util.List;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
@Component
public class AgentRepository extends AbstractRepositoty {
private final Object addDeleteLock = new Object();
public Object getAddDeleteLock() {
return addDeleteLock;
}
public boolean attach(Agent agentWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session
.createCriteria(Agent.class)
.add(Restrictions.eq("hostName",
agentWithoutId.getHostName())).uniqueResult();
if (agent != null) {
return false;
}
session.merge(agentWithoutId);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
@Override
protected void guardOtherUniqueConditionForEntity(
String uniquePropertyName, String value)
throws EntityUniqueAlReadyExistException {
Agent agent = this.getAgentBy(value);
if (agent != null) {
throw new EntityUniqueAlReadyExistException(
"Agent with the hostName " + agent.getHostName()
+ " already Exist");
}
}
public boolean detach(int id) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = (Agent) session.get(Agent.class, id);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public boolean detach(String hostName) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Agent agent = getAgentBy(hostName, session);
if (agent == null) {
return false;
}
session.delete(agent);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
public boolean update(Agent agentForUpdate) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.update(agentForUpdate);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public boolean Update(Collection<Agent> poolToUpdate) {
if (poolToUpdate.size() == 0) {
return true;
}
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
for (Agent agent : poolToUpdate) {
session.update(agent);
}
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public Agent getAgentBy(String hostName) {
Session session = this.getSessionHelper().openSession();
try {
Agent agent = getAgentBy(hostName, session);
return agent;
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
private Agent getAgentBy(String hostName, Session session) {
return (Agent) session.createCriteria(Agent.class)
.add(Restrictions.eq("hostName", hostName)).uniqueResult();
}
public Agent getEntity(int id) {
Session session = this.getSessionHelper().openSession();
try {
return (Agent) session.get(Agent.class, id);
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<Agent> loadEntities() {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(Agent.class)
.addOrder(Order.desc("remainLoad")).list();
} catch (Exception e) {
return null;
} finally {
releaseSession(session);
}
}
}

View File

@ -1,229 +1,226 @@
package org.bench4q.master.domain.repository;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.MonitorResult;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanRepository extends AbstractRepositoty {
private TestPlanFactory testPlanFactory;
private final Map<UUID, TestPlan> runningTestPlans = new HashMap<UUID, TestPlan>();
private Logger logger = Logger.getLogger(TestPlanRepository.class);
private TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
}
@Autowired
private void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
private Map<UUID, TestPlan> getRunningTestPlans() {
return runningTestPlans;
}
public boolean attach(TestPlan testPlanDBWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.merge(testPlanDBWithoutId);
transaction.commit();
return true;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
@Override
protected void guardOtherUniqueConditionForEntity(
String uniquePropertyName, String value)
throws EntityUniqueAlReadyExistException {
if (getTestPlanInDomainBy(UUID.fromString(value)) != null) {
throw new EntityUniqueAlReadyExistException(
"Testplan with the testPlanRunId " + value
+ "already exists");
}
}
public boolean detach(int id) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
TestPlan testPlanInDB = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("id", id)).uniqueResult();
if (testPlanInDB == null) {
return false;
}
session.delete(testPlanInDB);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public TestPlan getTestPlanInDomainBy(UUID testPlanRunId) {
TestPlan result = null;
Session session = this.getSessionHelper().openSession();
result = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
.uniqueResult();
if (result == null) {
logger.error("can't find testplan with testPlanRunId : "
+ testPlanRunId.toString() + " in TestPlanRepository");
}
releaseSession(session);
return this.getTestPlanFactory().convertToDomain(result);
}
public TestPlan doGetTestPlanBy(Session session, UUID testPlanRunId) {
return (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
.uniqueResult();
}
public List<TestPlan> loadEntities(User user) {
Session session = this.getSessionHelper().openSession();
try {
@SuppressWarnings("unchecked")
List<TestPlan> ret = session.createCriteria(TestPlan.class)
.add(Restrictions.eq("user", user))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return ret;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<TestPlan> loadTestPlansBy(List<Criterion> criterions,
Order order) {
Session session = this.getSessionHelper().openSession();
try {
Criteria criteria = session.createCriteria(TestPlan.class);
for (Criterion criterion : criterions) {
criteria.add(criterion);
}
return criteria.addOrder(order).list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return new LinkedList<TestPlan>();
} finally {
releaseSession(session);
}
}
public boolean updateEntity(TestPlan testPlanDB) {
if (testPlanDB == null) {
return false;
}
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.update(testPlanDB);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public boolean doUpdateEntity(Session session, TestPlan testPlan) {
Transaction transaction = session.beginTransaction();
try {
session.update(testPlan);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
}
}
public TestPlan getRunningTestPlanBy(UUID testPlanRunId) {
return this.getRunningTestPlans().get(testPlanRunId);
}
public void attachRunningTestPlan(TestPlan testPlan) {
this.getRunningTestPlans().put(
UUID.fromString(testPlan.getTestPlanRunId()), testPlan);
}
public void detachRunningTestPlan(UUID testPlanRunId) {
this.getRunningTestPlans().remove(testPlanRunId);
}
@SuppressWarnings("unchecked")
public List<MonitorResult> getMonitorResultsByMonitor(Monitor monitor) {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(MonitorResult.class)
.add(Restrictions.eq("monitor", monitor)).list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
@SuppressWarnings("unchecked")
public List<TestPlanScriptResult> getScriptResultsByTestPlanScript(
TestPlanScript testPlanScript) {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
}
package org.bench4q.master.domain.repository;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.MonitorResult;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.EntityUniqueAlReadyExistException;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanRepository extends AbstractRepositoty {
private TestPlanFactory testPlanFactory;
private final Map<UUID, TestPlan> runningTestPlans = new HashMap<UUID, TestPlan>();
private Logger logger = Logger.getLogger(TestPlanRepository.class);
private TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
}
@Autowired
private void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
private Map<UUID, TestPlan> getRunningTestPlans() {
return runningTestPlans;
}
public boolean attach(TestPlan testPlanDBWithoutId) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.merge(testPlanDBWithoutId);
transaction.commit();
return true;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
@Override
protected void guardOtherUniqueConditionForEntity(
String uniquePropertyName, String value)
throws EntityUniqueAlReadyExistException {
if (getTestPlanInDomainBy(UUID.fromString(value)) != null) {
throw new EntityUniqueAlReadyExistException(
"Testplan with the testPlanRunId " + value
+ "already exists");
}
}
public boolean detach(int id) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
TestPlan testPlanInDB = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("id", id)).uniqueResult();
if (testPlanInDB == null) {
return false;
}
session.delete(testPlanInDB);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
releaseSession(session);
}
}
public TestPlan getTestPlanInDomainBy(UUID testPlanRunId) {
TestPlan result = null;
Session session = this.getSessionHelper().openSession();
result = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
.uniqueResult();
if (result == null) {
logger.error("can't find testplan with testPlanRunId : "
+ testPlanRunId.toString() + " in TestPlanRepository");
}
releaseSession(session);
return this.getTestPlanFactory().convertToDomain(result);
}
public TestPlan doGetTestPlanBy(Session session, UUID testPlanRunId) {
return (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
.uniqueResult();
}
public List<TestPlan> loadEntities(User user) {
Session session = this.getSessionHelper().openSession();
try {
@SuppressWarnings("unchecked")
List<TestPlan> ret = session.createCriteria(TestPlan.class)
.add(Restrictions.eq("user", user))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return ret;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
} finally {
releaseSession(session);
}
}
@SuppressWarnings("unchecked")
public List<TestPlan> loadTestPlansBy(List<Criterion> criterions,
Order order) {
Session session = this.getSessionHelper().openSession();
try {
Criteria criteria = session.createCriteria(TestPlan.class);
for (Criterion criterion : criterions) {
criteria.add(criterion);
}
return criteria.addOrder(order).list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return new LinkedList<TestPlan>();
} finally {
releaseSession(session);
}
}
public boolean updateEntity(TestPlan testPlanDB) {
if (testPlanDB == null) {
return false;
}
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
session.update(testPlanDB);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}
public boolean doUpdateEntity(Session session, TestPlan testPlan) {
Transaction transaction = session.beginTransaction();
try {
session.update(testPlan);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
}
}
public TestPlan getRunningTestPlanBy(UUID testPlanRunId) {
return this.getRunningTestPlans().get(testPlanRunId);
}
public void attachRunningTestPlan(TestPlan testPlan) {
this.getRunningTestPlans().put(
UUID.fromString(testPlan.getTestPlanRunId()), testPlan);
}
public void detachRunningTestPlan(UUID testPlanRunId) {
this.getRunningTestPlans().remove(testPlanRunId);
}
@SuppressWarnings("unchecked")
public List<MonitorResult> getMonitorResultsByMonitor(Monitor monitor) {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(MonitorResult.class)
.add(Restrictions.eq("monitor", monitor)).list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
@SuppressWarnings("unchecked")
public List<TestPlanScriptResult> getScriptResultsByTestPlanScript(
TestPlanScript testPlanScript) {
Session session = this.getSessionHelper().openSession();
try {
return session.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.list();
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
}

View File

@ -1,138 +1,150 @@
package org.bench4q.master.domain.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanScriptResultService {
private TestPlanRepository testPlanRepository;
private SessionHelper sessionHelper;
private static Logger logger = Logger
.getLogger(TestPlanScriptResultService.class);
public SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
@Autowired
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
}
public List<ScriptBriefResultModel> loadScriptBriefWithDuation(
UUID testPlanId, int scriptId, long startTime) {
Session session = this.getSessionHelper().openSession();
List<ScriptBriefResultModel> result = new ArrayList<ScriptBriefResultModel>();
try {
List<TestPlanScriptResult> scriptResults = doGetTestPlanScriptResults(
testPlanId, scriptId, startTime,
ScriptBriefResultModel.class, session);
for (TestPlanScriptResult testPlanScriptResult : scriptResults) {
result.add((ScriptBriefResultModel) MarshalHelper.unmarshal(
ScriptBriefResultModel.class,
testPlanScriptResult.getResultContent()));
}
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
}
return result;
}
private List<TestPlanScriptResult> doGetTestPlanScriptResults(
UUID testPlanId, int scriptId, long startTime, Class<?> resultType,
Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.add(Restrictions.eq("resultType", resultType.getName()))
.add(Restrictions.ge("createDatetime", new Date(startTime)))
.list();
return results;
}
public ScriptBehaviorsBriefModel getScriptBehaviorsBrief(UUID testPlanId,
int scriptId) {
Session session = this.getSessionHelper().openSession();
try {
TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult(
testPlanId, scriptId, ScriptBehaviorsBriefModel.class,
session);
if (testPlanScriptResult == null) {
return new ScriptBehaviorsBriefModel();
}
return (ScriptBehaviorsBriefModel) MarshalHelper.unmarshal(
ScriptBehaviorsBriefModel.class,
testPlanScriptResult.getResultContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
}
return new ScriptBehaviorsBriefModel();
}
private TestPlanScriptResult doGetLastSampleResult(UUID testPlanId,
int scriptId, Class<?> resultType, Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.add(Restrictions.eq("resultType", resultType.getName()))
.addOrder(Order.desc("createDatetime")).setFetchSize(1).list();
if (results.size() > 0) {
return results.get(0);
}
return null;
}
public ScriptPagesBriefModel getScriptPagesBrief(UUID testPlanId,
int scriptId) {
Session session = this.getSessionHelper().openSession();
ScriptPagesBriefModel result = new ScriptPagesBriefModel();
try {
TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult(
testPlanId, scriptId, ScriptPagesBriefModel.class, session);
if (testPlanScriptResult == null) {
return result;
}
result = (ScriptPagesBriefModel) MarshalHelper.unmarshal(
ScriptPagesBriefModel.class,
testPlanScriptResult.getResultContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
}
return result;
}
}
package org.bench4q.master.domain.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanScriptResultService {
private TestPlanRepository testPlanRepository;
private SessionHelper sessionHelper;
private static Logger logger = Logger
.getLogger(TestPlanScriptResultService.class);
private SessionHelper getSessionHelper() {
return sessionHelper;
}
@Autowired
private void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
@Autowired
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
}
public List<ScriptBriefResultModel> loadScriptBriefWithDuation(
UUID testPlanId, int scriptId, long startTime) {
Session session = this.getSessionHelper().openSession();
List<ScriptBriefResultModel> result = new ArrayList<ScriptBriefResultModel>();
try {
List<TestPlanScriptResult> scriptResults = doGetTestPlanScriptResults(
testPlanId, scriptId, startTime,
ScriptBriefResultModel.class, session);
for (TestPlanScriptResult testPlanScriptResult : scriptResults) {
result.add((ScriptBriefResultModel) MarshalHelper.unmarshal(
ScriptBriefResultModel.class,
testPlanScriptResult.getResultContent()));
}
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
} finally {
if (session != null) {
session.close();
}
}
return result;
}
private List<TestPlanScriptResult> doGetTestPlanScriptResults(
UUID testPlanId, int scriptId, long startTime, Class<?> resultType,
Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.add(Restrictions.eq("resultType", resultType.getName()))
.add(Restrictions.ge("createDatetime", new Date(startTime)))
.list();
return results;
}
public ScriptBehaviorsBriefModel getScriptBehaviorsBrief(UUID testPlanId,
int scriptId) {
Session session = this.getSessionHelper().openSession();
try {
TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult(
testPlanId, scriptId, ScriptBehaviorsBriefModel.class,
session);
if (testPlanScriptResult == null) {
return new ScriptBehaviorsBriefModel();
}
return (ScriptBehaviorsBriefModel) MarshalHelper.unmarshal(
ScriptBehaviorsBriefModel.class,
testPlanScriptResult.getResultContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
} finally {
if (session != null) {
session.close();
}
}
return new ScriptBehaviorsBriefModel();
}
private TestPlanScriptResult doGetLastSampleResult(UUID testPlanId,
int scriptId, Class<?> resultType, Session session) {
TestPlanScript testPlanScript = this.getTestPlanRepository()
.getTestPlanInDomainBy(testPlanId)
.extracSpecifiedScript(scriptId);
@SuppressWarnings("unchecked")
List<TestPlanScriptResult> results = (List<TestPlanScriptResult>) session
.createCriteria(TestPlanScriptResult.class)
.add(Restrictions.eq("testPlanScript", testPlanScript))
.add(Restrictions.eq("resultType", resultType.getName()))
.addOrder(Order.desc("createDatetime")).setFetchSize(1).list();
if (results.size() > 0) {
return results.get(0);
}
return null;
}
public ScriptPagesBriefModel getScriptPagesBrief(UUID testPlanId,
int scriptId) {
Session session = this.getSessionHelper().openSession();
ScriptPagesBriefModel result = new ScriptPagesBriefModel();
try {
TestPlanScriptResult testPlanScriptResult = doGetLastSampleResult(
testPlanId, scriptId, ScriptPagesBriefModel.class, session);
if (testPlanScriptResult == null) {
return result;
}
result = (ScriptPagesBriefModel) MarshalHelper.unmarshal(
ScriptPagesBriefModel.class,
testPlanScriptResult.getResultContent());
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
} finally {
if (session != null) {
session.close();
}
}
return result;
}
}

View File

@ -45,17 +45,23 @@ public class TestResultSave {
public void update(TestPlan testPlan, List<Object> messages) {
Session session = this.getSessionHelper().openSession();
TestPlan testPlanFromRepo = this.getTestPlanRepository()
.doGetTestPlanBy(session,
UUID.fromString(testPlan.getTestPlanRunId()));
updateTestPlanResult(messages, testPlanFromRepo);
saveUpdatedResult(testPlanFromRepo, session);
session.close();
try {
TestPlan testPlanFromRepo = this.getTestPlanRepository()
.doGetTestPlanBy(session,
UUID.fromString(testPlan.getTestPlanRunId()));
updateTestPlanResult(messages, testPlanFromRepo);
saveUpdatedResult(testPlanFromRepo, session);
} catch (Exception e) {
Logger.getLogger(this.getClass()).info(e, e);
} finally {
if (session != null) {
session.close();
}
}
}
private void updateTestPlanResult(List<Object> messages,
TestPlan testPlanFromRepo) {
for (Object message : messages) {
if (message == null)
return;
@ -98,7 +104,6 @@ public class TestResultSave {
private boolean saveUpdatedResult(TestPlan testPlan, Session session) {
try {
getTestPlanRepository().doUpdateEntity(session, testPlan);
return true;
} catch (Exception e) {