Spring的定时器

1.web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- spring文件的加载 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:/spring.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- struts2文件加载 -->
	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	 </filter>
	 <filter-mapping>
	  <filter-name>struts2</filter-name>
	  <url-pattern>*.do</url-pattern>
	 </filter-mapping>
	 <filter-mapping>
		  <filter-name>struts2</filter-name>
		  <url-pattern>*.jsp</url-pattern>
	  </filter-mapping>
	  
	   <filter>
		  <filter-name>encodingFilter</filter-name>
		  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		  <init-param>
		   <param-name>encoding</param-name>
		   <param-value>UTF-8</param-value>
		  </init-param>
		  <init-param>
		   <param-name>forceEncoding</param-name>
		   <param-value>true</param-value>
		  </init-param>
		 </filter>
		 <filter-mapping>
		  <filter-name>encodingFilter</filter-name>
		  <url-pattern>/*</url-pattern>
		 </filter-mapping>
</web-app>

 

2.spring.xml文件配置也就是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"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="configLocation"
   value="classpath:hibernate.cfg.xml">
  </property>
 </bean>
 <!--调用service的接口的方法让他定时执行,必须的文件  -->
 <bean class="com.guoxin.util.MyApplicationContextUtil"></bean>
    <import resource="spring_BlockAction.xml"/>
 <import resource="spring_BlockDao.xml"/>
 <import resource="spring_BlockService.xml"/>
    <import resource="scheduler.xml"/>
</beans>

 3.spring_BlockAction.xml文件配置

<?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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="blockAction" class="com.guoxin.action.BlockAction">
		<property name="blockservice">
			<ref bean="blockService"/>
		</property>
	</bean>
</beans>

 

4.spring_BlockDao.xml文件配置

<?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-2.5.xsd">
<bean id="blockDao" class="com.guoxin.dao.GetBlockUrlImp">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
</beans>

 

5.spring_BlockService.xml文件配置

<?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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="blockService" class="com.guoxin.service.GetBlockUrlServiceImp">
		<property name="blockdao">
			<ref bean="blockDao"/>
		</property>
	</bean>
</beans>

 

6.struts.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<include file="Block.xml"></include>
</struts>

 

7.Block.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="default" extends="struts-default">
		<action name="blockaction" class="blockAction" method="test">
			<result name="success">/MyJsp2.jsp</result>
		</action>
	</package>
</struts>

 

8.主要代码

package com.guoxin.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MyApplicationContextUtil implements ApplicationContextAware {

	private static ApplicationContext context;

	// 声明一个静态变量保存
	public void setApplicationContext(ApplicationContext contex) throws BeansException {
		context = contex;
	}

	public static ApplicationContext getContext() {
		return context;
	}
}
 
 

 

package com.guoxin.util;

import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.guoxin.bean.Block;
import com.guoxin.service.Iservice.GetBlockUrlService;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.sun.codemodel.JNullType;

@SuppressWarnings("deprecation")
public class DMSMethod extends QuartzJobBean{
	private GetBlockUrlService blockservice;//调用service层的接口
	
	public DMSMethod(){
	   blockservice = (GetBlockUrlService)MyApplicationContextUtil.getContext().getBean("blockService");
	}


	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		try {
    			//获得静态版块的链接
			List<Block> staticList=blockservice.queryBlockUrlStatic();
			for(Block block:staticList){
		    	  String task=block.getBlock_url();
		       	  System.out.println("静态:"+task);
		    	}
			    	} catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
		System.out.println("执行..........");
		
	}

	public GetBlockUrlService getBlockservice() {
		return blockservice;
	}
}

 

package com.guoxin.service;

import java.util.List;

import com.guoxin.bean.Block;
import com.guoxin.dao.Idao.GetBlockUrl;
import com.guoxin.service.Iservice.GetBlockUrlService;

public class GetBlockUrlServiceImp implements GetBlockUrlService {
    private GetBlockUrl  blockdao;
	public List<Block> queryBlockUrlStatic() {
		List<Block> list=blockdao.queryBlockUrlStatic();
		return list;
	}
	public List<Block> queryBlockUrlTrends() {
		List<Block> list=blockdao.queryBlockUrlStatic();
		return list;
	}
	public GetBlockUrl getBlockdao() {
		return blockdao;
	}
	public void setBlockdao(GetBlockUrl blockdao) {
		this.blockdao = blockdao;
	}

}

 

你可能感兴趣的:(Spring的定时器)