Search in sources :

Example 1 with Notifications

use of com.b2international.snowowl.core.events.Notifications in project snow-owl by b2ihealthcare.

the class RepositoryPlugin method init.

@Override
public void init(SnowOwlConfiguration configuration, Environment env) throws Exception {
    final IManagedContainer container = env.container();
    final boolean gzip = configuration.isGzip();
    final RpcConfiguration rpcConfig = configuration.getModuleConfig(RpcConfiguration.class);
    LOG.debug("Preparing RPC communication (config={},gzip={})", rpcConfig, gzip);
    RpcUtil.prepareContainer(container, rpcConfig, gzip);
    LOG.debug("Preparing EventBus communication (gzip={})", gzip);
    RepositoryConfiguration repositoryConfiguration = configuration.getModuleConfig(RepositoryConfiguration.class);
    env.services().registerService(RepositoryConfiguration.class, repositoryConfiguration);
    int maxThreads = repositoryConfiguration.getMaxThreads();
    EventBusNet4jUtil.prepareContainer(container, gzip, maxThreads);
    env.services().registerService(IEventBus.class, EventBusNet4jUtil.getBus(container, maxThreads));
    LOG.debug("Preparing JSON support");
    final ObjectMapper mapper = JsonSupport.getDefaultObjectMapper();
    mapper.registerModule(new PrimitiveCollectionModule());
    env.services().registerService(ObjectMapper.class, mapper);
    // initialize Notification support
    env.services().registerService(Notifications.class, new Notifications(env.service(IEventBus.class), env.plugins().getCompositeClassLoader()));
    env.services().registerService(RepositoryCommitNotificationSender.class, new RepositoryCommitNotificationSender());
    // initialize Index Settings
    final IndexSettings indexSettings = new IndexSettings();
    indexSettings.putAll(initIndexSettings(env));
    env.services().registerService(IndexSettings.class, indexSettings);
}
Also used : PrimitiveCollectionModule(com.b2international.collections.PrimitiveCollectionModule) IManagedContainer(org.eclipse.net4j.util.container.IManagedContainer) IndexSettings(com.b2international.snowowl.core.config.IndexSettings) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) Notifications(com.b2international.snowowl.core.events.Notifications) RpcConfiguration(com.b2international.snowowl.rpc.RpcConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Notifications

use of com.b2international.snowowl.core.events.Notifications in project snow-owl by b2ihealthcare.

the class ClassifyOperation method run.

/**
 * Allocates a reasoner instance, performs the requested operation, then releases the borrowed instance back to the pool.
 * @param monitor an {@link IProgressMonitor} to monitor operation progress
 * @return the value returned by {@link #processResults(IProgressMonitor, long)}
 * @throws OperationCanceledException
 */
public T run(final IProgressMonitor monitor) throws OperationCanceledException {
    monitor.beginTask("Classification in progress...", IProgressMonitor.UNKNOWN);
    try {
        final String classificationId = UUID.randomUUID().toString();
        final String jobId = IDs.sha1(classificationId);
        final Notifications notifications = getServiceForClass(Notifications.class);
        final BlockingQueue<RemoteJobEntry> jobQueue = Queues.newArrayBlockingQueue(1);
        final Observable<RemoteJobEntry> jobObservable = notifications.ofType(RemoteJobNotification.class).filter(RemoteJobNotification::isChanged).filter(notification -> notification.getJobIds().contains(jobId)).concatMap(notification -> JobRequests.prepareSearch().one().filterById(jobId).buildAsync().execute(getEventBus())).map(RemoteJobs::first).map(Optional<RemoteJobEntry>::get).filter(RemoteJobEntry::isDone);
        // "One-shot" subscription; it should self-destruct after the first notification
        jobObservable.subscribe(new DisposableObserver<RemoteJobEntry>() {

            @Override
            public void onComplete() {
                dispose();
            }

            @Override
            public void onError(final Throwable t) {
                dispose();
            }

            @Override
            public void onNext(final RemoteJobEntry job) {
                try {
                    jobQueue.put(job);
                } catch (InterruptedException e) {
                    throw new SnowowlRuntimeException("Interrupted while trying to add a remote job entry to the queue.", e);
                } finally {
                    dispose();
                }
            }
        });
        ClassificationRequests.prepareCreateClassification().setClassificationId(classificationId).setReasonerId(reasonerId).setUserId(userId).addAllConcepts(additionalConcepts).setParentLockContext(parentLockContext).build(branch).get(ApplicationContext.getServiceForClass(Environment.class));
        while (true) {
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            try {
                final RemoteJobEntry jobEntry = jobQueue.poll(CHECK_JOB_INTERVAL_SECONDS, TimeUnit.SECONDS);
                if (jobEntry == null) {
                    continue;
                }
                switch(jobEntry.getState()) {
                    // $FALL-THROUGH$
                    case SCHEDULED:
                    case RUNNING:
                    case CANCEL_REQUESTED:
                        break;
                    case FINISHED:
                        try {
                            return processResults(classificationId);
                        } finally {
                            deleteEntry(jobId);
                        }
                    case CANCELED:
                        deleteEntry(jobId);
                        throw new OperationCanceledException();
                    case FAILED:
                        deleteEntry(jobId);
                        throw new SnowowlRuntimeException("Failed to retrieve the results of the classification.");
                    default:
                        throw new IllegalStateException("Unexpected state '" + jobEntry.getState() + "'.");
                }
            } catch (final InterruptedException e) {
            // Nothing to do
            }
        }
    } finally {
        monitor.done();
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Notifications(com.b2international.snowowl.core.events.Notifications) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ClassificationRequests(com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests) BlockingQueue(java.util.concurrent.BlockingQueue) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) IEventBus(com.b2international.snowowl.eventbus.IEventBus) UUID(java.util.UUID) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDs(com.b2international.snowowl.core.id.IDs) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DatastoreLockContextDescriptions(com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions) RemoteJobNotification(com.b2international.snowowl.core.jobs.RemoteJobNotification) Environment(com.b2international.snowowl.core.setup.Environment) Queues(com.google.common.collect.Queues) DisposableObserver(io.reactivex.observers.DisposableObserver) ApplicationContext.getServiceForClass(com.b2international.snowowl.core.ApplicationContext.getServiceForClass) JobRequests(com.b2international.snowowl.core.jobs.JobRequests) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) Optional(java.util.Optional) Observable(io.reactivex.Observable) ApplicationContext(com.b2international.snowowl.core.ApplicationContext) RemoteJobs(com.b2international.snowowl.core.jobs.RemoteJobs) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RemoteJobEntry(com.b2international.snowowl.core.jobs.RemoteJobEntry) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Environment(com.b2international.snowowl.core.setup.Environment) Notifications(com.b2international.snowowl.core.events.Notifications)

Aggregations

Notifications (com.b2international.snowowl.core.events.Notifications)2 PrimitiveCollectionModule (com.b2international.collections.PrimitiveCollectionModule)1 ApplicationContext (com.b2international.snowowl.core.ApplicationContext)1 ApplicationContext.getServiceForClass (com.b2international.snowowl.core.ApplicationContext.getServiceForClass)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 IndexSettings (com.b2international.snowowl.core.config.IndexSettings)1 RepositoryConfiguration (com.b2international.snowowl.core.config.RepositoryConfiguration)1 IDs (com.b2international.snowowl.core.id.IDs)1 DatastoreLockContextDescriptions (com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions)1 JobRequests (com.b2international.snowowl.core.jobs.JobRequests)1 RemoteJobEntry (com.b2international.snowowl.core.jobs.RemoteJobEntry)1 RemoteJobNotification (com.b2international.snowowl.core.jobs.RemoteJobNotification)1 RemoteJobs (com.b2international.snowowl.core.jobs.RemoteJobs)1 Environment (com.b2international.snowowl.core.setup.Environment)1 IEventBus (com.b2international.snowowl.eventbus.IEventBus)1 RpcConfiguration (com.b2international.snowowl.rpc.RpcConfiguration)1 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)1 ClassificationRequests (com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Queues (com.google.common.collect.Queues)1