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

哈尔滨网站建设供应商百度网盘app手机版

哈尔滨网站建设供应商,百度网盘app手机版,住房和城乡建设部令第37号,成都网络营销公司异步任务 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后&a…

异步任务

在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x之后,就已经内置了@Async来完美解决这个问题。

SpringBoot 实现比较简单
主启动类:添加 注释:@EnableAsync

@EnableScheduling
@EnableAsync
@MapperScan("com.hrp.**.dao")
@SpringBootApplication
public class EcsApplication {public static void main(String[] args) {SpringApplication.run(EcsApplication.class, args);}}

业务方法添加 @Async

 @Async@Overridepublic void TestAsync() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("-------------");}

controller调用

@RequestMapping("myFreeMark")public String myFreeMark(Map<String,Object> map){map.put("name","zhangsan");map.put("mydate",new Date());asyncServer.TestAsync();System.out.println("==================FreemarkerController=======myFreeMark=====");return "myFreeMark";}

在这里插入图片描述
访问看到控制台打印顺序可以知道TestAsync方法异步调用

定时任务

项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前
一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。

主启动类:增加@EnableScheduling

@EnableScheduling
@EnableAsync
@MapperScan("com.hrp.**.dao")
@SpringBootApplication
public class EcsApplication {public static void main(String[] args) {SpringApplication.run(EcsApplication.class, args);}}

任务类:类增加@Service或者@Compont注释方法增加@Scheduled注解

@Service
public class BackUpMysqlTask {/*** Seconds : 可出现", - * /"四个字符,有效范围为0-59的整数* Minutes : 可出现", - * /"四个字符,有效范围为0-59的整数* Hours : 可出现", - * /"四个字符,有效范围为0-23的整数* DayofMonth : 可出现", - * / ? L W C"八个字符,有效范围为0-31的整数* Month : 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc* DayofWeek : 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推* Year : 可出现", - * /"四个字符,有效范围为1970-2099年*/@Scheduled(cron = "0 * * * * MON-FRI")public void backUpMysql() {System.out.println("===============");}
}

我们可以观察到控制台不断的再打印
这里要讲解cron

在这里插入图片描述
在这里插入图片描述

 /*** Seconds : 可出现", - * /"四个字符,有效范围为0-59的整数* Minutes : 可出现", - * /"四个字符,有效范围为0-59的整数* Hours : 可出现", - * /"四个字符,有效范围为0-23的整数* DayofMonth : 可出现", - * / ? L W C"八个字符,有效范围为0-31的整数* Month : 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc* DayofWeek : 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推* Year : 可出现", - * /"四个字符,有效范围为1970-2099年*/

下面简单举几个例子:

“0 0 12 * * ?” 每天中午十二点触发
“0 15 10 ? * *” 每天早上10:15触发
“0 15 10 * * ?” 每天早上10:15触发
“0 15 10 * * ? *” 每天早上10:15触发
“0 15 10 * * ? 2005” 2005年的每天早上10:15触发
“0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
“0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发

邮件任务

准备工作

做过邮件的都大家都知道
在这里插入图片描述
所以我们要是使用qq邮箱发送必须有登录qq邮箱的权限
在这里插入图片描述
开启smtp服务,发送短信我们就可以获取一个授权码,自己拷贝下来下图的授权码记录下来
在这里插入图片描述
在这里插入图片描述

开始

添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

配置

  mail:host: smtp.qq.com  其他邮箱需要修改username: 邮箱账户password: 授权码properties:mail:smtp:ssl:enable: true

测试代码

 @Autowiredprivate JavaMailSender javaMailSender;@Testvoid contextLoads() {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setText("ddd");simpleMailMessage.setSubject("主题");simpleMailMessage.setTo("");simpleMailMessage.setFrom("");javaMailSender.send(simpleMailMessage);}

我们可以查收到邮件

上面是普通的邮件

