Pārlūkot izejas kodu

计数线程处理调整

master
林万龙 pirms 3 gadiem
vecāks
revīzija
e43bb19863
1 mainītis faili ar 8 papildinājumiem un 6 dzēšanām
  1. +8
    -6
      src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java

+ 8
- 6
src/main/java/com/telpo/iotgateway/listener/IotMessageListener.java Parādīt failu

@@ -1,16 +1,16 @@
package com.telpo.iotgateway.listener;

import com.google.common.base.Stopwatch;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.scheduling.annotation.Scheduled;
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 javax.validation.constraints.Positive;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
@@ -36,13 +36,15 @@ public class IotMessageListener implements MessageListener {
public void run() {
// task to run goes here
count.getAndSet(0);
System.out.println("Hello, stranger");
log.warn("约1分钟处理 {} 个请求",count);
//System.out.println("Hello, stranger");
}
};

// ScheduledExecutorService:是从Java SE5的java.util.concurrent里,
// 做为并发工具类被引进的,这是最理想的定时任务实现方式。
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1,
new BasicThreadFactory.Builder().namingPattern("count-schedule-pool-%d").daemon(true).build());
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
// 10:秒 5:秒
// 第一次执行的时间为10秒,然后每隔五秒执行一次


Notiek ielāde…
Atcelt
Saglabāt