/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.ngrinder.agent.service;

import java.util.Set;

import net.grinder.console.communication.AgentProcessControlImplementation;
import net.grinder.console.communication.AgentProcessControlImplementation.AgentStatus;

import org.ngrinder.extension.OnPeriodicWorkingAgentCheckRunnable;
import org.ngrinder.infra.plugin.PluginManager;
import org.ngrinder.perftest.service.AgentManager;
import org.python.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Agent periodic check service.
 * 
 * This class runs the plugins implementing
 * {@link OnPeriodicWorkingAgentCheckRunnable}.
 * 
 * It's separated from {@link AgentManagerService} to get rid of cyclic
 * injection.
 * 
 * @author JunHo Yoon
 * @since 3.1.2
 */
@Service
public class PeriodicWorkingAgentCheckService {
	@Autowired
	private PluginManager pluginManager;

	@Autowired
	private AgentManager agentManager;

	/**
	 * Run scheduled tasks checking the agent status on the currently working
	 * agents.
	 * 
	 * @since 3.1.2
	 */
	@Scheduled(fixedDelay = 2000)
	@Transactional
	public void checkWorkingAgents() {
		Set<AgentStatus> workingAgents = agentManager
				.getAgentStatusSet(new Predicate<AgentProcessControlImplementation.AgentStatus>() {
					@Override
					public boolean apply(AgentStatus agentStatus) {
						return agentStatus.getConnectingPort() != 0;
					}
				});
		for (OnPeriodicWorkingAgentCheckRunnable runnable : pluginManager
				.getEnabledModulesByClass(OnPeriodicWorkingAgentCheckRunnable.class)) {
			runnable.checkWorkingAgent(workingAgents);
		}

	}
}

@@ -892,32 +892,89 @@
 .PluginManager;%0A
+import org.ngrinder.infra.schedule.ScheduledTaskService;%0A
 import org.ngrin
@@ -1000,32 +1000,32 @@
 e.AgentManager;%0A
-
 import org.pytho
@@ -1292,16 +1292,56 @@
 ional;%0A%0A
+import javax.annotation.PostConstruct;%0A%0A
 /**%0A * A
@@ -1368,24 +1368,28 @@
 service.%0A * 
+%3Cp/%3E
 %0A * This cla
@@ -1468,24 +1468,28 @@
 nnable%7D.%0A * 
+%3Cp/%3E
 %0A * It's sep
@@ -1565,17 +1565,16 @@
 tion.%0A *
- 
 %0A * @aut
@@ -1663,16 +1663,36 @@
 Service 
+implements Runnable 
 %7B%0A%09@Auto
@@ -1790,29 +1790,37 @@
 ;%0A%0A%09
-/**%0A%09 * Run s
+@Autowired%0A%09private S
 cheduled
  tas
@@ -1819,183 +1819,197 @@
 uled
- tasks checking the agent status on the currently working%0A%09 * agents.%0A%09 * %0A%09 * @since 3.1.2%0A%09 */%0A%09@Scheduled(fixedDelay = 2000)%0A%09@Transactional%0A%09public void checkWorkingAgents
+TaskService scheduledTaskService;%0A%0A%09@PostConstruct%0A%09public void init() %7B%0A%09%09scheduledTaskService.addFixedDelayedScheduledTaskInTransactionContext(this, 2000);%0A%09%7D%0A%0A%09@Override%0A%09public void run
 () %7B
@@ -2473,13 +2473,12 @@
 s);%0A
+
 %09%09%7D%0A
-%0A
 %09%7D%0A%7D

remove unnecessary test failure