From 9f16e26bf4df1800b1a902a8b8460fee09c20445 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Thu, 25 Feb 2021 10:14:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/IotMessageListener.java | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java b/src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java index c6825b3..e13f429 100644 --- a/src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java +++ b/src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java @@ -8,6 +8,9 @@ import org.springframework.stereotype.Component; import javax.jms.Message; import javax.jms.MessageListener; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** @@ -21,6 +24,30 @@ import java.util.concurrent.atomic.AtomicInteger; public class IotMessageListener implements MessageListener { private AtomicInteger count = new AtomicInteger(0); + /** + * Runnable:实现了Runnable接口,jdk就知道这个类是一个线程 + */ + private Runnable runnable; + + public IotMessageListener() { + this.runnable = new Runnable() { + //创建 run 方法 + @Override + public void run() { + // task to run goes here + count.getAndSet(0); + System.out.println("Hello, stranger"); + } + }; + + // ScheduledExecutorService:是从Java SE5的java.util.concurrent里, + // 做为并发工具类被引进的,这是最理想的定时任务实现方式。 + ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); + // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间 + // 10:秒 5:秒 + // 第一次执行的时间为10秒,然后每隔五秒执行一次 + service.scheduleAtFixedRate(runnable, 60, 60, TimeUnit.SECONDS); + } private ExecutorService executorService; public void setExecutorService(ExecutorService executorService) { @@ -56,22 +83,24 @@ public class IotMessageListener implements MessageListener { + ", messageId = " + messageId + ", content = " + content); count.getAndIncrement(); - log.info("count is : " + count.toString()); + } catch (Exception e) { log.error("processMessage occurs error ", e); } } - @Scheduled(cron = "${scheduler.task.cron}") - public void countMessages() { - //log.info("开始执行定时推送失败的推送记录!"); - // 获取推送失败的记录 - //try { - log.warn("约1分钟处理 {} 个请求",count); - count.getAndSet(0); - //} catch (Exception e) { - // log.error("执行定时计数发生异常:", e); - //} - } + + +// @Scheduled(cron = "${scheduler.task.cron}") +// public void countMessages() { +// //log.info("开始执行定时推送失败的推送记录!"); +// // 获取推送失败的记录 +// //try { +// log.warn("约1分钟处理 {} 个请求",count); +// count.getAndSet(0); +// //} catch (Exception e) { +// // log.error("执行定时计数发生异常:", e); +// //} +// } }