我正在开发一个网站,负责提供有关在线电台的信息。web服务器本身从不同的来源获取信息(有一个由API提供的数据库,我的流媒体服务器也提供了一些XML文件,我用它来动态提取几个重要的信息)。
好吧,我的问题开始时,我决定创建基于看门狗线程的观众警报,从流服务器读取XML文件,更新具有固定长度的数组(360个地方的长数组,预分配,所以没有那么大),并通过一个动作访问,它返回这个数组,通过java脚本例程在图中转换。因此,这个看门狗线程对于访问web站点的每个客户机都需要相同,因为它提供的信息需要对每个客户机都是相同的。因此,我决定使用spring框架来调用启动Watchdog线程的服务。
它工作,每个网站看到相同的信息和信息总是更新…直到我创建的while true线程停止,数组的最后状态被保留,但是线程不再工作。
弹簧注入定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:sec="http://www.springframework.org/schema/security" xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
default-autowire="byName"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
<bean id="wowzaService" class="br.com.imusica.reports.services.radio.WowzaService"/>
</beans>
服务(通过操作访问)按如下方式编程:
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class WowzaService extends AbstractService {
@Autowired
private WatchDogThread watchdog;
public void startWatchdog() {
watchdog.start();
}
public WatchDogArray getWatchdogArray() {
return watchdog.getWatchDogArray();
}
}
这个服务被注入到ApplicationContextInitializer类中,如下所示:
public synchronized void contextInitialized(ServletContextEvent contextEvent) {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(contextEvent.getServletContext());
WowzaService ws = (WowzaService) wac.getBean("wowzaService");
ws.startWatchdog();
CacheManager manager = (CacheManager) wac.getBean("cacheManager");
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(manager, mBeanServer, false, false, false, true);
cleanUpService = (CleanUpService) wac.getBean("cleanUpService");
ApplicationContextInitializer.applicationDiskRootPath = contextEvent.getServletContext().getRealPath("/");
ComplexReportsControllerService complexReportsService =
(ComplexReportsControllerService) wac.getBean("complexReportsControllerService");
complexReportsService.cleanUpUnfinishedReports();
cleanUpService.setRootPath(ApplicationContextInitializer.applicationDiskRootPath);
cleanUpService.cleanUpAllSessions();
// StdScheduler schedulerFactoryBean;
ApplicationContextInitializer.reportsBasePath = (String) wac.getBean("reportsDataPath");
}
看门狗线程被定义为一个组件,它有一个@Scope(BeanDefinition.SCOPE_SINGLETON),扩展线程,等等。我想知道春天是否会在一段时间后停止这条线。我确信,当我更新我的数据数组时,我没有附加信息,而是实际移动值,丢弃最旧的值并插入新值。因此,内存问题没有发生。
提前感谢您的帮助
Joao布鲁诺Spring不能停止你的线程,你的线程由于某种原因失败了