我有以下使用反应堆核心中的通量的反应代码:
Flux.create(sink -> ... /* listens to and receives from external source */ , FluxSink.OverflowStrategy.LATEST)
.flatMap(map -> redisHashReactiveCommands.hmset(key, map))
//.flatMap(... //want to store same data async into kafka with its own back pressure handling)
.subscribeOn(Schedulers.parallel())
.doOnNext(s -> log.debug("Redis consumed. Result -> {}", s))
.doOnComplete(() -> log.debug("On completed."))
.doOnError(exception -> log.error("Error occurred while consuming message", exception))
.subscribe();
Run Code Online (Sandbox Code Playgroud)
如您所见,我对流程的外部源 (FluxSink.OverflowStrategy.LATEST) 进行了背压处理。但是,我还想为我的进程配置背压到 redis (redisHashReactiveCommands.hmset(key, map)),因为它可能是比我的进程的外部源更大的瓶颈。我希望我需要为 redis 部分创建另一个通量并将其与这个通量链接,但是我如何实现这一点,因为 .flatMap 适用于单个项目而不是项目流?
另外,我也想将相同发出的项目存储到 Kafka 中,但是链接flapMap 似乎不起作用.. 是否有一种简单的方法将所有这些链接到一组函数调用中(外部源 -> 我的过程,我的进程-> redis,我的进程-> kafka)?
感谢 @GlennWatson 指出ReactiveUI.WPF除了ReactiveUI包之外,我还需要添加对 Nuget Package 的引用。
我有一个ReactiveObject视图模型,我想在其中使用 anOpenFileDialog来设置我的一个视图模型属性 ( PdfFilePath) 的值。我尝试过的一切都会导致The calling thread cannot access this object because a different thread owns it错误。
我意识到下面的代码不符合 MVVM,因为我在视图模型中使用了“显式引用/实例化视图的类型”的代码,但我只是在寻找一个可以工作的最小示例,以便我可以向后工作,将视图和视图模型代码分开,并最终将服务传递给我的视图模型,该服务处理整个“选择文件路径”部分。
public class ImportPdfViewModel : ReactiveObject
{
public ImportPdfViewModel()
{
SelectFilePathCommand = ReactiveCommand.Create(() =>
{
OpenFileDialog ofd = new OpenFileDialog() { };
//
if (ofd.ShowDialog() == DialogResult.OK)
PdfFilePath = ofd.FileName;
});
}
private string _PdfFilePath;
public string PdfFilePath
{
get => _PdfFilePath;
set => this.RaiseAndSetIfChanged(ref …Run Code Online (Sandbox Code Playgroud) 我使用的是 spring Spring boot 2.1.0。
public Mono<ServerResponse> getConvertXmlToJson(ServerRequest serverRequest) {
Mono<String> requestString = serverRequest.bodyToMono(String.class);
Mono<String> response = this.convertFormatService.convertXmlToJson(requestString);
return response.subscribeOn(elastic()).flatMap(rta -> ok()
.contentType(APPLICATION_JSON_UTF8)
.body(fromObject(rta)))
.switchIfEmpty(ServerResponse.badRequest().build());
}
Run Code Online (Sandbox Code Playgroud)
在其他 bean 中调用此方法
private Mono<String> converter(String function, Mono<String> stringToConverter, String setting) {
log.info("salida, {}", stringToConverter.block());
try {
Object result = invocable.invokeFunction(function, stringToConverter, setting);
log.info("{}", result);
return Mono.just((String) result);
} catch (Exception e) {
throw new ConverterException(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
我需要传递一个字符串值,而不是 Mono,当我传递 Mono 时,答案为空。
当我尝试从 Mono 获取字符串值时,出现此错误:
{
"timestamp": "2018-11-13T03:29:02.161+0000",
"path": "/v1/convert",
"status": 500,
"error": "Internal …Run Code Online (Sandbox Code Playgroud) 我得到了一个这样的可观察对象:
const original = Observable.of({
a: this.http.get('https://jsonplaceholder.typicode.com/todos/11'),
b: this.http.get('https://jsonplaceholder.typicode.com/todos/22'),
c: this.http.get('https://jsonplaceholder.typicode.com/todos/33')
});
Run Code Online (Sandbox Code Playgroud)
我需要解析内部 observables 并在订阅处理程序中得到类似的东西:
{
a: ResponseFromServer,
b: ResponseFromServer,
c: ResponseFromServer,
}
Run Code Online (Sandbox Code Playgroud)
我应该如何解决这个问题?
谢谢。
编辑:我已经想通了,请阅读下文。
似乎没有多少人知道 *Map 运算符曾经有一个称为resultSelector第二个参数的东西。现在在 rxjs v6 中,你可以用 做同样的事情inner map,让我来告诉你。
const original = Observable.of({
a: this.http.get('https://jsonplaceholder.typicode.com/todos/11'),
b: this.http.get('https://jsonplaceholder.typicode.com/todos/22'),
c: this.http.get('https://jsonplaceholder.typicode.com/todos/33')
});
const transformed = original.pipe(
mergeMap(sourceValue =>
forkJoin(_.values(sourceValue)).pipe(map(resolvedHttpRequests => {
// here you have access to sourceValue as well as resolvedHttpRequests so you can do manual join to match the data.
})) …Run Code Online (Sandbox Code Playgroud) reactive-programming object-properties rxjs reactivex angular
我是 Spring WebFlux 的新手,正在尝试将我的 Spring MVC 应用程序转换为 webflux。我从我的服务中返回一个 Mono 单声道:
List<Store> stores = new ArrayList();
Run Code Online (Sandbox Code Playgroud)
当我做:
mono.subscribe(stores::addAll);
dataexchange.put("stores", stores);
return Mono.just(dataexchange);
Run Code Online (Sandbox Code Playgroud)
然后作为响应将商店填充为空列表。但是,我可以在返回响应后验证 subscribe() 是否正常工作。
当我做 :
return mono.flatmap( (response) -> {
dataexchange.put("stores", response));
return Mono.just(dataexchange);
});
Run Code Online (Sandbox Code Playgroud)
然后在响应中填充商店。
有人可以解释一下这两者有什么区别吗?flatMap 是否阻塞?提前致谢 !
I'm attempting to achieve the following in the method below
- Get All Cars from Dealer X
- Create wrapper object that stores a set of all cars and another set of all manufactures 2a. Populate Cars set with the cars obtained in Step 1
- 对于每辆汽车,获得他们所有的独立制造商
- 将所有获得的制造商存储到包装对象制造商集合中
- 返回汽车和制造商的单声道
Mono<CarAndManufactures> requestCarAndManufactures(Request req) {
final String dealerId = buildDealerId(req.getDealerRegion(), req.getDealerId());
final CarAndManufactures CarAndManufactures = new CarAndManufactures();
return webSocketClient.getCars(dealerId) //note #getCars returns a Mono
.map(getCarsResponse -> getCarsResponse
.getResult()
.stream()
.map(Car::getId)
.collect(toSet()))
.map(carIds -> {
CarAndManufactures.setCars(carIds); …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我想让多个客户端连接到一个服务。我正在使用 java Rsocket 实现。
服务应该为每个客户端维护一个状态。现在,我可以通过一些标识符来管理客户端。这个选项我已经实现了。但我不想使用字符串手动管理会话。
所以另一个想法是通过 Rsocket 连接来识别客户端。有没有办法使用 Rsocket 通道来识别特定客户端?
想象一个示例服务和几个客户端。每个客户端都有 Rsocket 通道,服务启动并运行。有没有办法使用 Rsocket 通道在服务器端识别这些客户端?如果您能展示此类行为的程序化示例,那就太棒了。谢谢!
编辑(更详细地描述案例)
这是我的例子。
我们目前使用了三个 CORBA 对象,如图所示:
所以在这里我们使用会话对象在我们的服务上执行事务。
现在我们决定从 CORBA 转移到 Rsocket。因此我们需要 Rsocket 微服务能够存储会话的状态,否则我们无法知道将要提交或回滚什么。这可以通过每个客户端的单独发布者完成吗?
我正在使用轮询方法定期获取数据。新数据可能随时到达。我想向我的客户公开一个反应式接口。所以,我想创建一个发布者(Flux?),它会在新数据可用时发布并通知订阅者。我怎么做?我看到的所有 Flux 示例都是针对数据已知/可用的情况。实际上,我想要类似基于队列的 Flux 之类的东西,并且我的轮询线程在找到新数据时可以继续填充队列。
我正在构建一个 RxJS 幻灯片,如果用户按住右箭头键,我想每 500 毫秒导航到下一个图块。我使用throttleTime如下:
const forwardNavigation$ = fromEvent(document, 'keydown').pipe(
filter(event => event.keyCode === KEY_CODE.arrowRight),
throttleTime(500)
);
Run Code Online (Sandbox Code Playgroud)
我现在想要做的是在导航到第 5 个磁贴后不释放箭头键将油门时间减少到 100 毫秒。
这是可能的,人们将如何实施它?
在我的项目中,需要从外部服务下载文件并将其存储在某个路径中。所以我RestTemplate在 spring中使用将文件下载为 Byte[] 。但是这个操作消耗了我的内存消耗。我在这个博客https://inneka.com/programming/spring/spring-webclient-how-to-stream-large-byte-to-file/ 中发现了关于使用 spring webClient 并将文件写入 path 。由于我关注的文件是我使用的音频文件APPLICATION_OCTET_STREAM。以下代码不起作用的问题。我对反应式编程非常陌生,任何对此的提示都会有所帮助。
public Mono<void> testWebClientStreaming(String filename, String format) throws IOException {
WebClient webClient = WebClient.create("https://stackoverflow.com");
String username = "Username";
String token = "Password";
String path = DOWNLOAD_TEMP_PATH + PATH_SEPERATOR + filename +"."+ format;
Flux<DataBuffer> stream = webClient
.get()
.uri("/files/{filename}.{format}",
filename, format)
.accept(MediaType.APPLICATION_OCTET_STREAM)
.header("Authorization", "Basic " + Base64Utils.encodeToString((username + ":" + token).getBytes(UTF_8)))
.retrieve()
.bodyToFlux(DataBuffer.class);
return DataBufferUtils.write(stream, asynchronousFileChannel)
.doOnComplete(() -> System.out.println("\n\n\n\n\n\n FINISHED "))
.doOnNext(DataBufferUtils.releaseConsumer())
.doAfterTerminate(() -> {
try …Run Code Online (Sandbox Code Playgroud) java reactive-programming spring-boot spring-webflux spring-webclient
java ×5
rxjs ×2
spring-boot ×2
angular ×1
c# ×1
java-8 ×1
javascript ×1
mvvm ×1
reactiveui ×1
reactivex ×1
rsocket ×1