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 ...
随机推荐
- Java开发学习--Java 中基本类型和包装类之间的转换
Java 中基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之 ...
- Tempter of the Bone 搜索---奇偶性剪枝
Tempter of the Bone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- jvm 内存机制
jvm 的内存包括stack ,Heap,NonHeap,在此重点说明Heap,NonHeap. Heap叫堆内存,它用于存放类实例和数组信息.NonHeap叫非堆内存,用于存放类,方法等可反射的对象 ...
- CSS,js,html
图片盗链问题使用以下meta标签解决 <meta name="referrer" content="never"> Chrome 中文界面下默认会将 ...
- Access MetaData
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 使用javascript调用android代码
1.使用webview对象的addJavascriptInterface方法 2.addJavascriptInterface方法有两个参数,第一个参数就是我们一般会实现一个自己的类,类里面提供我们要 ...
- Python之装饰器复习
一.什么是装饰器? 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 二.强调装饰器的原则: 1 不修改被装饰对象的源代码 2 不修改被装饰对象的调用方式 3:在遵循1和2 ...
- ORACLE数据仓库学习记录
一.数据仓库安装 安装ORACLE DATABASE 10g Release 2 ORACLE数据库版本是:10.2.0.1.0(服务器).执行基本安装(安装全部的组件)并创建示例数据库. 安装ORA ...
- 简析 Tomcat 、Nginx 与 Apache 的区别
简析 Tomcat .Nginx 与 Apache 的区别 本文讲的是简析 Tomcat .Nginx 与Apache的区别, 经常在用 apache 和 tomcat 等这些服务器,可是总感觉还是不 ...
- 算法之杨辉三角形(Java语言)
杨辉三角形, 又称贾宪三角形.帕斯卡三角形. 前9层写出来如下: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 ...