31、springboot与任务
异步任务
- @Service
- public class AsynService {
- public void hello(){
- try {
- Thread.sleep();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("处理数据.....");
- }
- }
controller类:
- @Controller
- public class AsynController {
- @Autowired
- AsynService asynService;
- @ResponseBody
- @RequestMapping("/hello")
- public String hello(){
- asynService.hello();
- return "success";
- }
- }
此时会有三秒的等待响应时间!!!!
但是如果工程量大的话,这样会比较麻烦
- @Service
- public class AsynService {
- //告诉spring这是一个异步的方法
- @Async
- public void hello(){
- try {
- Thread.sleep();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("处理数据.....");
- }
- }
开启方法:
- @EnableAsync
- @SpringBootApplication
- public class TaskApplication {
- public static void main(String[] args) {
- SpringApplication.run(TaskApplication.class, args);
- }
- }
定时任务
定时做打印操作:
- @Service
- public class ScheduleService {
- //cron:second、minute、hour、day of mounth、day of week
- @Scheduled(cron ="0 * * * * MON-SAT" )
- public void hello(){
- System.out.println("定时处理");
- }
- }
开启注解:
- //开启定时任务
- @EnableScheduling
- @SpringBootApplication
- public class TaskApplication {
- public static void main(String[] args) {
- SpringApplication.run(TaskApplication.class, args);
- }
- }
在任意分钟的0-10s进行打印
- @Scheduled(cron = "0-10 * * * * 0-7")
- public void hello(){
- System.out.println("定时处理");
- }
邮件任务

- @ConfigurationProperties(
- prefix = "spring.mail"
- )
- public class MailProperties {
- private static final Charset DEFAULT_CHARSET;
- private String host;
- private Integer port;
- private String username;
- private String password;
- private String protocol = "smtp";
- private Charset defaultEncoding;
- private Map<String, String> properties;
- private String jndiName;
- ...
- }
配置文件:
- spring.mail.username=@qq.com
- #授权码
- spring.mail.password=keoszgbsssddbaad
- spring.mail.host=smtp.qq.com
- spring.mail.properties.mail.smtp.ssl.enable=true
host:
简单邮件
- @Autowired
- JavaMailSenderImpl javaMailSender;
- @Test
- public void contextLoads() {
- SimpleMailMessage msg = new SimpleMailMessage();
- //邮件设置
- msg.setSubject("猪头");
- msg.setText("你就是猪头哦!!");
- msg.setTo("xxxxxxxxx@qq.com");
- msg.setFrom("12872213xx@qq.com");
- javaMailSender.send(msg);
- }
复杂的邮件测试:
- @Test
- public void test1() throws MessagingException {
- //创建复杂邮件
- MimeMessage msg = javaMailSender.createMimeMessage();
- //上传文件
- MimeMessageHelper helper = new MimeMessageHelper(msg,true);
- //邮件设置
- helper.setSubject("pig");
- helper.setText("<b style='color:red'>pig..... </b>",true);
- helper.setTo("3212393029@qq.com");
- helper.setFrom("12872213xx@qq.com");
- //上传文件
- helper.addAttachment("319898.jpg",new File("D:\\Tools\\319898.jpg"));
- javaMailSender.send(msg);
- }
html的设置等都可以显示,图片的上传!!!
31、springboot与任务的更多相关文章
- 第3章 springboot接口返回json 3-1 SpringBoot构造并返回一个json对象
数据的使用主要还是以JSON为主,我们不会去使用XML. 这个时候我们先不使用@RestController,我们使用之前SpringMVC的那种方式,就是@Controller. @Respons ...
- 千锋很火的SpringBoot实战开发教程视频
springboot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- 微服务springboot视频最新SpringBoot2.0.3版本技术视频教程【免费学习】
超火爆的springboot微服务技术怎么学,看这里,springboot超详细的教程↓↓↓↓↓↓https://ke.qq.com/course/179440?tuin=9b386640 01.sp ...
- jquery-bootgrid
http://www.jquery-bootgrid.com/GettingStarted 日志是生产环境非常重要的配置,在迁移老的工程到spring-boot时日志的设置兼容很重要,以下是自己在配置 ...
- 每天学会一点点(JAVA基础)
1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? 虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件. Java被设计成允 ...
- 城市代码表mysql
只有代码: # ************************************************************ # Sequel Pro SQL dump # Version ...
- 「小程序JAVA实战」springboot的后台搭建(31)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...
- SpringBoot IntelliJ创建简单的Restful接口
使用SpringBoot快速建服务,和NodeJS使用express几乎一模一样,主要分为以下: 1.添加和安装依赖 2.添加路由(即接口) 3.对路由事件进行处理 同样坑的地方就是,祖国的防火墙太 ...
- springboot+druid
最近项目需要搭建新工程,打算使用微服务的形式搭建便于后期拓展.看了一圈发现springboot易于搭建,配置简单,强化注解功能,"just run". Spring Boot ma ...
随机推荐
- Golang报错mixture of field:value and value initializers
Golang 在使用匿名成员初始化时,如果出现 mixture of field:value and value initializers 是因为初始化的方式不对,见代码: package main ...
- 使用命令行编译打包运行自己的MapReduce程序 Hadoop2.6.0
使用命令行编译打包运行自己的MapReduce程序 Hadoop2.6.0 网上的 MapReduce WordCount 教程对于如何编译 WordCount.java 几乎是一笔带过… 而有写到的 ...
- 由上一个血案引发的关于property和attribute关系的思考
boss说,学习要刨根问底. 好的,开刨. 一.property和attribute在英语里有什么区别 看似没有区别.但其实大神说: property是 物体本身自带属性,不能改变的(一旦改了就是另外 ...
- HTML绝对路径和相对路径
HTML路径: 绝对路径:从根目录开始 相对路径:../ 相对于html文件,上一级 ./ 相对于html文件,当前路径(可以省略) 文件夹名 相对于html文件,往文件里面走
- JavaWeb学习总结(四):Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- 转:Redis和Memcache的区别分析
Redis和Memcache的区别分析 原文链接:http://blog.csdn.net/u013474436/article/details/48632665 简单区别: 1. Redis中,并 ...
- 获取JPEGImageEncoder和JPEGCode这两个类
最近要对PDF做一些操作,在查看别人代码,拿过来借用的时候,由于代码不完整,引用的类也不全,导致JPEGImageEncoder和JPEGCode这两个类找不到,后来网上搜索了下,发现这两个类来自于J ...
- GeoAnalytics Server学习笔记
GA的输入数据源 输入源 存储形式 Spatiotemporal 时空型ArcGIS DataStore 物联网数据 (通过GeoEvent Server输出) 大数据共享目录BigDataShare ...
- 移动目标在三维GIS中的实现方法
对于基于ArcGIS Runtime的应用程序,其实现方法比较简单,可以直接更新图形的Geometry属性,即可实现位置的移动: private void AddGraphics() { var gl ...
- (WCF) 多线程 (Multi-threading) 和 并发性 (Concurency)
问题:WCF 有个Server端,还有个Client端,他们之间是如何进行并发,多线程通信的呢?多个Client端同时访问Server,如何保证Server端的操作线程安全呢? 在理解WCF Conc ...