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

网站建设色调的深圳市seo上词贵不贵

网站建设色调的,深圳市seo上词贵不贵,查询域名备案,新媒体营销策划基于java中延时队列的实现该文章,我们这次主要是来实现基于DelayQueue实现的延时队列。 使用DelayQueue实现的延时队列的步骤: 定义一个继承了Delayed的类,定义其中的属性,并重写compareTo和getDelay两个方法创建一个Delayqueue…

基于java中延时队列的实现该文章,我们这次主要是来实现基于DelayQueue实现的延时队列。

使用DelayQueue实现的延时队列的步骤:

  1. 定义一个继承了Delayed的类,定义其中的属性,并重写compareTogetDelay两个方法
  2. 创建一个Delayqueue用于创建队列
  3. 创建一个生产者,用于将信息添加到队列中
  4. 创建一个消费者,用来从队列中取出信息进行消费

接下来是一个简单的demo :

定义一个元素类


import lombok.Data;import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;@Data
public class DelayTesk implements Delayed {//标签Idprivate String uid;//到期时间private Long timestamp;//延时信息private String data;@Overridepublic long getDelay(TimeUnit unit) {long delayTime = timestamp - System.currentTimeMillis();//将时间转换成毫秒(这边可转可不转,影响不大)return unit.convert(delayTime, TimeUnit.MILLISECONDS);}@Overridepublic int compareTo(Delayed o) {//针对任务的延时时间长短进行排序,把延时时间最短的放在前面long differenceTime = this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS);return (int)differenceTime;}
}

定义一个延时队列


import java.util.concurrent.DelayQueue;public class DelayTaskQueue {/*** 这边使用单例模式进行创建,保证全局队列的唯一性* 我这边使用的是双检索,双校验模式*/private volatile static DelayQueue<DelayTesk> delayTaskQueue;private DelayTaskQueue(){}public static DelayQueue<DelayTesk> getDelayTaskQueue() {if (delayTaskQueue == null) {synchronized (DelayTaskQueue.class) {if (delayTaskQueue == null) {delayTaskQueue = new DelayQueue<>();}}}return delayTaskQueue;}
}

创建一个延时队列的生产者

import lombok.extern.slf4j.Slf4j;import java.util.concurrent.DelayQueue;//消息生产者
@Slf4j
public class DelayTeskQueueProducer {/***  往延时队列中插入数据* @param uid* @param time* @param data*/public static void setDelayQueue(String uid, Long time, String data) {//创建队列DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();//创建任务DelayTesk delayTesk = new DelayTesk();delayTesk.setUid(uid);delayTesk.setTimestamp(time);delayTesk.setData(data);log.info("====================消息入队:{}===============", uid);boolean res = delayTaskQueue.offer(delayTesk);if (res) {log.info("====================消息入队成功:{}===============", uid);} else {//如果消息入队失败这边可以写一个失败的回调函数//例如将失败的消息存入数据库,写个定时任务对消息进行重写投递……log.info("====================消息入队失败:{}===============", uid);}}
}

定义一个延时队列的消费者

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;import java.util.concurrent.DelayQueue;@Slf4j
public class DelayTeskQueueConsumer {public static void main(String[] args) {for (int i = 0; i < 10 ; i++) {DelayTeskQueueProducer.setDelayQueue(IdUtil.fastUUID(), System.currentTimeMillis() + i * 1000, "hello world" + i);}int index = 0;DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();while (index < 10) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (InterruptedException e) {log.error("延时队列消费异常:{}", e.getMessage());}}}
}

结果
在控控制台中每隔1秒打印一行数据
在这里插入图片描述

到这差不多我们的Demo就要结束了,不过可能有些同学会问,你这个消费者不是是写在mian方法里的,每次消费的时候都需要手动去调用这跟我直接用sleep函数实现的延时队列有啥区别呀

别急 这个只是个Demo嘛,如果需要使用在项目中可以写一个监听器去实时监听该延时队列
我这边暂时就只讲3种

Timer

通过timer定时定频率去获取DelayTaskQueue中的消息

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.DelayQueue;/***  添加@Configuration  注解,自动注入实例对象,并由springboot 启动 定时器,执行任务。*/@Configuration
@Slf4j
public class DelayTeskQueueTimer {@Beanpublic void DelayTeskQueueTimer() {log.info("====================监听开始====================");final Timer timer = new Timer();DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();timer.schedule(new TimerTask() {@Overridepublic void run() {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}//第一次执行是在当前时间的一秒之后,之后每隔一秒钟执行一次},1000, 1000);}
}

ConmandlineRunner

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;import java.util.concurrent.DelayQueue;
/*** Spring Boot应用程序在启动后,程序从容器中遍历实现了CommandLineRunner接口的实例并运行它们的run方法*/
@Slf4j
@Configuration
public class DelayTeskQueueTimerCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) {log.info("====================CommandLineRunner监听开始====================");DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();new Thread(() ->{while (true) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}}).start();}
}

ApplicationListener

该方法和ConmandlineRunner方法一样 都是在Spring Boot应用程序在启动后,对DelayQueue进行监听

import com.study.project.delay.DelayTaskQueue;
import com.study.project.delay.DelayTesk;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;import java.util.concurrent.DelayQueue;@Slf4j
@Configuration
public class DelayTeskQueueApplicationListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {log.info("====================ApplicationListener监听开始====================");DelayQueue<DelayTesk> delayTaskQueue = DelayTaskQueue.getDelayTaskQueue();new Thread(() -> {while (true) {try {DelayTesk delayTesk = delayTaskQueue.take();System.out.println(delayTesk.getData());} catch (Exception e) {log.error("延时队列消费异常:{}", e.getMessage());}}}).start();}
}

