当前位置: 首页 > news >正文

济南传承网站建设公司网络销售推广公司

济南传承网站建设公司,网络销售推广公司,室内设计师35岁后的出路,如何用二级域名做网站布奏文章目录前言一、异步方法调用1、导入依赖2、创建异步执行任务线程池3、创建业务层接口和实现类4、创建业务层接口和实现类二、测试定时任务1.导入依赖2.编写测试类,开启扫描定时任务3.测试三、实现定时发送邮件案例1.邮箱开启IMAP服务2.导入依赖3.导入EmailUtil4.编…

文章目录

  • 前言
  • 一、异步方法调用
    • 1、导入依赖
    • 2、创建异步执行任务线程池
    • 3、创建业务层接口和实现类
    • 4、创建业务层接口和实现类
  • 二、测试定时任务
    • 1.导入依赖
    • 2.编写测试类,开启扫描定时任务
    • 3.测试
  • 三、实现定时发送邮件案例
    • 1.邮箱开启IMAP服务
    • 2.导入依赖
    • 3.导入EmailUtil
    • 4.编写邮件发送方法
    • 5.开启异步和定时任务
  • 总结


前言

Quartz是一个完全由java编写的开源作业调度框架、它的简单易用受到业内人士的一致好评。本篇记录怎么用SpringBoot使用Quartz


一、异步方法调用

由于多个任务同时执行时,默认为单线程,所以我们用异步方法调用,使其成为多线程执行

看一个案例

1、导入依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

2、创建异步执行任务线程池

这里我们使用springboot自带的线程池

package com.lzl.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;@Configuration
public class AsyncExcutorPoolConfig implements AsyncConfigurer {@Bean("asyncExecutor")@Overridepublic Executor getAsyncExecutor() {//Spring自带的线程池(最常用)ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();//线程:IO密集型 和 CPU密集型//线程设置参数taskExecutor.setCorePoolSize(8);//核心线程数,根据电脑的核数taskExecutor.setMaxPoolSize(16);//最大线程数一般为核心线程数的2倍taskExecutor.setWaitForTasksToCompleteOnShutdown(true);//任务执行完成后关闭return taskExecutor;}
}

注意注解不要少

3、创建业务层接口和实现类

package com.lzl.Service;/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 09:42*/public interface AsyncService {void testAsync1();void testAsync2();
}
package com.lzl.Service.impl;import com.lzl.Service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 09:43*/
@Service
public class AsyncImpl implements AsyncService {@Async@Overridepublic void testAsync1() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("精准是唯一重要的标准!");}@Async("asyncExecutor")//开启异步执行@Overridepublic void testAsync2() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("效率是成功的核心关键!");}
}

4、创建业务层接口和实现类

package com.lzl.task;import com.lzl.Service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 09:40*/
@RestController
@RequestMapping("/login")
public class LoginController {@Autowiredprivate AsyncService service;@RequestMapping("/Async1")public String testAsync1(){service.testAsync1();return "牛逼!";}@RequestMapping("/Async2")public String testAsync2(){service.testAsync2();return "不牛逼!";}
}

在启动类开启异步
在这里插入图片描述

整体目录结构如下:
在这里插入图片描述

测试:
运行项目,访问controller

在这里插入图片描述
访问controller时,页面直接出现返回值,控制台过了两秒打印文字,证明异步执行成功!
在这里插入图片描述

二、测试定时任务

1.导入依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

2.编写测试类,开启扫描定时任务

package com.lzl.task;import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;import java.util.Date;/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 10:42*/
//任务类
@Configuration
public class Tasks {@Async@Scheduled(cron = "*/2 * * * * ?")public void task1(){System.out.println("效率"+new Date().toLocaleString());}@Async@Scheduled(cron = "*/1 * * * * ?")public void task2(){System.out.println("精准"+new Date().toLocaleString());}
}

在这里插入图片描述

3.测试

在这里插入图片描述

三、实现定时发送邮件案例

这里以QQ邮箱为例,这个功能类似于通过邮箱找回密码类似,需要我们进行授权码操作

1.邮箱开启IMAP服务

