线程池为7的java Spring调度程序不工作
我用以下代码创建了一个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秒输出七个线程,但这并没有发生。我希望在每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();
}
}
# 1 楼答案
如果当前线程执行完成,它宁愿回收该线程,也不愿创建一个新线程,因此,您会不断看到同一个线程——尝试添加一个线程。在睡眠时间为1分钟的计划任务中睡眠-然后,在计划下一个任务时,您应该会看到正在创建的新线程(因为上一个线程/执行仍然很忙)