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 ...
随机推荐
- DBUtils结果集处理器介绍
common-dbutils.jar是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能. 1.QueryRunner类 ① ...
- JS中数组和对象的区别
- BZOJ1996 [Hnoi2010] 合唱队
Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Output 8 HINT Solution 令$f_{i,j}$ ...
- attr()与prop()之全选、反选问题
获取js dom原生属性的时候最好用prop()方法,获取自己添加的属性时用attr() 原文:http://blog.sina.com.cn/s/blog_bf5ce8cc0102vuyt.html ...
- JavaScript的进阶之路(六)理解函数
函数:定义一次,多次调用:用于对象的属性则称为对象的方法:在JavaScript中,函数即对象:嵌套的函数形成闭包: 定义函数和简单调用函数: //函数定义 function f1(){ //没有参数 ...
- MySQL图形化界面SQLyog
软件地址:SQLyog 密码: mmaf
- slice()方法 和splice 方法的区别
定义 splice() 方法 用于插入.删除或替换数组的元素. slice() 方法 可提取字符串的某个部分,并以新的字符串返回被提取的部分. 更多的可查看: http://www.cnblogs.c ...
- vue-cli构建项目 npm run build后应该怎么运行在本地查看效果
问题: 就是 bulid 打包后,想本地看看效果,本地看不了.... 网上看到一个.... 具体更多在: http://www.dabaipm.cn/static/frontend/346.htm ...
- synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...
- VMware下Linux配置局域网和外网访问(CentOS)
要使用Linux系统很重要的一个操作就是使Linux系统能够访问互联网,只有Linux系统能够访问互联网才能够去下载很多自己所需要的资源,如果不能访问互联网那么使用Linux系统往往会卡在这一步,假设 ...