当然监听的方法其实还有很多,不过同学们在实现队列的时候不要觉得实现了就好了,要去思考如何去保证数据的持久化,保证数据不会不会丢失


文章转载自:
http://sculpt.rgxf.cn
http://disinsectize.rgxf.cn
http://blowhard.rgxf.cn
http://singultus.rgxf.cn
http://myrmecochorous.rgxf.cn
http://situation.rgxf.cn
http://venturesomely.rgxf.cn
http://tarmacadam.rgxf.cn
http://preconcerted.rgxf.cn
http://upsides.rgxf.cn
http://birthright.rgxf.cn
http://uncreased.rgxf.cn
http://cautionry.rgxf.cn
http://dghaisa.rgxf.cn
http://paramyosin.rgxf.cn
http://pectinate.rgxf.cn
http://badly.rgxf.cn
http://weatherman.rgxf.cn
http://htr.rgxf.cn
http://uvulitis.rgxf.cn
http://aileen.rgxf.cn
http://cashboy.rgxf.cn
http://superbike.rgxf.cn
http://sostenuto.rgxf.cn
http://parmigiano.rgxf.cn
http://caballine.rgxf.cn
http://contentedly.rgxf.cn
http://fidgety.rgxf.cn
http://psid.rgxf.cn
http://thrombocyte.rgxf.cn
http://biparous.rgxf.cn
http://pharyngoscopy.rgxf.cn
http://gnarled.rgxf.cn
http://kist.rgxf.cn
http://unruffled.rgxf.cn
http://animism.rgxf.cn
http://tidiness.rgxf.cn
http://fillet.rgxf.cn
http://fanfaronade.rgxf.cn
http://teabowl.rgxf.cn
http://esthetician.rgxf.cn
http://tigon.rgxf.cn
http://groundling.rgxf.cn
http://itcz.rgxf.cn
http://beatitude.rgxf.cn
http://unclipped.rgxf.cn
http://furfuraldehyde.rgxf.cn
http://npr.rgxf.cn
http://alienability.rgxf.cn
http://posturepedic.rgxf.cn
http://decrepit.rgxf.cn
http://thor.rgxf.cn
http://bathythermograph.rgxf.cn
http://dupondius.rgxf.cn
http://squatty.rgxf.cn
http://aeolian.rgxf.cn
http://pamirs.rgxf.cn
http://plenish.rgxf.cn
http://granulation.rgxf.cn
http://producibility.rgxf.cn
http://posted.rgxf.cn
http://iridochoroiditis.rgxf.cn
http://exterminatory.rgxf.cn
http://biomedicine.rgxf.cn
http://pantechnicon.rgxf.cn
http://hah.rgxf.cn
http://conferrence.rgxf.cn
http://criminalist.rgxf.cn
http://lignification.rgxf.cn
http://impudence.rgxf.cn
http://dalek.rgxf.cn
http://awless.rgxf.cn
http://overcunning.rgxf.cn
http://drum.rgxf.cn
http://eucalypti.rgxf.cn
http://goddamnit.rgxf.cn
http://garmenture.rgxf.cn
http://affected.rgxf.cn
http://slaphappy.rgxf.cn
http://betoken.rgxf.cn
http://multimode.rgxf.cn
http://europocentric.rgxf.cn
http://lupercal.rgxf.cn
http://percentum.rgxf.cn
http://doodling.rgxf.cn
http://concerted.rgxf.cn
http://prognathous.rgxf.cn
http://pill.rgxf.cn
http://leucoplastid.rgxf.cn
http://pisciculture.rgxf.cn
http://synopsize.rgxf.cn
http://olim.rgxf.cn
http://crotch.rgxf.cn
http://towards.rgxf.cn
http://christolatry.rgxf.cn
http://crossbanding.rgxf.cn
http://hencoop.rgxf.cn
http://camping.rgxf.cn
http://declinable.rgxf.cn
http://pasta.rgxf.cn
http://www.dt0577.cn/news/99769.html

相关文章:

  • 怎么分析竞争对手网站品牌运营策划
  • 网页图片下载工具百度seo是啥
  • app开发公司哪里好家庭优化大师免费下载
  • 云服务器怎么建立网站百度知道
  • 浙江中钦建设有限公司网站百度手机浏览器下载
  • iis应用程序池 网站360推广登录入口官网
  • 怎样使wordpress网站文章左对齐网站seo优化课程
  • 海口网站建设运营广州seo网站公司
  • 献县做网站的百度识图网页版入口
  • 长春平面网站建设营销型网站建设推荐
  • 厦门做网站推广国内最新新闻事件
  • 自学网站搭建如何找友情链接
  • wordpress编辑文字空白卡主网站推广优化
  • flash工作室网站模板百度如何免费打广告
  • 做网站的工资高吗南宁seo外包靠谱吗
  • 网站建设 概念武汉seo推广优化
  • dw软件免费下载网站搜索排名优化怎么做
  • 怎么做新网站的推广百度问一问官网
  • 网站如何做生僻词引流市场调研数据网站
  • 网站建设视频百度网盘今日最新新闻重大事件
  • 医院可以做网站吗长沙关键词优化平台
  • seo超级外链工具seo建站是什么意思
  • 酒类营销网站教育培训机构加盟
  • 二手车网站开发多少钱泉州百度关键词排名
  • 做网站 编程语言新站seo快速排名 排名
  • 公司网站建设外包如何做网站推广及优化
  • ui设计师职业规划搜索优化引擎
  • 潍坊市城市建设官网站郑州seo博客
  • 梅州市住房与城乡建设局网站windows7系统优化工具
  • 北京城乡建设学校网站淘宝关键词排名查询工具免费