`

转-项目中用到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. }
分享到:
评论

相关推荐

    Java开发案例-springboot-06-整合RabbitMQ-源代码+文档.rar

    Java开发案例-springboot-06-整合RabbitMQ-源代码+文档.rar Java开发案例-springboot-06-整合RabbitMQ-源代码+文档.rar Java开发案例-springboot-06-整合RabbitMQ-源代码+文档.rar Java开发案例-springboot-06-整合...

    spring-boot-mq-rabbitmq 一套打通rabbitmq 打开可用 有注释

    spring.rabbitmq.host=your-rabbitmq-host spring.rabbitmq.port=your-rabbitmq-port spring.rabbitmq.username=your-username spring.rabbitmq.password=your-password ``` 或者使用YAML格式: ```yaml # ...

    rabbitmq-server-3.11.13

    rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-3.11.13rabbitmq-server-...

    RabbitMq与Spring整合实例

    1. **配置依赖**:在项目的`pom.xml`文件中添加RabbitMQ的Spring整合依赖。如: ```xml &lt;groupId&gt;org.springframework.amqp &lt;artifactId&gt;spring-rabbit &lt;version&gt;2.3.7 ``` 确保版本号与你的Spring框架版本兼容...

    rabbitmq-c-master.rar_RabbitMQ c lib_cmake编译_rabbitmq_rabbitmq-c

    1. **获取源码**:首先,从官方仓库或者提供的压缩包中下载`rabbitmq-c-master`源代码。 2. **创建构建目录**:为了保持源代码的整洁,我们通常在源代码目录外创建一个新目录,例如`build`,用于放置编译生成的文件...

    rabbitmq 3.6.5-1离线安装

    rpm -ivh rabbitmq-server-3.6.5-1.noarch.rpm systemctl status rabbitmq-server systemctl start rabbitmq-server systemctl stop rabbitmq-server systemctl restart rabbitmq-server systemctl enable ...

    RabbitMQ-c源码

    **RabbitMQ-c源码分析** RabbitMQ-c是一个轻量级且高效的C语言实现的RabbitMQ客户端库。RabbitMQ是一个开源的消息代理和队列服务器,它使用AMQP(Advanced Message Queuing Protocol)协议,广泛应用于分布式系统中...

    158-最简单的 RabbitMQ 监控方法1

    在安装了RabbitMQ的服务器上执行`rabbitmq-plugins enable rabbitmq_management`命令即可启用。接着,需要创建一个用户,例如`user_admin`,并设置相应的权限,以便登录管理控制台。 一旦登录Web控制台,最需要注意...

    rabbitmq spring demo

    rabbitmq spring rabbitmq spring rabbitmq spring rabbitmq spring http://knight-black-bob.iteye.com/blog/2304089

    rabbitmq-server-3.5.4.tar.gz

    1. **下载**:首先,你需要从官方网站下载适合你的Linux发行版的RabbitMQ版本,如"rabbitmq-server-generic-unix-3.5.4.tar.gz"。你可以使用wget命令或者直接在浏览器中下载。 2. **解压**:使用tar命令解压下载的...

    rabbitmq-server-3.7.3百度网盘下载.txt

    这里提供了rabbitmq-server-3.7.3.exe百度网盘下载,官网下载实在是太慢了,亲测有效! rabbitmq-server-3.7.3.exe rabbitmq-server-3.7.3.exe rabbitmq-server-3.7.3.exe

    spring-cloud-steam-rabbitmq-demo.zip

    在“spring-cloud-steam-rabbitmq-demo”项目中,我们将了解以下几个关键知识点: 1. **配置Spring Cloud Stream**: 首先,我们需要在`application.yml`或`application.properties`中配置RabbitMQ的相关信息,...

    rabbitmq-server-generic-unix-3.6.1.tar

    标题中的“rabbitmq-server-generic-unix-3.6.1.tar”是指RabbitMQ服务器的3.6.1版本的通用Unix安装包。RabbitMQ是一个开源的消息代理和队列服务器,广泛应用于分布式系统中,用于处理异步任务、消息传递和负载均衡...

    rabbitmq4.0.2离线安装

    rpm -ivh rabbitmq-server-4.0.2-1.el8.noarch.rpm systemctl status rabbitmq-server systemctl start rabbitmq-server systemctl stop rabbitmq-server systemctl restart rabbitmq-server systemctl enable ...

    java队列源码-rabbitmq-repository:RabbitMQ消息队列学习的源码记录

    rabbitmq-spring-topic-exchange rabbitmq主题示例 rabbitmq-java-rpc rabbitmq PRC通信示例 rabbitmq-spring-helloworld spring boot 使用rabbitmq的第一个demo rabbitmq-spring-work-queue spring boot使用...

    RabbitMQ快速入门及API介绍(401M)

    【课程目录】:---第一章:RabbitMQ介绍----1-什么是消息中间件.mp4----2-RabbitMQ消息队列安装:window环境.mp4----3-RabbitMQ消息队列安装 :Linux环境.mp4----4-Rabbitmq入口示例:server.mp4----5-rabbitmq入口...

    rabbitmq-server-3.6.1

    rabbitmq-server-3.6.1 的安装程序,rabbitmq-server-3.6.1 安装 rabbitmq-server

    最新版windows rabbitmq-server-3.8.5.zip

    2. **安装与配置**:`rabbitmq-server-3.8.5.exe`是RabbitMQ Windows版的安装程序,用户可以通过这个可执行文件进行安装。安装过程中会涉及服务启动设置、端口配置(默认5672用于AMQP,15672用于管理界面)、环境...

    windows服务----基于RabbitMQ

    为了与RabbitMQ进行交互,我们需要使用RabbitMQ的C#客户端库,这可以通过NuGet包管理器添加到项目中。该库提供了创建连接、通道、声明交换机和队列,以及接收和发送消息的功能。在这个场景中,服务会在启动时建立...

    docker-compose部署单机rabbitmq

    docker-compose部署单机rabbitmq,一键启动

Global site tag (gtag.js) - Google Analytics