【Spring】利用Spring最简单地使用异步方法
有时候我们想异步地调用某个方法。
比如这个场景:在业务处理完毕后,需给用户发送通知邮件。由于邮件发送需调用邮箱服务商,有可能发生阻塞,我们就可以异步调用。当然有个前提,即如果邮件发送失败,不需要提示用户的。
> 版本说明
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.14.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
</dependencies>
> 一个简单的异步方法
package service; import java.util.concurrent.TimeUnit; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; @Service
public class BusinessService { @Async
public void doBusiness() throws InterruptedException {
System.out.println("start to do business.");
TimeUnit.SECONDS.sleep(5);
System.out.println("end to do business.");
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 扫描指定包下的组件 -->
<context:component-scan base-package="service"/> <!-- 支持异步方法 -->
<task:annotation-driven/> </beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import service.BusinessService; public class HowToTest { public static void main(String[] args) throws InterruptedException {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
BusinessService s = context.getBean("businessService", BusinessService.class); s.doBusiness();
System.out.println("HowToTest completed."); } }
日志是这样的
HowToTest completed.
start to do business.
end to do business.
【Spring】利用Spring最简单地使用异步方法的更多相关文章
- spring:利用Spring AOP 使日志输入与方法分离
对方法进行日志输出是一种很常见的功能.传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行! 1.先写一个普通类: package com.importnew; ...
- 利用Spring Boot+zxing,生成二维码还能这么简单
在网站开发中,经常会遇到要生成二维码的情况,比如要使用微信支付.网页登录等,本文分享一个Spring Boot生成二维码的例子,这里用到了google的zxing工具类. 本文目录 一.二维码简介二. ...
- 利用spring boot创建java app
利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
java 利用spring JavaMailSenderImpl发送邮件,支持普通文本.附件.html.velocity模板 博客分类: Java Spring 本文主要介绍利用JavaMailS ...
- 利用Spring创建定时任务
啊Spring Task看似很简单的感觉,但是自己搞起来还是花了蛮大的精力的,因为以前没接触过这个东西,所有当任务交给我的时候,我是一头的雾水的.然后我就各种查资料.其中我印象最深的是版本的问题和架包 ...
- spring利用javamail,quartz定时发送邮件 <转>
原文地址:spring利用javamail,quartz定时发送邮件 <转>作者:物是人非 spring提供的定时发送邮件功能,下面是一个简单的例子以供大家参考,首先从spring配置文件 ...
- Spring 利用PropertyPlaceholderConfigurer占位符
Hey Girl 博客园 首页 博问 闪存 新随笔 订阅 管理 posts - 42, comments - 3, trackbacks - 0 Sp ...
- 利用Spring MVC 上传图片文件
本文转自:http://amcucn.iteye.com/blog/264457.感谢作者 近日在工作当中,需要用到上传图片的功能,然而自己平时学习的时候只会使用struts的上传功能,但因为项目并没 ...
- Spring data jpa 实现简单动态查询的通用Specification方法
本篇前提: SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法 这篇文章中的第二种方法 实现Specification 这块的方法 只适用于一个对象针对某一个固定字 ...
随机推荐
- java类集框架图(google找的,备个份)
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- Yii源码阅读笔记(二十八)
Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...
- 使用Entity Framework 自动产生的Sql语句
对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...
- jquery 图片本地预览
uploadPreview.js /* *名称:图片上传本地预览插件 v1.1 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *参数说明: I ...
- LeetCode Encode and Decode Strings
原题链接在这里:https://leetcode.com/problems/encode-and-decode-strings/ 题目: Design an algorithm to encode a ...
- CentOS7安装配置SAMBA服务器
假设我们有这样一个场景 共享名 路径 权限 SHAREDOC /smb/docs 所有人员包括来宾均可以访问 RDDOCS /smb/tech 仅允许特定组的用户进行读写访问 特定组的组名为RD,目前 ...
- Windows上搭建Kafka运行环境
完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS 在环境搭建过程中遇到两个问题,在这里先列出来,以方便查询: 1. \Jav ...
- Python2.7的安装
>登录python官网下载python2.7的相关版本 python官网链接 >根据平台选择相应的版本 >下载完毕后点击安装即可 >配置环境变量 >安装成功
- TCP_NODELAY
启用TCP_NODELAY的情况下: 客户端程序C连接到服务器程序S: C仅接受数据,S仅发送数据 S循环调用send发送长度很小的数据包比如:10字节; 在C上用任务管理器查看到C的上行流量大约是下 ...