转-项目中用到rabbitmq的地方1之spring定时器

Spring的定时器

1.web.xml文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- spring文件的加载 -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:/spring.xml</param-value>
  14. </context-param>
  15. <listener>
  16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17. </listener>
  18. <!-- struts2文件加载 -->
  19. <filter>
  20. <filter-name>struts2</filter-name>
  21. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  22. </filter>
  23. <filter-mapping>
  24. <filter-name>struts2</filter-name>
  25. <url-pattern>*.do</url-pattern>
  26. </filter-mapping>
  27. <filter-mapping>
  28. <filter-name>struts2</filter-name>
  29. <url-pattern>*.jsp</url-pattern>
  30. </filter-mapping>
  31. <filter>
  32. <filter-name>encodingFilter</filter-name>
  33. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  34. <init-param>
  35. <param-name>encoding</param-name>
  36. <param-value>UTF-8</param-value>
  37. </init-param>
  38. <init-param>
  39. <param-name>forceEncoding</param-name>
  40. <param-value>true</param-value>
  41. </init-param>
  42. </filter>
  43. <filter-mapping>
  44. <filter-name>encodingFilter</filter-name>
  45. <url-pattern>/*</url-pattern>
  46. </filter-mapping>
  47. </web-app>
<?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的核心配置文件

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>"
  3. xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
  4. xmlns:tx="<a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a>"
  5. xmlns:aop="<a href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a>"
  6. xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a>
  7. <a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">http://www.springframework.org/schema/tx/spring-tx-2.5.xsd</a>
  8. <a href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a> <a href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</a>">
  9. <bean id="sessionFactory"
  10. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  11. <property name="configLocation"
  12. value="classpath:hibernate.cfg.xml">
  13. </property>
  14. </bean>
  15. <!--调用service的接口的方法让他定时执行,必须的文件 -->
  16. <bean class="com.guoxin.util.MyApplicationContextUtil"></bean>
  17. <import resource="spring_BlockAction.xml"/>
  18. <import resource="spring_BlockDao.xml"/>
  19. <import resource="spring_BlockService.xml"/>
  20. <import resource="scheduler.xml"/>
  21. </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: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文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  7. <bean id="blockAction"class="com.guoxin.action.BlockAction">
  8. <property name="blockservice">
  9. <ref bean="blockService"/>
  10. </property>
  11. </bean>
  12. </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: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文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="blockDao"class="com.guoxin.dao.GetBlockUrlImp">
  7. <property name="sessionFactory">
  8. <ref bean="sessionFactory"/>
  9. </property>
  10. </bean>
  11. </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"
	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文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  7. <bean id="blockService"class="com.guoxin.service.GetBlockUrlServiceImp">
  8. <property name="blockdao">
  9. <ref bean="blockDao"/>
  10. </property>
  11. </bean>
  12. </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: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文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <include file="Block.xml"></include>
  7. </struts>
<?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文件配置

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constant name="struts.i18n.encoding" value="UTF-8" />
  7. <package name="default"extends="struts-default">
  8. <action name="blockaction"class="blockAction" method="test">
  9. <result name="success">/MyJsp2.jsp</result>
  10. </action>
  11. </package>
  12. </struts>
<?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.主要代码

Java代码 复制代码 收藏代码
  1. package com.guoxin.util;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. publicclass MyApplicationContextUtil implements ApplicationContextAware {
  6. privatestatic ApplicationContext context;
  7. // 声明一个静态变量保存
  8. publicvoid setApplicationContext(ApplicationContext contex) throws BeansException {
  9. context = contex;
  10. }
  11. publicstatic ApplicationContext getContext() {
  12. return context;
  13. }
  14. }
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;
	}
}
 
 
Java代码 复制代码 收藏代码
  1. package com.guoxin.util;
  2. import java.util.List;
  3. import org.quartz.JobExecutionContext;
  4. import org.quartz.JobExecutionException;
  5. import org.springframework.scheduling.quartz.QuartzJobBean;
  6. import com.guoxin.bean.Block;
  7. import com.guoxin.service.Iservice.GetBlockUrlService;
  8. import com.rabbitmq.client.Channel;
  9. import com.rabbitmq.client.Connection;
  10. import com.rabbitmq.client.ConnectionFactory;
  11. import com.rabbitmq.client.MessageProperties;
  12. import com.sun.codemodel.JNullType;
  13. @SuppressWarnings("deprecation")
  14. publicclass DMSMethod extends QuartzJobBean{
  15. private GetBlockUrlService blockservice;//调用service层的接口
  16. public DMSMethod(){
  17. blockservice = (GetBlockUrlService)MyApplicationContextUtil.getContext().getBean("blockService");
  18. }
  19. protectedvoid executeInternal(JobExecutionContext arg0)
  20. throws JobExecutionException {
  21. try {
  22. //获得静态版块的链接
  23. List<Block> staticList=blockservice.queryBlockUrlStatic();
  24. for(Block block:staticList){
  25. String task=block.getBlock_url();
  26. System.out.println("静态:"+task);
  27. }
  28. } catch (Exception e) {
  29. // TODO: handle exception
  30. e.printStackTrace();
  31. }
  32. System.out.println("执行..........");
  33. }
  34. public GetBlockUrlService getBlockservice() {
  35. return blockservice;
  36. }
  37. }
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;
	}
}

Java代码 复制代码 收藏代码
  1. package com.guoxin.service;
  2. import java.util.List;
  3. import com.guoxin.bean.Block;
  4. import com.guoxin.dao.Idao.GetBlockUrl;
  5. import com.guoxin.service.Iservice.GetBlockUrlService;
  6. publicclass GetBlockUrlServiceImp implements GetBlockUrlService {
  7. private GetBlockUrl blockdao;
  8. public List<Block> queryBlockUrlStatic() {
  9. List<Block> list=blockdao.queryBlockUrlStatic();
  10. return list;
  11. }
  12. public List<Block> queryBlockUrlTrends() {
  13. List<Block> list=blockdao.queryBlockUrlStatic();
  14. return list;
  15. }
  16. public GetBlockUrl getBlockdao() {
  17. return blockdao;
  18. }
  19. publicvoid setBlockdao(GetBlockUrl blockdao) {
  20. this.blockdao = blockdao;
  21. }
  22. }

猜你喜欢

转载自chenhaiyang.iteye.com/blog/1770085
今日推荐