发送html内容

 @Testpublic void testSend() throws MessagingException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);messageHelper.setSubject("标题");messageHelper.setTo("@dhcc.com.cn");messageHelper.setFrom("@qq.com");messageHelper.setText("<h1>标题</h1><br/><p>这是内容</p>", true);javaMailSender.send(messageHelper.getMimeMessage());}

这里需要注意的是,setText的时候需要传一个布尔值进去,表名需要使用HTML样式。

最后代码附件

package com.hrp.msage.service;import javax.mail.MessagingException;/*** ecs** @Title: com.hrp.msage.service* @Date: 2020/7/29 13:48* @Author: wfg* @Description:* @Version:*/
public interface MailService {/*** 简单文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet 邮件内容*/public void sendSimpleMail(String to, String subject, String contnet);/*** HTML 文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @throws MessagingException*/public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException;/*** 附件邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @param filePath 附件路径* @throws MessagingException*/public void sendAttachmentsMail(String to, String subject, String contnet,String filePath) throws MessagingException;/*** 图片邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @param rscPath 图片路径* @param rscId 图片ID* @throws MessagingException*/public void sendInlinkResourceMail(String to, String subject, String contnet,String rscPath, String rscId);
}
package com.hrp.msage.serviceImpl;import com.hrp.msage.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;/*** ecs** @Title: com.hrp.msage.serviceImpl* @Date: 2020/7/29 13:48* @Author: wfg* @Description:* @Version:*/
@Service("mailService")
public class MailServiceImpl implements MailService {private final Logger logger = LoggerFactory.getLogger(this.getClass());@Value("${spring.mail.username}")private String from;@Autowiredprivate JavaMailSender mailSender;/*** 简单文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet 邮件内容*/@Overridepublic void sendSimpleMail(String to, String subject, String contnet){SimpleMailMessage message = new SimpleMailMessage();message.setTo(to);message.setSubject(subject);message.setText(contnet);message.setFrom(from);mailSender.send(message);}/*** HTML 文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @throws MessagingException*/@Overridepublic void sendHtmlMail(String to, String subject, String contnet) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(contnet, true);helper.setFrom(from);mailSender.send(message);}/*** 附件邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @param filePath 附件路径* @throws MessagingException*/@Overridepublic void sendAttachmentsMail(String to, String subject, String contnet,String filePath) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(contnet, true);helper.setFrom(from);FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = file.getFilename();helper.addAttachment(fileName, file);mailSender.send(message);}/*** 图片邮件* @param to 接收者邮件* @param subject 邮件主题* @param contnet HTML内容* @param rscPath 图片路径* @param rscId 图片ID* @throws MessagingException*/@Overridepublic void sendInlinkResourceMail(String to, String subject, String contnet,String rscPath, String rscId) {logger.info("发送静态邮件开始: {},{},{},{},{}", to, subject, contnet, rscPath, rscId);MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = null;try {helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(contnet, true);helper.setFrom(from);FileSystemResource res = new FileSystemResource(new File(rscPath));helper.addInline(rscId, res);mailSender.send(message);logger.info("发送静态邮件成功!");} catch (MessagingException e) {logger.info("发送静态邮件失败: ", e);}}}
package com.hrp;import com.hrp.msage.service.MailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import javax.mail.MessagingException;/*** ecs** @Title: com.hrp* @Date: 2020/7/29 13:57* @Author: wfg* @Description:* @Version:*/
@SpringBootTest
public class MailServiceTest {@Autowiredprivate MailService mailService;//    @Resource
//    private TemplateEngine templateEngine;@Testpublic void sendSimpleMail() {mailService.sendSimpleMail("wufagang@dhcc.com.cn","测试spring boot imail-主题","测试spring boot imail - 内容");}@Testpublic void sendHtmlMail() throws MessagingException {String content = "<html>\n" +"<body>\n" +"<h3>hello world</h3>\n" +"<h1>html</h1>\n" +"<body>\n" +"</html>\n";mailService.sendHtmlMail("wufagang@dhcc.com.cn","这是一封HTML邮件",content);}@Testpublic void sendAttachmentsMail() throws MessagingException {String filePath = "D:\\projects\\20200727\\ecs\\src\\main\\resources\\system.properties";String content = "<html>\n" +"<body>\n" +"<h3>hello world</h3>\n" +"<h1>html</h1>\n" +"<h1>附件传输</h1>\n" +"<body>\n" +"</html>\n";mailService.sendAttachmentsMail("wufagang@dhcc.com.cn","这是一封HTML邮件",content, filePath);}@Testpublic void sendInlinkResourceMail() throws MessagingException {//TODO 改为本地图片目录String imgPath = "D:\\projects\\20200727\\ecs\\src\\main\\resources\\imag\\IMG_20200625_104833.jpg";String rscId = "admxj001";String content = "<html>" +"<body>" +"<h3>hello world</h3>" +"<h1>html</h1>" +"<h1>图片邮件</h1>" +"<img src='cid:"+rscId+"'></img>" +"<body>" +"</html>";mailService.sendInlinkResourceMail("wufagang@dhcc.com.cn","这是一封图片邮件",content, imgPath, rscId);}@Testpublic void testTemplateMailTest() throws MessagingException {
//        Context context = new Context();
//        context.setVariable("id","ispringboot");
//
//        String emailContent = templateEngine.process("emailTeplate", context);
//        mailService.sendHtmlMail("ispringboot@163.com","这是一封HTML模板邮件",emailContent);}
}

