about 8 years ago
排程在專案中常常會需要使用到,但是一般排程他是透過其他的Thread來計時跟控管,所以你沒辦法拿到Spring中既有的資源,你的排程程式也必須繼承Job,所以排程這隻就只能for排程使用了。
但是如果你透過下面方式來設定,你的程式就可以不用繼承Job,又可取得Spring的資源,開發上會省事點
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="CloudPush" class="com.secom.mobile.scheduler.CloudPush" />
<!-- specifing class and method that is going to be called on a specified
time basis -->
<bean id="myJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="CloudPush" />
<property name="targetMethod" value="start" />
</bean>
<!-- simple trigger specify repeat interval and delay time -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="myJob" />
<property name="repeatInterval" value="60000" /><!-- 毫秒 -->
<property name="startDelay" value="300000" />
</bean>
<!-- scheduler factory bean to bind,the executing code and time intervals
together -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="myJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
public class CloudPush {
private Logger logger = LoggerFactory.getLogger("system");
@Value("${pushserver.url}")
private String pushserverurl;
@Autowired
private UsercloudService usercloudService;
public void start(){
//只要是公開的一般方法就可以執行了
logger.info("Push scheduled start");
}
}