如何将类的单个实例用于所有请求RestController



我有一个名为PriorityJobScheduler的类,在RestControll中,我只想为我在@PostConstruct方法中安装这个类的所有请求创建一次它的对象,但对于每个新请求,我都会得到一个新对象。

我想知道如何只实例化一次这个类,并将其对象用于所有请求。

public class PriorityJobScheduler {
Logger log = LoggerFactory.getLogger(PriorityJobScheduler.class);
private ExecutorService priorityJobPoolExecutor;
private ExecutorService priorityJobScheduler = 
Executors.newSingleThreadExecutor();
private PriorityBlockingQueue<DowloadProccess> priorityQueue;
public PriorityJobScheduler(Integer poolSize, Integer queueSize) {
Comparator<DowloadProccess> priorityComparator = Comparator.comparingInt(DowloadProccess::getJobPriority).reversed();

priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize);
priorityQueue = new PriorityBlockingQueue<DowloadProccess>(queueSize, priorityComparator);
priorityJobScheduler.execute(() -> {
while (true) {
try {
log.debug(priorityQueue.take().toString());
priorityJobPoolExecutor.execute(priorityQueue.take());
} catch (InterruptedException e) {
break;
}
}
});
}

public void scheduleJob(DowloadProccess job) {   
priorityQueue.add(job);   
log.debug("Jobs in queu :: " +  priorityQueue.size());   
}   

}

我的控制器

@RestController
public class ZcrController {
private static int POOL_SIZE = 1;
private static int QUEUE_SIZE = 100;
PriorityJobScheduler pjs;


@PostConstruct
public void init() {
pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
}

@GetMapping("/zcr/run")
public List<Map<String,Object>>  exicuteCRTask(@RequestParam(required = false) String agentId,@RequestParam(required = false) String callCenterId,
@RequestParam(required = false) String clientId,@RequestParam(required = false) String skillId,
@RequestParam(required = false) String interval,@RequestParam("callRecordingId") String callRecordingId,@RequestParam(required = false) String type, @RequestParam(required = false) String customReport,@RequestParam(required = false) boolean ivrAcd,
@RequestParam(required = false) String endDate, @RequestParam(required = false) String startDate,
@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
ITxn txn = new TxnToken();
List<Map<String,Object>> listCDRDetail = dao.getIvrRecords(txn, params,dbParam,type);
log.debug("CDR bean size::" +listCDRDetail.size());

// Here I am passing of instance of that class.
// And on each new request i am getting a new instance pjs.
taskExecutor.runZcrJob(pjs, dbParam, listCDRDetail, type, txn);
return listCDRDetail;

}   

您可以在Spring容器中创建bean

@Configuration
class JobSchedulerConfig {
private static int POOL_SIZE = 1;
private static int QUEUE_SIZE = 100;
@Bean
public PriorityJobScheduler createJobScheduler() {
return new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
}
}

然后在Rest控制器中注入这个bean。

@RestController
public class ZcrController {
private final PriorityJobScheduler priorityJobScheduler;
@Autowired // kept it for readability
public ZcrController(PriorityJobScheduler priorityJobScheduler) {
this.priorityJobScheduler = priorityJobScheduler;
}
...
}

相关内容

  • 没有找到相关文章

最新更新