java—如何将类的单个示例用于所有请求控制器

42fyovps  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(292)

我有一个名为priorityjobscheduler的类,在restcontrol中,我只想创建一次它的对象,以便在@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;

    }
3z6pesqy

3z6pesqy1#

您可以在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);
    }
}

然后将这个bean注入rest控制器。

@RestController
public class ZcrController {

   private final PriorityJobScheduler priorityJobScheduler;

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

   ...
}

相关问题