diff --git a/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java b/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java index 1844a7cf..3f673b1c 100644 --- a/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java +++ b/src/main/java/org/bench4q/master/domain/repository/TestPlanRepository.java @@ -73,13 +73,6 @@ public class TestPlanRepository extends AbstractRepositoty { @SuppressWarnings("unchecked") List ret = session.createCriteria(TestPlanDB.class) .add(Restrictions.eq("user", user)).list(); - // @SuppressWarnings("unchecked") - // List ret3 = session.createCriteria(TestPlanDB.class) - // .list(); - // @SuppressWarnings("unchecked") - // List ret2 = session.createSQLQuery( - // "select * from testPlan where userId = " + user.getId()) - // .list(); return ret; } catch (Exception e) { logger.error(ExceptionLog.getStackTrace(e)); diff --git a/src/main/java/org/bench4q/master/domain/service/AgentService.java b/src/main/java/org/bench4q/master/domain/service/AgentService.java index 26b2d053..39a8f1c1 100644 --- a/src/main/java/org/bench4q/master/domain/service/AgentService.java +++ b/src/main/java/org/bench4q/master/domain/service/AgentService.java @@ -2,14 +2,9 @@ package org.bench4q.master.domain.service; import java.util.List; -import org.apache.log4j.Logger; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.domain.repository.AgentRepository; -import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.testplan.highavailable.HighAvailablePool; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -19,25 +14,14 @@ public class AgentService { public static int AGENT_STATUS_InRun = 2; public static int AGENT_STATUS_BackUp = 3; public static int AGENT_STATUS_BreakDown = 4; - private SessionHelper sessionHelper; private AgentRepository agentRepository; private Object AGENT_LOCK; private HighAvailablePool highAvailablePool; - private static Logger logger = Logger.getLogger(AgentService.class); public AgentService() { this.setAgentLock(new Object()); } - private SessionHelper getSessionHelper() { - return sessionHelper; - } - - @Autowired - private void setSessionHelper(SessionHelper sessionHelper) { - this.sessionHelper = sessionHelper; - } - private HighAvailablePool getHighAvailablePool() { return highAvailablePool; } @@ -88,29 +72,15 @@ public class AgentService { } public boolean backLoadToAgent(String hostName, int backLoad) { - Session session = this.getSessionHelper().openSession(); - Transaction transaction = session.beginTransaction(); - try { - Agent agent = (Agent) session.createCriteria(Agent.class) - .add(Restrictions.eq("hostName", hostName)).uniqueResult(); - if (!isValidBackLoad(agent, backLoad) || agent == null) { - return false; - } - agent.setRemainLoad(agent.getRemainLoad() + backLoad); - session.merge(agent); - transaction.commit(); - logger.info("The agent with hostname " + agent.getHostName() - + " remainload is " + agent.getRemainLoad()); - return true; - } catch (Exception e) { - e.printStackTrace(); - transaction.rollback(); + Agent agent = this.getAgentRepository().getAgentBy(hostName); + if (agent == null) { return false; - } finally { - if (session != null) { - session.close(); - } } + if (!isValidBackLoad(agent, backLoad)) { + return false; + } + agent.setRemainLoad(agent.getRemainLoad() + backLoad); + return this.getAgentRepository().update(agent); } private boolean isValidBackLoad(Agent agentToOperate, int backLoad) { @@ -120,7 +90,7 @@ public class AgentService { public boolean getLoadFromAgent(String hostName, int requireLoad) { Agent agent = this.getAgentRepository().getAgentBy(hostName); - if (agent == null || !isValidRequireLoad(agent, requireLoad)) { + if (!isValidRequireLoad(agent, requireLoad) || agent == null) { return false; } agent.setRemainLoad(agent.getRemainLoad() - requireLoad); @@ -133,54 +103,22 @@ public class AgentService { } public boolean resetAgent(Agent agentInParam) { - Session session = this.getSessionHelper().openSession(); - Transaction transaction = session.beginTransaction(); - try { - Agent agentFromDB = (Agent) session - .createCriteria(Agent.class) - .add(Restrictions.eq("hostName", agentInParam.getHostName())) - .uniqueResult(); - if (agentFromDB == null) { - return false; - } - - agentFromDB.setRemainLoad(agentFromDB.getMaxLoad()); - session.merge(agentFromDB); - logger.info("agent with hostname " + agentFromDB.getHostName() - + "remain load is " + agentFromDB.getRemainLoad()); - transaction.commit(); - return true; - } catch (Exception e) { - transaction.rollback(); + Agent agentInRepo = this.getAgentRepository().getAgentBy( + agentInParam.getHostName()); + if (agentInRepo == null) { return false; - } finally { - if (session != null) { - session.close(); - } } + agentInRepo.setRemainLoad(agentInRepo.getMaxLoad()); + return this.getAgentRepository().update(agentInRepo); } public boolean updateAgentStatus(int currentStatus, String hostName) { - Session session = this.getSessionHelper().openSession(); - Transaction transaction = session.beginTransaction(); - try { - Agent agentFromDB = (Agent) session.createCriteria(Agent.class) - .add(Restrictions.eq("hostName", hostName)).uniqueResult(); - if (agentFromDB == null) { - return false; - } - agentFromDB.setCurrentStatus(currentStatus); - session.merge(agentFromDB); - transaction.commit(); - return true; - } catch (Exception e) { - transaction.rollback(); + Agent agent = this.getAgentRepository().getAgentBy(hostName); + if (agent == null) { return false; - } finally { - if (session != null) { - session.close(); - } } + agent.setCurrentStatus(currentStatus); + return this.getAgentRepository().update(agent); } } diff --git a/src/test/java/org/bench4q/master/test/service/Test_AgentService.java b/src/test/java/org/bench4q/master/test/service/Test_AgentService.java index e53b1cc2..894b9115 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_AgentService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_AgentService.java @@ -152,4 +152,24 @@ public class Test_AgentService { getAgentForTest().getMaxLoad() - getAgentForTest().getRemainLoad()); } + + @Test + public void testResetAgent() { + assertTrue(this.getAgentService().getLoadFromAgent(hostNameForTest, + getAgentForTest().getRemainLoad() - 10)); + assertEquals(10, getAgentForTest().getRemainLoad()); + assertTrue(this.getAgentService().resetAgent(getAgentForTest())); + assertEquals(getAgentForTest().getMaxLoad(), getAgentForTest() + .getRemainLoad()); + } + + @Test + public void testUpdateAgentStatus() { + assertEquals(AgentService.AGENT_STATUS_Idel, getAgentForTest() + .getCurrentStatus()); + assertTrue(this.getAgentService().updateAgentStatus( + AgentService.AGENT_STATUS_BreakDown, hostNameForTest)); + assertEquals(AgentService.AGENT_STATUS_BreakDown, getAgentForTest() + .getCurrentStatus()); + } }