List of usage examples for java.util.concurrent PriorityBlockingQueue offer
public boolean offer(E e)
From source file:com.pari.nm.modules.jobs.PcbImportJob.java
@Override public void execute(JobExecutionContext context) throws JobExecutionException { long t = System.currentTimeMillis(); // to give enough time for client to register for the jobstatus messages try {//from ww w . j a v a2s .c om Thread.sleep(5000); } catch (Exception ee) { } logger = PariLoggerFactory.getLogger(Constants.NM_LOGGER); JobDetail job = context.getJobDetail(); jobId = InventoryDBHelper.getJobId(job.getName(), job.getGroup()); jobRunId = job.getJobDataMap().getInt("jobrunid"); keys = (String[]) job.getJobDataMap().get("PCBSelectedDevices"); int numDevs = (keys == null) ? 0 : keys.length; pcbFileName = (String) job.getJobDataMap().get("PCBFileName"); grpName = (String) job.getJobDataMap().get("GroupName"); login = (String) job.getJobDataMap().get("login"); uId = ((Integer) job.getJobDataMap().get("userId")).intValue(); customerId = ((Integer) job.getJobDataMap().get("CustomerID")).intValue(); wingInstanceName = (String) job.getJobDataMap().get("WingInstanceName"); custWingUniqueId = customerId + "_" + wingInstanceName; int row_id = -1; int version = -1; Key key = null; Map<String/* deivceIp */, PerDeviceImportStatus> perDeviceImportStatus = null; Map<String/* deivceIp */, PerDeviceConfigBackupStatus> perDeviceConfigBackupStatus = null; nccmJobId = ((Integer) job.getJobDataMap().get("nccmJobId")).intValue(); nccmJobRunId = ((Integer) job.getJobDataMap().get("nccmJobRunId")).intValue(); profileName = (String) job.getJobDataMap().get("profileName"); // Fix for Duplicate device issue with parallel device import jobs for same customer // For a customer, only 1 import job will run at a time and other jobs need to wait until previous // import jobs are done. if (currentCustomerJobTokens.containsKey(custWingUniqueId)) { token = currentCustomerJobTokens.get(custWingUniqueId).incrementAndGet(); logger.debug("The token for customer " + customerId + " is " + token); runnableTokens.get(custWingUniqueId).offer(token); } else { AtomicInteger retVal = currentCustomerJobTokens.putIfAbsent(custWingUniqueId, new AtomicInteger(0)); // This can happen first time if two threads simultaneously try to put value in currentCustomerJobTokens // If one thread puts the value, other thread will get some return value... if (retVal != null) { token = currentCustomerJobTokens.get(custWingUniqueId).incrementAndGet(); runnableTokens.get(custWingUniqueId).offer(token); } else { PriorityBlockingQueue<Integer> pbq = new PriorityBlockingQueue<Integer>(); runnableTokens.put(custWingUniqueId, pbq); pbq.offer(token); } currentCustomerRunningToken.put(custWingUniqueId, -1); logger.debug("The token for customer " + customerId + " is " + token); } if (runnableTokens.get(custWingUniqueId) != null) { if (currentCustomerRunningToken.get(custWingUniqueId) == -1) { currentCustomerRunningToken.put(custWingUniqueId, runnableTokens.get(custWingUniqueId).peek()); } if (runnableTokens.get(custWingUniqueId).peek() != currentCustomerRunningToken.get(custWingUniqueId)) { logMsg("Waiting for previous VSEM Import Jobs on same customer to finish."); while (runnableTokens.get(custWingUniqueId).peek() != currentCustomerRunningToken .get(custWingUniqueId)) { try { Thread.sleep(1 * 60 * 1000); } catch (Exception e) { logger.error("Error while waiting for VSEM Jobs to finish...", e); } } } // removing the head of queue runnableTokens.get(custWingUniqueId).poll(); } try { String zipFileName = job.getJobDataMap().getString("ZIPFILE"); PCBFileIf pcbFile = null; if (customerId >= 0) { row_id = ServerDBHelper.insertPcbImportLog(customerId, System.currentTimeMillis(), "NCCM Device file import is in progress.", wingInstanceName); try { processCustomerAndInstance(row_id); } catch (Exception ex) { logger.error("Exception while processing customer instance information", ex); return; } } if (zipFileName != null) { logger.debug("Processing zip file:" + zipFileName); PcbImportJobStatus jobStatus = new PcbImportJobStatus(jobId, jobRunId, 10, "Processing zip file.", JobStatus.RUNNING); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); ZIPImportListener zipListener = new ZIPImportListener(); logger.debug("processing zip file : " + zipFileName + " Job: " + jobId + " Runid: " + jobRunId); ZIPProcessor zipProcessor = ZIPProcessor.open(new File(zipFileName), zipListener, customerId); try { while (zipListener.inProgress) { Thread.sleep(3000); } String err = zipProcessor.getErrorMessage(); if (err != null && !err.isEmpty()) { context.setResult("Completed"); JobRun.logJobCompletionStatus(jobId, jobRunId, false); jobStatus = new PcbImportJobStatus(jobId, jobRunId, 100, err, JobStatus.FAILED); logMsg(err); // CSCua41383 // CSCua44590: Customer upload summary shows wrong msg when an invalid file is imported ServerDBHelper.updatePcbImportLog(customerId, row_id, err); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); return; } String[] deviceNames = zipProcessor.getDeviceNames(); int numZipDevs = (deviceNames == null) ? 0 : deviceNames.length; ZIPImportFileResultMsg msg = new ZIPImportFileResultMsg(jobId, jobRunId, 10, "ZIP Import successful", zipProcessor.getFileResultMap(), zipProcessor.getNumEntries(), numZipDevs, zipProcessor.getErrorMessage(), zipProcessor.getWarningMessage()); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, msg); if (deviceNames == null || numZipDevs == 0) { context.setResult("Completed"); JobRun.logJobCompletionStatus(jobId, jobRunId, false); // CSCua44590: Updated the Displaying Message. ServerDBHelper.updatePcbImportLog(customerId, row_id, "Unable to find any valid devices in the zip file."); jobStatus = new PcbImportJobStatus(jobId, jobRunId, 100, "Unable to find any valid devices in the zip file.", JobStatus.FAILED); logMsg("Unable to find any valid devices in the zip file."); // CSCua41383 ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); return; } jobStatus = new PcbImportJobStatus(jobId, jobRunId, 10, "Processed ZIP file. Found out " + numZipDevs + " devices from " + zipProcessor.getNumEntries() + " files. Converting to VSEM file.", JobStatus.RUNNING); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); logger.debug("Zip processing done for file: " + zipFileName + " Job: " + jobId + " Runid: " + jobRunId); List<HeuristicDescriptor> heus = HeuristicManager.getInstance() .getHeuristics(PCBFile.DEVICE_FAMILY); File f = new File(zipFileName); File f1 = f.getParentFile(); File f2 = new File(f1, "TEMP"); f2.mkdir(); File f3 = new File(f2, "PCB_" + System.nanoTime()); f3.mkdir(); pcbFileName = f3.getAbsolutePath(); ZIP2PCBv2Converter converter = new ZIP2PCBv2Converter(); converter.exportToVSEMFile(zipProcessor, f3, null, heus); logger.debug("Exported to VSEM file"); jobStatus = new PcbImportJobStatus(jobId, jobRunId, 10, "Exported " + zipProcessor.getDeviceNames().length + " devices to VSEM file. Starting import...", JobStatus.RUNNING); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); version = 2; f.delete(); // CSCtz18817 } finally { zipProcessor.cleanup(); } } else { key = getKey(customerId); logger.debug("Checking PCB Version for file: " + pcbFileName); try { version = getPCBFileVersion(pcbFileName, key); } catch (Exception ex) { // CSCtz03087:Job State not getting populated properly in Inventory job logs context.setResult("Completed"); JobRun.logJobCompletionStatus(jobId, jobRunId, false); PcbImportJobStatus jobStatus = new PcbImportJobStatus(jobId, jobRunId, 100, "Not a valid PCB/VSEM File", JobStatus.FAILED); logMsg("Not a valid PCB/VSEM File"); // CSCua41383 // CSCua44590: Customer upload summary shows wrong msg when an invalid file is imported ServerDBHelper.updatePcbImportLog(customerId, row_id, "Not a valid PCB/VSEM File"); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); return; } logger.debug("Found out PCB Version in pcb File: " + pcbFileName + " to be " + version); } PcbImportJobMsg pcbImMsg = null; if (version == 1) { pcbFile = processPcbV1(key); logger.debug("Job: " + jobId + " Number of thread for PCB import: " + noThreads); pcbImMsg = new PcbImportJobMsg(keys, pcbFile, customerId, grpName, wingInstanceName, this, row_id); numDevs = (keys == null) ? 0 : keys.length; Messenger.getInstance().publish(MessageTypes.TRIGGER_PCBIMPORT, pcbImMsg); } else { try { String login = "Unknown " + uId; UserDetails user = UsersFactory.getUser(uId); if (user != null) { login = user.getLogin(); } // Pass jobId also so that DeviceAdder where device list is prepared for custom reports can maintain // map of jobId and device list. This will make sure things work fine in case of parallel vsem // imports. UserDetails historyUserObj = UsersFactory.getUser(InventoryDBHelper.getJobCreatorId(nccmJobId)); String historyUser = login; if (historyUserObj != null) historyUser = historyUserObj.getLogin(); VSEMImporter vsemImporter = new VSEMImporter(pcbFileName, customerId, wingInstanceName, grpName, this, row_id, login, jobId, new JobParameters(jobId, nccmJobId, historyUser)); try { vsemImporter.importVsem(); } catch (Exception expr) { context.setResult("Completed"); JobRun.logJobCompletionStatus(jobId, jobRunId, false); ServerDBHelper.updatePcbImportLog(customerId, row_id, expr.getMessage()); PcbImportJobStatus jobStatus = new PcbImportJobStatus(jobId, job.getJobDataMap().getInt("jobrunid"), 10, "Validate Licenses.", JobStatus.RUNNING); jobStatus = new PcbImportJobStatus(jobId, job.getJobDataMap().getInt("jobrunid"), 100, expr.getMessage(), JobStatus.FAILED); logMsg("No sufficient license found." + expr.getMessage()); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); return; } numDevs = vsemImporter.getNumberOfDevices(); perDeviceImportStatus = vsemImporter.getPerDeviceImportStatus(); perDeviceConfigBackupStatus = vsemImporter.getPerDeviceConfigBackupStatus(); /*XMLImporter xmlImporter = new XMLImporter(pcbFileName, customerId, wingInstanceName, grpName, this, row_id, login, jobId); xmlImporter.importXml(); numDevs = xmlImporter.getNumberOfDevices(); perDeviceImportStatus = xmlImporter.getPerDeviceImportStatus(); perDeviceConfigBackupStatus = xmlImporter.getPerDeviceConfigBackupStatus();*/ noThreads = 0; } catch (Throwable ex) { logger.error("Exception while importing from VSEM File: " + pcbFile, ex); } } if (noThreads > numDevs) { noThreads = numDevs; } int size = numDevs; System.out.println("Number of devices = " + size); while (noThreads > 0) { try { Thread.sleep(2000); logger.debug("Job: " + jobId + ". " + noThreads + " more to go."); } catch (Exception ee) { } } DeviceImportEventHandler evt = new DeviceImportEventHandler(); if (virtsAdded.size() > 0) { Iterator<VirtualDeviceRefresh> it = virtsAdded.iterator(); while (it.hasNext()) { VirtualDeviceRefresh deviceAddedMsg = it.next(); try { DeviceManager.getInstance().processDevice(MessageTypes.VIRUAL_DEVICE_DISCOVERED, deviceAddedMsg); Messenger.getInstance().publish(MessageTypes.DEVICE_CONFIG_CHANGED, deviceAddedMsg.getNodeId() + ""); try { ServerAuditLog.getInstance().logAudit(login, ServerAuditConstants.DEVICE_MANAGEMENT, ServerAuditConstants.DEVICE_MANAGEMENT_DEVICE_STATE, "Virtual Device (" + deviceAddedMsg.getDeviceName() + ") added.", -1, -1); } catch (Exception ee) { } } catch (Exception ex) { ex.printStackTrace(); } } } if (version == 1) { // look for voip phones String voipList = pcbFile.getAttributeValue("ExtendedAttributes", "VoipList"); if (voipList != null) { populateVoIPPhones(voipList); } } context.setResult("Completed"); if (state != JobStatus.FAILED) { state = JobStatus.SUCCESS; } JobRun.logJobCompletionStatus(jobId, jobRunId, (state == JobStatus.SUCCESS ? true : false)); PcbImportJobStatus jobStatus = new PcbImportJobStatus(jobId, jobRunId, 100, "Completed", state); logger.error("Finished executing job: " + jobId + " in " + ((System.currentTimeMillis() - t) / 1000) + " secs"); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); ServerAuditLog.getInstance().logAudit(login, ServerAuditConstants.DEVICE_MANAGEMENT, ServerAuditConstants.DEVICE_MANAGEMENT_DISCOVERY, "Pcb/Zip Import Task Triggered.", jobId, jobRunId); ServerDBHelper.updatePcbImportLog(customerId, row_id, (state == 1) ? "NCCM Device File import partially successfull." : "Successfully imported the NCCM Device File ."); setDeviceImportEventParameters(job, numDevs, row_id, evt); evt.setJobStatus((state == JobStatus.SUCCESS) ? "Success" : "Failed"); evt.setPerDeviceImportStatus(perDeviceImportStatus); EventManager.getInstance().sendEvent(evt); if (perDeviceConfigBackupStatus != null && !perDeviceConfigBackupStatus.isEmpty()) { ConfigBackupEventHandler cfgbkpEvt = new ConfigBackupEventHandler(); cfgbkpEvt.setJobId(jobId); cfgbkpEvt.setJobRunId(jobRunId); cfgbkpEvt.setJobName(job.getName()); cfgbkpEvt.setCount(perDeviceConfigBackupStatus.size()); cfgbkpEvt.setUserId(uId); cfgbkpEvt.setPerDeviceConfigBackupStatus(perDeviceConfigBackupStatus); cfgbkpEvt.setJobStatus((state == JobStatus.SUCCESS) ? "Success" : "Failed"); EventManager.getInstance().sendEvent(cfgbkpEvt); } } catch (Exception ee) { ServerDBHelper.updatePcbImportLog(customerId, row_id, ee.getMessage()); ee.printStackTrace(); logger.error("Error while importing PCB File", ee); logMsg("Error while importing PCB File: " + ee.getMessage()); // CSCtx75737 context.setResult("Completed"); // CSCtx75737 - Setting State of the Job. JobRun.logJobCompletionStatus(jobId, jobRunId, false); PcbImportJobStatus jobStatus = new PcbImportJobStatus(jobId, jobRunId, 100, "Completed", JobStatus.FAILED); ClientSessionManager.getInstance().sendJobStatusMessages(jobId, jobStatus); DeviceImportEventHandler evt = new DeviceImportEventHandler(); setDeviceImportEventParameters(job, numDevs, row_id, evt); evt.setJobStatus("Failed"); evt.setStatus(Constants.PCB_IMPORT_FAIL); EventManager.getInstance().sendEvent(evt); if (perDeviceConfigBackupStatus != null && !perDeviceConfigBackupStatus.isEmpty()) { ConfigBackupEventHandler cfgbkpEvt = new ConfigBackupEventHandler(); cfgbkpEvt.setJobId(jobId); cfgbkpEvt.setJobRunId(jobRunId); cfgbkpEvt.setJobName(job.getName()); cfgbkpEvt.setCount(perDeviceConfigBackupStatus.size()); cfgbkpEvt.setUserId(uId); cfgbkpEvt.setPerDeviceConfigBackupStatus(perDeviceConfigBackupStatus); cfgbkpEvt.setJobStatus("Failed"); EventManager.getInstance().sendEvent(cfgbkpEvt); } } finally { try { // Invoke custom report engine after vsem import job is complete - generates report for devices in vsem // CustomReportHandler handler = CustomReportHandler.getInstance(); // handler.generateReport(); CustomReportJobDetails jobDetails = new CustomReportJobDetails(); jobDetails.setJobDesc("Job triggered due to device import"); jobDetails.setVsemFileName(pcbFileName, customerId); jobDetails.setVsemImportJobId(jobId); List<ScriptInfo> list = ReportDefManagerImpl.getInstance().getBasicScriptInfoByType(ScriptType.Top, customerId); // if a vsem file is being imported for a customer, then all the scripts which belong to him as well as // the scripts which belong to all customers should be executed. if (customerId != ICEntity.ALL_CUSTOMER_ID) { list.addAll(ReportDefManagerImpl.getInstance().getBasicScriptInfoByType(ScriptType.Top, ICEntity.ALL_CUSTOMER_ID)); } // customerId); Set<String> devices = new HashSet<String>(); StringBuilder devIds = new StringBuilder(); // Get unique list of devices associated with all the scripts. for (ScriptInfo script : list) { Set<Device> devSet = NCCMCustomReportHandler.getInstance().getDeviceList(script, false, jobId); for (Device dev : devSet) { devices.add(dev.getDeviceID()); } } int i = 0; for (String device : devices) { devIds.append("D").append(device); if (i < (devices.size() - 1)) { devIds.append(","); } i++; } if (list.size() > 0) { jobDetails.setDevices(devIds.toString()); JobMgr.getInstance().scheduleRunNowCustomReportJob(uId, jobDetails); } if (pcbFileName != null) { pushToBackupServer(); } } catch (Exception ee) { ee.printStackTrace(); } finally { if (runnableTokens.get(custWingUniqueId) != null) { // Remove from currentCustomerJob if all threads have finished if (runnableTokens.get(custWingUniqueId).isEmpty()) { currentCustomerJobTokens.remove(custWingUniqueId); } else { // Getting and setting the latest token to the current customer. currentCustomerRunningToken.put(custWingUniqueId, runnableTokens.get(custWingUniqueId).peek()); } } } } }