use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.
the class ReactiveChangeStreamOperationSupportTests method changeStreamEventsShouldBeEmittedCorrectly.
@SneakyThrows
// DATAMONGO-2089
@Test
public void changeStreamEventsShouldBeEmittedCorrectly() {
BlockingQueue<ChangeStreamEvent<Document>> documents = new LinkedBlockingQueue<>(100);
Disposable disposable = //
template.changeStream(Document.class).watchCollection(//
"person").listen().doOnNext(documents::add).subscribe();
// just give it some time to link to the collection.
Thread.sleep(500);
Person person1 = new Person("Spring", 38);
Person person2 = new Person("Data", 39);
Person person3 = new Person("MongoDB", 37);
Flux.merge(template.insert(person1).delayElement(Duration.ofMillis(2)), template.insert(person2).delayElement(Duration.ofMillis(2)), //
template.insert(person3).delayElement(Duration.ofMillis(2))).as(//
StepVerifier::create).expectNextCount(//
3).verifyComplete();
// just give it some time to link receive all events
Thread.sleep(500);
try {
assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).hasSize(3).allMatch(Document.class::isInstance);
} finally {
disposable.dispose();
}
}
use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.
the class ReactiveChangeStreamOperationSupportTests method changeStreamEventsShouldBeFilteredCorrectly.
// DATAMONGO-1803
@Test
public void changeStreamEventsShouldBeFilteredCorrectly() throws InterruptedException {
BlockingQueue<ChangeStreamEvent<Person>> documents = new LinkedBlockingQueue<>(100);
Disposable disposable = //
template.changeStream(Person.class).watchCollection(//
Person.class).filter(//
where("age").gte(38)).listen().doOnNext(documents::add).subscribe();
// just give it some time to link to the collection.
Thread.sleep(500);
Person person1 = new Person("Spring", 38);
Person person2 = new Person("Data", 37);
Person person3 = new Person("MongoDB", 39);
Flux.merge(template.save(person1), template.save(person2).delayElement(Duration.ofMillis(50)), //
template.save(person3).delayElement(Duration.ofMillis(100))).as(//
StepVerifier::create).expectNextCount(//
3).verifyComplete();
// just give it some time to link receive all events
Thread.sleep(500);
try {
assertThat(documents.stream().map(ChangeStreamEvent::getBody).collect(Collectors.toList())).containsOnly(person1, person3);
} finally {
disposable.dispose();
}
}
use of reactor.core.Disposable in project spring-data-mongodb by spring-projects.
the class ReactiveMongoRepositoryTests method shouldUseTailableCursorWithProjection.
// DATAMONGO-1444
@Test
void shouldUseTailableCursorWithProjection() throws Exception {
//
template.dropCollection(Capped.class).then(//
template.createCollection(//
Capped.class, CollectionOptions.empty().size(1000).maxDocuments(100).capped())).as(//
StepVerifier::create).expectNextCount(//
1).verifyComplete();
template.insert(new Capped("value", Math.random())).as(StepVerifier::create).expectNextCount(1).verifyComplete();
BlockingQueue<CappedProjection> documents = new LinkedBlockingDeque<>(100);
Disposable disposable = cappedRepository.findProjectionByKey("value").doOnNext(documents::add).subscribe();
CappedProjection projection1 = documents.poll(5, TimeUnit.SECONDS);
assertThat(projection1).isNotNull();
assertThat(projection1.getRandom()).isNotEqualTo(0);
template.insert(new Capped("value", Math.random())).as(StepVerifier::create).expectNextCount(1).verifyComplete();
CappedProjection projection2 = documents.poll(5, TimeUnit.SECONDS);
assertThat(projection2).isNotNull();
assertThat(projection2.getRandom()).isNotEqualTo(0);
assertThat(documents).isEmpty();
disposable.dispose();
}
use of reactor.core.Disposable in project sts4 by spring-projects.
the class ClasspathListenerManager method addClasspathListener.
public Disposable addClasspathListener(ClasspathListener classpathListener) {
String callbackCommandId = "sts4.classpath." + RandomStringUtils.randomAlphabetic(8);
// 1. register callback command handler in SimpleLanguageServer
Disposable unregisterCommand = server.onCommand(callbackCommandId, (ExecuteCommandParams callbackParams) -> async.invoke(() -> {
List<Object> args = callbackParams.getArguments();
// Note: not sure... but args might be deserialized as com.google.gson.JsonElement's.
// If so the code below is not correct (casts will fail).
String projectUri = ((JsonElement) args.get(0)).getAsString();
String name = ((JsonElement) args.get(1)).getAsString();
boolean deleted = ((JsonElement) args.get(2)).getAsBoolean();
Classpath classpath = gson.fromJson((JsonElement) args.get(3), Classpath.class);
classpathListener.changed(new ClasspathListener.Event(projectUri, name, deleted, classpath));
return "done";
}));
// 2. register the callback command with the client
String registrationId = UUID.randomUUID().toString();
RegistrationParams params = new RegistrationParams(ImmutableList.of(new Registration(registrationId, WORKSPACE_EXECUTE_COMMAND, ImmutableMap.of("commands", ImmutableList.of(callbackCommandId)))));
server.getClient().registerCapability(params).join();
// 3. call the client to ask it to call that callback
server.getClient().addClasspathListener(new ClasspathListenerParams(callbackCommandId)).join();
// Cleanups:
return () -> {
try {
log.info("Unregistering classpath callback " + callbackCommandId + " ...");
this.server.getClient().removeClasspathListener(new ClasspathListenerParams(callbackCommandId)).join();
log.info("Unregistering classpath callback " + callbackCommandId + " OK");
this.server.getClient().unregisterCapability(new UnregistrationParams(ImmutableList.of(new Unregistration(registrationId, WORKSPACE_EXECUTE_COMMAND)))).join();
unregisterCommand.dispose();
} catch (Exception e) {
log.error("", e);
}
};
}
use of reactor.core.Disposable in project azure-tools-for-java by Microsoft.
the class SpringCloudDeploymentConfigurationState method execute.
@Override
@Nullable
public ExecutionResult execute(Executor executor, @NotNull ProgramRunner<?> runner) throws ExecutionException {
final RunProcessHandler processHandler = new RunProcessHandler();
processHandler.addDefaultListener();
processHandler.startNotify();
processHandler.setProcessTerminatedHandler(RunProcessHandler.DO_NOTHING);
final ConsoleMessager messager = new ConsoleMessager(processHandler);
final ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(this.project).getConsole();
consoleView.attachToProcess(processHandler);
final Disposable subscribe = Mono.fromCallable(() -> this.execute(messager)).doOnTerminate(processHandler::notifyComplete).subscribeOn(Schedulers.boundedElastic()).subscribe((res) -> messager.success("Deploy succeed!"), messager::error);
processHandler.addProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(@NotNull ProcessEvent event) {
subscribe.dispose();
}
});
return new DefaultExecutionResult(consoleView, processHandler);
}
Aggregations