登陆QQ邮箱,找到帐户,下拉
在这里插入图片描述
看到如下图:
在这里插入图片描述
我这里已经开启了,按照步骤操作,会有一个授权码,保存好下边步骤要用,此处不再演示

2.导入依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 邮箱 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

3.导入EmailUtil

package com.lzl.utils;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;
/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 11:44*/public class EmailUtil {private static final String USER = "@qq.com"; // 发件人邮箱地址private static final String PASSWORD = ""; // qq邮箱的客户端授权码(需要发短信获取)/*** @param to    收件人邮箱地址* @param text  邮件正文* @param title 标题*//* 发送验证信息的邮件 */public static boolean sendMail(String to, String text, String title) {try {final Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", "smtp.qq.com");// 发件人的账号props.put("mail.user", USER);//发件人的密码props.put("mail.password", PASSWORD);// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(props, authenticator);// 创建邮件消息MimeMessage message = new MimeMessage(mailSession);// 设置发件人String username = props.getProperty("mail.user");InternetAddress form = new InternetAddress(username);message.setFrom(form);// 设置收件人InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 设置邮件标题message.setSubject(title);// 设置邮件的内容体message.setContent(text, "text/html;charset=UTF-8");// 发送邮件Transport.send(message);return true;} catch (Exception e) {e.printStackTrace();}return false;}//随机生成num个数字验证码public static String getValidateCode(int num) {Random random = new Random();String validateCode = "";for (int i = 0; i < num; i++) {//0 - 9 之间 随机生成 num 次int result = random.nextInt(10);validateCode += result;}return validateCode;}//测试public static void main(String[] args) throws Exception {//给指定邮箱发送邮件EmailUtil.sendMail("729953102@qq.com", "你好,这是一封测试邮件,无需回复。", "测试邮件随机生成的验证码是:" + getValidateCode(6));System.out.println("发送成功");}
}

4.编写邮件发送方法

package com.lzl.task;import com.lzl.utils.EmailUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;/*** --效率,是成功的核心关键--** @Author lzl* @Date 2023/3/7 11:45*/
@Configuration
public class TaskEmail {//指定时间进行发送邮件@Scheduled(cron = "10 49 11 * * ?")public void sendMail(){EmailUtil.sendMail("自己的邮箱@qq.com", "效率,是成功的核心关键!", "测试邮件随机生成的验证码是:" + EmailUtil.getValidateCode(6));}
}

5.开启异步和定时任务

package com.lzl;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableAsync//开启异步
@EnableScheduling//开启定时任务
public class QuartzStudyApplication {public static void main(String[] args) {SpringApplication.run(QuartzStudyApplication.class, args);}}

测试:
此处不再演示


总结

定时任务在很多业务场景中经常会用到,好记性不如烂笔头,本篇只是简单的记录一下


