Преглед изворни кода

定时20分钟检测是否有异常和掉线的设备

seyason пре 5 година
родитељ
комит
54329241c1

+ 58 - 54
cloud-socket/src/applicationContext.xml

@@ -1,54 +1,58 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xmlns:p="http://www.springframework.org/schema/p"
-	xmlns:context="http://www.springframework.org/schema/context"
-	xmlns:tx="http://www.springframework.org/schema/tx"
-	xmlns:task="http://www.springframework.org/schema/task"
-	xsi:schemaLocation="
-	http://www.springframework.org/schema/beans 
-	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
-	http://www.springframework.org/schema/tx 
-	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
-	http://www.springframework.org/schema/context 
-	http://www.springframework.org/schema/context/spring-context-3.0.xsd
-	http://www.springframework.org/schema/task 
-	http://www.springframework.org/schema/task/spring-task.xsd">
-	
-	<!-- 引入jdbc配置文件 -->
-	<context:property-placeholder location="classpath:jdbc.properties" />
-
-	<!--创建jdbc数据源 -->
-	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
-		destroy-method="close">
-		<property name="driverClassName" value="${driver}" />
-		<property name="url" value="${url}" />
-		<property name="username" value="${username}" />
-		<property name="password" value="${password}" />
-	</bean>
-
-	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
-	<bean id="transactionManager"
-		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
-		<property name="dataSource" ref="dataSource" />
-	</bean>
-
-	<!-- 创建SqlSessionFactory,同时指定数据源 -->
-	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
-		<property name="dataSource" ref="dataSource" />
-	</bean>
-	<!-- 可通过注解控制事务 -->
-	<tx:annotation-driven />
-
-	<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
-	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
-		<property name="basePackage" value="com.js.kbt.mapper" />
-	</bean>
-  	<context:component-scan
-		base-package="com.js.kbt.task" /> 
-  	<context:component-scan base-package="com.js.kbt.socket"/>
-	
-  <bean id="helloServer" class="com.js.kbt.socket.HelloServer" scope="singleton" init-method="startServer" destroy-method="shutdown"/>
-<!--   <bean id="nioServer" class="com.js.kbt.socket.NIOServer" scope="singleton" init-method="startServer" destroy-method="shutdown"/> -->
-  <bean id="userHandler" class="com.js.kbt.socket.UserHandler" scope="prototype"/> 
-</beans>
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xmlns:p="http://www.springframework.org/schema/p"
+	xmlns:context="http://www.springframework.org/schema/context"
+	xmlns:tx="http://www.springframework.org/schema/tx"
+	xmlns:task="http://www.springframework.org/schema/task"
+	xsi:schemaLocation="
+	http://www.springframework.org/schema/beans 
+	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+	http://www.springframework.org/schema/tx 
+	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
+	http://www.springframework.org/schema/context 
+	http://www.springframework.org/schema/context/spring-context-3.0.xsd
+	http://www.springframework.org/schema/task 
+	http://www.springframework.org/schema/task/spring-task.xsd">
+	
+	<!-- 引入jdbc配置文件 -->
+	<context:property-placeholder location="classpath:jdbc.properties" />
+
+	<!--创建jdbc数据源 -->
+	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
+		destroy-method="close">
+		<property name="driverClassName" value="${driver}" />
+		<property name="url" value="${url}" />
+		<property name="username" value="${username}" />
+		<property name="password" value="${password}" />
+	</bean>
+
+	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
+	<bean id="transactionManager"
+		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+		<property name="dataSource" ref="dataSource" />
+	</bean>
+
+	<!-- 创建SqlSessionFactory,同时指定数据源 -->
+	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
+		<property name="dataSource" ref="dataSource" />
+	</bean>
+	<!-- 可通过注解控制事务 -->
+	<tx:annotation-driven />
+
+	<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
+	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
+		<property name="basePackage" value="com.js.kbt.mapper" />
+	</bean>
+  	<context:component-scan base-package="com.js.kbt.socket"/>
+<!--   	<context:component-scan base-package="com.js.kbt.task"/> -->
+  	<!-- 定时任务启动 -->
+	<task:annotation-driven />
+<!-- 	//进行定时任务的类,将其定义为一个bean -->
+    <bean id="myTask" class="com.js.kbt.task.MyTask"></bean>
+    <task:scheduled-tasks>
+        <task:scheduled ref="myTask" method="doTask" fixed-rate="1200000" />
+    </task:scheduled-tasks>
+  	<bean id="helloServer" class="com.js.kbt.socket.HelloServer" scope="singleton" init-method="startServer" destroy-method="shutdown"/>
+  	<bean id="userHandler" class="com.js.kbt.socket.UserHandler" scope="prototype"/> 
+</beans>

+ 45 - 45
cloud-socket/src/com/js/kbt/task/MyTask.java

