use of org.eclipse.scanning.api.event.scan.ScanEvent in project gda-core by openGDA.
the class MScanSubmitter method submit.
/**
* Handle submission of the supplied ScanRequest and monitoring and reporting of the progress of the associated scan
*
* @param scanRequest The ScanRequest to be submitted
* @param block Whether the request should be submitted in blocking or non-blocking mode
* @throws Exception Any Exception arising from the submission operation
*/
public void submit(final ScanRequest scanRequest, final boolean block, final String name) throws Exception {
// validate the scan bean properly here or you get a nullpointer exception
final ScanBean bean = new ScanBean(scanRequest);
eventService.getEventConnectorService().marshal(bean);
final URI uri = new URI(LocalProperties.getActiveMQBrokerURI());
if (name != null && !name.isBlank()) {
bean.setName(name);
}
try (final ISubmitter<ScanBean> submitter = eventService.createSubmitter(uri, EventConstants.SUBMISSION_QUEUE);
final ISubscriber<IScanListener> subscriber = eventService.createSubscriber(uri, EventConstants.STATUS_TOPIC);
var queue = eventService.createJobQueueProxy(uri, EventConstants.SUBMISSION_QUEUE)) {
subscriber.addListener(new IScanListener() {
@Override
public void scanEventPerformed(ScanEvent evt) {
String message = evt.getBean().getMessage();
if (message != null) {
printToJython(Map.of(EMPTYSTRING, message));
}
}
@Override
public void scanStateChanged(ScanEvent evt) {
ScanBean bean = evt.getBean();
if ((bean.getStatus().equals(Status.PREPARING) || bean.getStatus().equals(Status.COMPLETE)) && bean.getFilePath() != null) {
printToJython(Map.of("Output file: ", bean.getFilePath()));
} else if (bean.getStatus().equals(Status.FAILED)) {
facade.print(bean.getMessage());
}
}
});
if (block) {
try {
submitter.blockingSubmit(bean);
} catch (InterruptedException e) {
queue.terminateJob(bean);
facade.print("Scan terminated");
Thread.currentThread().interrupt();
}
} else {
submitter.submit(bean);
}
}
}
Aggregations