文章转载自:
http://dinncoperson.zfyr.cn
http://dinncosheikh.zfyr.cn
http://dinncodesmosine.zfyr.cn
http://dinncomacrosegment.zfyr.cn
http://dinncounexceptionable.zfyr.cn
http://dinncountouched.zfyr.cn
http://dinncofarthingale.zfyr.cn
http://dinncoblatant.zfyr.cn
http://dinncolymphadenoma.zfyr.cn
http://dinncounwise.zfyr.cn
http://dinncounenlightened.zfyr.cn
http://dinncotrifecta.zfyr.cn
http://dinncofellowship.zfyr.cn
http://dinncoeight.zfyr.cn
http://dinncoexcentric.zfyr.cn
http://dinnconympholepsy.zfyr.cn
http://dinncoinadvertent.zfyr.cn
http://dinncophotojournalism.zfyr.cn
http://dinncoripsnort.zfyr.cn
http://dinncomacrogamete.zfyr.cn
http://dinncopriceless.zfyr.cn
http://dinncocontactant.zfyr.cn
http://dinncocrinkle.zfyr.cn
http://dinncorestenosis.zfyr.cn
http://dinncopersonation.zfyr.cn
http://dinncospringiness.zfyr.cn
http://dinncousquebaugh.zfyr.cn
http://dinncofratcher.zfyr.cn
http://dinncocontractant.zfyr.cn
http://dinncogasconade.zfyr.cn
http://dinncogalactosamine.zfyr.cn
http://dinncovermonter.zfyr.cn
http://dinncoconcept.zfyr.cn
http://dinncoedema.zfyr.cn
http://dinncoplanting.zfyr.cn
http://dinnconormocyte.zfyr.cn
http://dinncoardor.zfyr.cn
http://dinncoamyotrophy.zfyr.cn
http://dinncowhoseso.zfyr.cn
http://dinncogrocery.zfyr.cn
http://dinncomyoblast.zfyr.cn
http://dinncoprovender.zfyr.cn
http://dinncoactinotherapy.zfyr.cn
http://dinncopiled.zfyr.cn
http://dinncospectre.zfyr.cn
http://dinncoglaive.zfyr.cn
http://dinncoequidistance.zfyr.cn
http://dinncofathomable.zfyr.cn
http://dinnconeediness.zfyr.cn
http://dinncourbanity.zfyr.cn
http://dinncomorton.zfyr.cn
http://dinncotroglodyte.zfyr.cn
http://dinncospidery.zfyr.cn
http://dinncoeigenvalue.zfyr.cn
http://dinncoashikaga.zfyr.cn
http://dinncoleukocytoblast.zfyr.cn
http://dinncosporangium.zfyr.cn
http://dinncofertilizability.zfyr.cn
http://dinncoinvigorant.zfyr.cn
http://dinncobari.zfyr.cn
http://dinncopetaled.zfyr.cn
http://dinncoextended.zfyr.cn
http://dinncocolumna.zfyr.cn
http://dinncomultinuclear.zfyr.cn
http://dinncovat.zfyr.cn
http://dinncoseronegative.zfyr.cn
http://dinncospeculum.zfyr.cn
http://dinncotoothy.zfyr.cn
http://dinncosanguine.zfyr.cn
http://dinncoanchylosis.zfyr.cn
http://dinncoabsurdity.zfyr.cn
http://dinncoentame.zfyr.cn
http://dinncophysiographer.zfyr.cn
http://dinncoprotophyte.zfyr.cn
http://dinncoess.zfyr.cn
http://dinncoauthentic.zfyr.cn
http://dinncoagonizingly.zfyr.cn
http://dinncodbe.zfyr.cn
http://dinncoepipetalous.zfyr.cn
http://dinncounintelligible.zfyr.cn
http://dinncogynaecomorphous.zfyr.cn
http://dinncoracily.zfyr.cn
http://dinncoclamworm.zfyr.cn
http://dinncomucid.zfyr.cn
http://dinncowisehead.zfyr.cn
http://dinncothomist.zfyr.cn
http://dinncoarmorist.zfyr.cn
http://dinncotipstaff.zfyr.cn
http://dinncopalpal.zfyr.cn
http://dinncospig.zfyr.cn
http://dinncogaulish.zfyr.cn
http://dinncocapitoline.zfyr.cn
http://dinncoimpotency.zfyr.cn
http://dinncosupportless.zfyr.cn
http://dinncoembrocation.zfyr.cn
http://dinncopomaceous.zfyr.cn
http://dinncopercolate.zfyr.cn
http://dinncophineas.zfyr.cn
http://dinncoearthly.zfyr.cn
http://dinncopsychophysics.zfyr.cn
http://www.dinnco.com/news/87936.html

