Search in sources :

Example 1 with IScanListener

use of org.eclipse.scanning.api.event.scan.IScanListener 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);
        }
    }
}
Also used : ScanBean(org.eclipse.scanning.api.event.scan.ScanBean) IScanListener(org.eclipse.scanning.api.event.scan.IScanListener) ScanEvent(org.eclipse.scanning.api.event.scan.ScanEvent) URI(java.net.URI)

Aggregations

URI (java.net.URI)1 IScanListener (org.eclipse.scanning.api.event.scan.IScanListener)1 ScanBean (org.eclipse.scanning.api.event.scan.ScanBean)1 ScanEvent (org.eclipse.scanning.api.event.scan.ScanEvent)1