我用下面的代码创建了一个spring调度器。
SchedulerConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
@Configuration
@EnableScheduling
public class SchedulerConfig implements SchedulingConfigurer {
private final int POOL_SIZE = 7;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setTaskScheduler(poolScheduler());
}
@Bean
public TaskScheduler poolScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setThreadNamePrefix("poolScheduler");
scheduler.setPoolSize(POOL_SIZE);
return scheduler;
}
}调度器
package com.boilerplate.components;
import java.util.Date;
import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Async;
import com.boilerplate.services.MessageListenerService;
import java.util.Random;
//Posts Table Create Code
/**
CREATE TABLE `posts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(50) NULL DEFAULT NULL,
`legendname` VARCHAR(50) NULL DEFAULT NULL,
`age` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
*/
//Hibernate
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
@Configuration
@EnableAsync
@EnableScheduling
public class Scheduler {
//Database read and update and delete
@Autowired
private RabbitTemplate rabbitTemplate;
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(MessageListenerService.class);
@Autowired
private SessionFactory sessionFactory;
@Async
@Scheduled(cron="*/6 * * * * *")
public void doSomething() {
Session currentSession = sessionFactory.getCurrentSession();
Random rand = new Random();
Random f = new Random();
Random l = new Random();
Random a = new Random();
int n = rand.nextInt(5000) + 1;
int firstname = f.nextInt(5000) + 98;
int legendname = l.nextInt(5000) + 54;
int age = a.nextInt(5000) + 23;
String all = Integer.toString(n)+"_"+"1200";
rabbitTemplate.convertAndSend("bottomlesspit", all);
LOGGER.info("this connected....");
LOGGER.info("Running at " + new Date());
LOGGER.info("Current Thread : {}", Thread.currentThread().getName());
@SuppressWarnings({"rawtypes","deprecation"})
Query theQuery = currentSession.createSQLQuery("INSERT INTO posts(firstname,legendname,age) values(:firstname,:legendname,:age)");
theQuery.setParameter("firstname", firstname);
theQuery.setParameter("legendname", legendname);
theQuery.setParameter("age",age);
theQuery.executeUpdate();
}
}然而,当我查看控制台时,我预计每隔6秒就会输出7个线程,但这并没有发生。我希望每隔6秒就能看到7条日志消息,但我只看到一条。
如何确保线程池中的所有7个线程都在调度程序中使用?
更新:
@Async
@Scheduled(cron="*/6 * * * * *")
public void doSomething() {
Session currentSession = sessionFactory.getCurrentSession();
Random rand = new Random();
Random f = new Random();
Random l = new Random();
Random a = new Random();
int n = rand.nextInt(5000) + 1;
int firstname = f.nextInt(5000) + 98;
int legendname = l.nextInt(5000) + 54;
int age = a.nextInt(5000) + 23;
int x = 1;
while( x < 100 ) {
String all = Integer.toString(n)+"_"+"1200";
rabbitTemplate.convertAndSend("bottomlesspit", all);
LOGGER.info("this connected....");
LOGGER.info("Running at " + new Date());
LOGGER.info("Current Thread : {}", Thread.currentThread().getName());
@SuppressWarnings({"rawtypes","deprecation"})
Query theQuery = currentSession.createSQLQuery("INSERT INTO posts(firstname,legendname,age) values(:firstname,:legendname,:age)");
theQuery.setParameter("firstname", firstname);
theQuery.setParameter("legendname", legendname);
theQuery.setParameter("age",age);
theQuery.executeUpdate();
}
}发布于 2018-11-02 16:20:07
如果当前线程执行完成,它会回收该线程,而不是创建一个新线程,所以这就是为什么你会一遍又一遍地看到同一个线程-尝试在计划的任务中添加一个休眠1分钟的Thread.sleep -当下一个任务被调度时,你应该会看到新的线程被创建(因为前一个线程/执行将仍然很忙)
https://stackoverflow.com/questions/53114831
复制相似问题