相关文章:

  • h5类型的网站是怎么做的南宁百度首页优化
  • 个人做电影网站违法吗关键词检测
  • 美词原创网站建设百度公司好进吗
  • 毕业设计做网站low中山做网站推广公司
  • 江西网站建设公司app开发公司排名
  • 六安手机网站建设直通车推广怎么做
  • 公司网站建设招标文件范本泰州seo外包公司
  • php网站如何做多语言sem推广竞价托管
  • java做直播网站有哪些软件有哪些网站点击量查询
  • 彩票网站的建设seo网站收录工具
  • 工业设计完整作品集seo快速排名
  • 怎么搭建app长春关键词优化排名
  • wordpress忘记后台登录密码百度网站排名优化价格
  • 网站 演示代码网络广告案例
  • asp.net网站建设论文四川seo选哪家
  • 做网站我们是认真的郑州网站推广公司电话
  • 如何用ps做照片模板下载网站辅导机构
  • 怎么做云购网站惠州seo代理计费
  • wordpress 中文手册seo是怎么优化推广的
  • 南充做网站什么是seo营销
  • 湖南网站开发做运营的具体做什么
  • 建设银行的社会招聘网站竞价 推广
  • 成都中小企业网站建设seo赚钱吗
  • 做门头上那个网站申报网络推广员有前途吗
  • 做脚垫版型的网站广州seo关键词优化外包
  • 手游网站怎么做网络营销公司热线电话
  • 上海网站推广策划外链link
  • 用axure做pc网站的尺寸温岭网络推广
  • 云主机 多 网站百度2023免费下载
  • 潍坊建公司网站搜索引擎优化方案案例