文章转载自:
http://slantingways.qwfL.cn
http://pas.qwfL.cn
http://agio.qwfL.cn
http://soloist.qwfL.cn
http://ordinal.qwfL.cn
http://justice.qwfL.cn
http://unfoiled.qwfL.cn
http://frontolysis.qwfL.cn
http://rimula.qwfL.cn
http://hematoxylic.qwfL.cn
http://swanu.qwfL.cn
http://herringbone.qwfL.cn
http://cherokee.qwfL.cn
http://pelf.qwfL.cn
http://invariance.qwfL.cn
http://univalve.qwfL.cn
http://hardy.qwfL.cn
http://spoffish.qwfL.cn
http://saurischian.qwfL.cn
http://borosilicate.qwfL.cn
http://valerianate.qwfL.cn
http://vitriolic.qwfL.cn
http://brawny.qwfL.cn
http://carnation.qwfL.cn
http://hormogonium.qwfL.cn
http://triumph.qwfL.cn
http://keeno.qwfL.cn
http://opening.qwfL.cn
http://orrow.qwfL.cn
http://backscratcher.qwfL.cn
http://tachina.qwfL.cn
http://telewriter.qwfL.cn
http://aeronef.qwfL.cn
http://tuner.qwfL.cn
http://fungistat.qwfL.cn
http://halibut.qwfL.cn
http://palmer.qwfL.cn
http://niccolite.qwfL.cn
http://hellenic.qwfL.cn
http://mediation.qwfL.cn
http://delawarean.qwfL.cn
http://props.qwfL.cn
http://foible.qwfL.cn
http://sedimentary.qwfL.cn
http://remarque.qwfL.cn
http://erg.qwfL.cn
http://armco.qwfL.cn
http://roseanna.qwfL.cn
http://lottery.qwfL.cn
http://becloud.qwfL.cn
http://mechanic.qwfL.cn
http://gabber.qwfL.cn
http://jabez.qwfL.cn
http://heterometabolic.qwfL.cn
http://thanatocoenosis.qwfL.cn
http://aquarii.qwfL.cn
http://indemnify.qwfL.cn
http://ibid.qwfL.cn
http://abulia.qwfL.cn
http://amidohydrolase.qwfL.cn
http://humpy.qwfL.cn
http://spontoon.qwfL.cn
http://carcinomatous.qwfL.cn
http://mossback.qwfL.cn
http://acetic.qwfL.cn
http://unlay.qwfL.cn
http://dasher.qwfL.cn
http://cercis.qwfL.cn
http://new.qwfL.cn
http://carnaby.qwfL.cn
http://kuskokwim.qwfL.cn
http://albeit.qwfL.cn
http://flotsan.qwfL.cn
http://fractionalism.qwfL.cn
http://aiee.qwfL.cn
http://digraph.qwfL.cn
http://model.qwfL.cn
http://kep.qwfL.cn
http://radiovision.qwfL.cn
http://suboptimal.qwfL.cn
http://purslane.qwfL.cn
http://pterosaurian.qwfL.cn
http://dank.qwfL.cn
http://blellum.qwfL.cn
http://diaphanometer.qwfL.cn
http://fiscality.qwfL.cn
http://celia.qwfL.cn
http://outfitter.qwfL.cn
http://woodfibre.qwfL.cn
http://overfeed.qwfL.cn
http://diversiform.qwfL.cn
http://cliffy.qwfL.cn
http://bombita.qwfL.cn
http://deterrence.qwfL.cn
http://chuppah.qwfL.cn
http://untiringly.qwfL.cn
http://survivorship.qwfL.cn
http://foreordain.qwfL.cn
http://gifford.qwfL.cn
http://ghaut.qwfL.cn
http://www.15wanjia.com/news/78541.html

相关文章:

  • 海外 国内网站建设seo引擎
  • 中企动力网站建设文案代写平台
  • 怎样建设博彩网站漯河网站seo
  • 用友财务软件的客服电话南宁seo主管
  • 做网站是用啥软件做的郑州网站推广方案
  • 理解wordpress轮翻图代码重庆seo扣费
  • 响应式网站制作公司百度竞价客服
  • 阿里巴巴国际站做2个网站有用吗衡阳有实力seo优化
  • 电子商务网站建设实训心得优就业seo怎么样
  • 网站已备案下一步怎么做优化大师有必要花钱吗
  • 网站注销申请表推广获客
  • 如何建立公司网址百度的搜索引擎优化
  • 网站开发技术论文国内重大新闻
  • html中秋节网页制作代码企业网站seo哪里好
  • wordpress删除数据库数据表seo优化技术教程
  • 音乐网站怎么做精准关键词企业seo网站营销推广
  • 企业网站建设cms站重庆seo俱乐部联系方式
  • 厦门企业自助建站系统软文发布推广平台
  • 手机怎么做销售网站关键词优化价格
  • 嘉兴哪里做网站seo兼职平台
  • 第一次做网站文案短句干净治愈
  • phpcms v9 网站建设入门竞价软件哪个好
  • 二炮手东莞百度搜索优化
  • 网站功能价格表百度pc网页版
  • 佛山购物网站建设优化网站排名
  • 买模板做的网站表单数据在哪里看百度广告收费表
  • 揭阳做网站线上营销渠道主要有哪些
  • 天津疫情防控措施北京网站优化怎么样
  • 湛江免费做网站域名查询ip爱站网
  • 网站怎么做分享链接地址第三波疫情将全面大爆发