@@ -1,45 +1,45 @@
-package com.js.kbt.task;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-import javax.annotation.Resource;
-
-import org.apache.log4j.Logger;
-import org.springframework.scheduling.annotation.Scheduled;
-import org.springframework.stereotype.Service;
-
-import com.js.kbt.mapper.TbMouldMapper;
-
-@Service
-public class MyTask {
-	
-	@Resource(name = "tbMouldMapper")
-	private TbMouldMapper tbMouldMapper;
-
-	Logger log = Logger.getLogger(MyTask.class);
-
-	@Scheduled(fixedRate = 1 * 10 * 1000)
-	// @Scheduled(fixedRate = 1*60*1000)
-	public void doTask() throws Exception {
-		Date now = new Date();
-		log.info("定时任务:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
-		//规则是, 运动状态下,上次接受,超过1个小时,不到6个小时的模具设备,状态设置为5-异常,6小时以上的,状态设置为掉线
-		/**now.setHours(now.getHours() -1);
-		TbMouldExample example = new TbMouldExample();
-		example.createCriteria().andStateEqualTo("1").andLastRecTimeLessThanOrEqualTo(now);
-		TbMould mould = new TbMould();
-		mould.setState("5");//超过1小时仍然是运行状态的,要改成异常。
-		tbMouldMapper.updateByExampleSelective(mould, example);
-		
-		//超过6小时,仍然是异常状态的,改成掉线
-		now = new Date();
-		now.setHours(now.getHours() - 6);
-		example.clear();
-		example.createCriteria().andStateEqualTo("5").andLastRecTimeLessThanOrEqualTo(now);
-		mould.setState("6");
-		tbMouldMapper.updateByExampleSelective(mould, example);**/
-		
-	}
-
-}
+package com.js.kbt.task;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.annotation.Resource;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Service;
+
+import com.js.kbt.mapper.TbMouldMapper;
+import com.js.kbt.model.TbMould;
+import com.js.kbt.model.TbMouldExample;
+
+@Service
+public class MyTask {
+	
+	@Resource(name = "tbMouldMapper")
+	private TbMouldMapper tbMouldMapper;
+
+	Logger log = Logger.getLogger(MyTask.class);
+
+	// @Scheduled(fixedRate = 1*60*1000)标签不能用,会导致Bean定义冲突报错
+	public void doTask() throws Exception {
+		Date now = new Date();
+		log.info("定时任务:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now));
+		//规则是, 运动状态下,上次接受,超过1个小时,不到6个小时的模具设备,状态设置为5-异常,6小时以上的,状态设置为掉线
+		now.setHours(now.getHours() -1);
+		TbMouldExample example = new TbMouldExample();
+		example.createCriteria().andStateEqualTo("1").andLastRecTimeLessThanOrEqualTo(now);
+		TbMould mould = new TbMould();
+		mould.setState("5");//超过1小时仍然是运行状态的,要改成异常。
+		int cnt = tbMouldMapper.updateByExampleSelective(mould, example);
+		log.info("更新了"+cnt+"条异常");
+		//超过6小时,仍然是异常状态的,改成掉线
+		now = new Date();
+		now.setHours(now.getHours() - 6);
+		example.clear();
+		example.createCriteria().andStateEqualTo("5").andLastRecTimeLessThanOrEqualTo(now);
+		mould.setState("6");
+		cnt = tbMouldMapper.updateByExampleSelective(mould, example);
+		log.info("更新了"+cnt+"条掉线");
+	}
+
+}

+ 34 - 34
cloud-socket/src/context-dispatcher.xml

@@ -1,35 +1,35 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans default-lazy-init="true"
-	xmlns="http://www.springframework.org/schema/beans"
-	xmlns:p="http://www.springframework.org/schema/p"
-	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xmlns:context="http://www.springframework.org/schema/context"
-	xmlns:mvc="http://www.springframework.org/schema/mvc"
-	xsi:schemaLocation="  
-       http://www.springframework.org/schema/beans   
-       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
-       http://www.springframework.org/schema/mvc   
-       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
-       http://www.springframework.org/schema/context  
-       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
-	<!-- 使用注解的包,包括子集 -->
-	<context:component-scan base-package="com.js.kbt" />
-	<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
-<!-- 	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
-	<mvc:annotation-driven /> 
-	<!-- 视图解析器 -->
-	<bean id="viewResolver"
-		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-		<property name="viewClass"
-			value="org.springframework.web.servlet.view.JstlView" />
-<!-- 		<property name="prefix" value="/pages/" /> -->
-		<property name="suffix" value=".jsp"></property>
-	</bean>
-
-	<bean id="multipartResolver"
-		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
-		p:defaultEncoding="UTF-8" >
-		<property name="maxUploadSize" value="10000000" /><!-- 10M --> 
-	</bean>
+<?xml version="1.0" encoding="UTF-8"?>
+<beans default-lazy-init="true"
+	xmlns="http://www.springframework.org/schema/beans"
+	xmlns:p="http://www.springframework.org/schema/p"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xmlns:context="http://www.springframework.org/schema/context"
+	xmlns:mvc="http://www.springframework.org/schema/mvc"
+	xsi:schemaLocation="  
+       http://www.springframework.org/schema/beans   
+       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
+       http://www.springframework.org/schema/mvc   
+       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
+       http://www.springframework.org/schema/context  
+       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+	<!-- 使用注解的包,包括子集 -->
+	<context:component-scan base-package="com.js.kbt" />
+	<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
+<!-- 	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
+	<mvc:annotation-driven /> 
+	<!-- 视图解析器 -->
+	<bean id="viewResolver"
+		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
+		<property name="viewClass"
+			value="org.springframework.web.servlet.view.JstlView" />
+<!-- 		<property name="prefix" value="/pages/" /> -->
+		<property name="suffix" value=".jsp"></property>
+	</bean>
+
+	<bean id="multipartResolver"
+		class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
+		p:defaultEncoding="UTF-8" >
+		<property name="maxUploadSize" value="10000000" /><!-- 10M --> 
+	</bean>
 </beans>