我在我的android项目中使用RxJava2.
我正在使用以下代码来创建 Observable
public Observable<AlbumDetails> loadAlbumFromAlbumId(final String albumId) {
return Observable.fromCallable(new Callable<AlbumDetails>() {
@Override
public AlbumDetails call() throws Exception {
AlbumDetails albumDetails = getAlbumDetails(albumId);
return albumDetails;
});
}
Run Code Online (Sandbox Code Playgroud)
从observable,我在DisposableObserver的onError方法中遇到以下错误
Callable returned null
使用RxJava时不会发生这种情况.
给定一个使用 的交换WebClient,并按自定义过滤ExchangeFilterFunction:
@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
return next.exchange(request)
.doOnSuccess(response -> {
// ...
});
}
Run Code Online (Sandbox Code Playgroud)
尝试使用多次访问响应正文response.bodyToMono()将导致底层 HTTP 客户端连接器抱怨只允许一个接收者。AFAIK,没有办法访问主体的发布者来获取cache()其信号(我不确定这在资源方面是一个好主意),也没有办法以response这样的方式改变或装饰对象:允许访问其主体(就像ServerWebExchange在服务器端一样)。
这是有道理的,但我想知道是否有任何方法可以通过像这样的过滤器形式订阅响应正文的发布者。我的目标是记录给定实例发送/接收的请求/响应WebClient。
我是反应式编程的新手,所以如果这里有任何明显的禁忌,请解释一下:)
Android Studio 3.2 Canary 8
com.squareup:otto:1.3.8
io.reactivex:rxjava:1.3.7
kotlin 1.2.31
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用otto EventBus将事件发送回我的活动.
但是,我正在使用RxJava执行一些后台工作,并且需要在第一个完成后发送事件.然而,在发布事件后.活动永远不会收到它.
此事件必须在主线程上执行此操作.RxJava位于IO线程上.我不确定最好的方法是什么:
这是我执行RxJava和EventBus帖子的交互器的代码
class Interactors(private val eventBus: Bus) {
fun transmitMessage(): Completable {
return insertTransmission()
.andThen(onTransmissionChanged()) /* Send event to the activity */
.andThen(requestTransmission())
}
private fun insertTransmission(): Completable {
return Completable.fromCallable {
Thread.sleep(4000)
System.out.println("insertTransmission doing some long operation")
}
}
private fun requestTransmission(): Completable {
return Completable.fromCallable {
Thread.sleep(2000)
System.out.println("requestTransmission doing some long operation")
}
}
/* Need to send this event back to the activity/fragment */
private …Run Code Online (Sandbox Code Playgroud) 在 ProjectReactor 或 Reactive Streams 中,在您 subscribe() 之前不会发生任何事情。
除非有人订阅,否则响应式流数据流不会发生,但我看到所有 REST API(如查找、保存和插入)都没有显式调用订阅,但数据在生产者和订阅者之间流动。
@RestController
class PersonController {
private final PersonRepository repository;
public PersonController(PersonRepository repository) {
this.repository = repository;
}
@GetMapping("/all")
public Flux<Person> index() {
return repository.findAll();
}
@GetMapping("/people")
Flux<String> namesByLastname(@RequestParam Mono<String> lastname) {
Flux<Person> result = repository.findByLastname(lastname);
return result.map(it -> it.getFullName());
}
@PostMapping("/people")
Flux<People> AddPeople(@RequestBody Flux<Person> people) {
return repository.saveAll(people);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不需要调用REST 端点的订阅来启动 Project Reactor 中的数据流?
当我从浏览器调用时,REST 端点(HTTP 请求)如何自动订阅 Reactive Streams 以获取数据流?
我在这里错过了什么吗?
reactive-programming project-reactor reactive-streams spring-webflux
嗨,我正在对 RxJS 进行一些研究。我可以简单地通过在我的浏览器中引用它来使用该库:
<script src="https://unpkg.com/@reactivex/rxjs@5.5.6/dist/global/Rx.js"></script>
Run Code Online (Sandbox Code Playgroud)
它使用全局对象命名空间变量“Rx”导入。我可以制作 observables 并做所有有趣的事情。
当我将 src 更改为指向最新的 UMD 文件时,一切都会崩溃 <script src="https://unpkg.com/rxjs/bundles/rxjs.umd.js"></script>
导入似乎不起作用,因为导出的对象函数似乎不存在?
我正在尝试使用一个名为“fromEvent”的特定函数,它允许从任何 DOM 事件创建一个可观察对象。
使用最新的 RxJS 6.2.2 UMD 文件时出现错误。
为什么是这样?如果你查看底部的 js 文件,你可以看到函数的导出,在文件的顶部你会看到名为“rxjs”的全局命名空间。
我没有使用任何像 requirejs 这样的加载器,也没有启用任何实验性浏览器功能。我没有使用任何“导入”语句。
我只是想引用脚本对象的全局命名空间。除了 Rx 与 rxjs 之外,模块定义的语法是相同的。
要复制错误,只需创建一个 Observable.fromEvent(.... 并观察错误控制台。
谢谢!
我很好奇谁开始了 ReactiveX / RxJS?
Rx 似乎在很多语言/平台中实现,我很好奇它的起源是什么?首先是哪个项目?
RxJS 在 Angular Web 框架中大量使用。它背后是否有像微软、Netflix 或谷歌这样的公司?
我正在尝试使用 Spring Web Flux 设置 Spring Security。我不明白如何手动设置SecurityContextwith ReactiveSecurityContextHolder. 您有任何资源或提示吗?以我编写的这个过滤器为例,它读取 JWT 令牌并需要手动设置身份验证:
@Slf4j
public class JwtTokenAuthenticationFilter implements WebFilter {
private final JwtAuthenticationConfig config;
private final JwtParser jwtParser = Jwts.parser();
public JwtTokenAuthenticationFilter(JwtAuthenticationConfig config) {
this.config = config;
jwtParser.setSigningKey(config.getSecret().getBytes());
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
String token = exchange.getRequest().getHeaders().getFirst(config.getHeader());
if (token != null && token.startsWith(config.getPrefix() + " ")) {
token = token.replace(config.getPrefix() + " ", "");
try {
Claims claims = jwtParser.parseClaimsJws(token).getBody();
String username = claims.getSubject();
@SuppressWarnings("unchecked") …Run Code Online (Sandbox Code Playgroud) 我想AWS对由 Spring 的响应式WebClient触发的 HTTP 请求进行签名。要签署请求,我需要访问以下内容:URL、HTTP 方法、查询参数、标头和请求正文字节。
我从编写ExchangeFilterFunction开始。由于ClientRequest接口,我可以访问我需要的所有内容,除了请求正文:
@Component
public class AwsSigningInterceptor implements ExchangeFilterFunction
{
private final AwsHeaderSigner awsHeaderSigner;
public AwsSigningInterceptor(AwsHeaderSigner awsHeaderSigner)
{
this.awsHeaderSigner = awsHeaderSigner;
}
@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next)
{
Map<String, List<String>> signingHeaders = awsHeaderSigner.createSigningHeaders(request, new byte[]{}, "es", "us-west-2"); // should pass request body bytes in place of new byte[]{}
ClientRequest.Builder requestBuilder = ClientRequest.from(request);
signingHeaders.forEach((key, value) -> requestBuilder.header(key, value.toArray(new String[0])));
return next.exchange(requestBuilder.build());
}
}
Run Code Online (Sandbox Code Playgroud)
在旧的 Spring 版本中,我们将RestTemplate与ClientHttpRequestInterceptor …
java spring reactive-programming amazon-web-services spring-webflux
我需要我的端点以以下 json 格式返回数据:
{
"code": "SUCCESS",
"message": "SUCCESS",
"errors": null,
"data": []
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器代码:
@GetMapping(value = "/productSubcategories", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<MyDTO> getMyObjects() {
return myObjectService.getAll()
.map(myObject -> modelMapper.map(productSubcategory, MyObject.class));
}
Run Code Online (Sandbox Code Playgroud)
将所有 MyDTO 对象包装在 json 响应的“数据”部分中的最佳方法是什么?
假设我有一个Parent组件提供Context一个Store对象。为了简单起见,我们假设这个 Store 有一个值和一个更新该值的函数
class Store {
// value
// function updateValue() {}
}
const Parent = () => {
const [rerender, setRerender] = useState(false);
const ctx = new Store();
return (
<SomeContext.Provider value={ctx}>
<Children1 />
<Children2 />
.... // and alot of component here
</SomeContext.Provider>
);
};
const Children1 = () => {
const ctx = useContext(SomeContext);
return (<div>{ctx.value}</div>)
}
const Children2 = () => {
const ctx = useContext(SomeContext);
const …Run Code Online (Sandbox Code Playgroud) javascript reactive-programming typescript reactjs react-context
rx-java ×3
android ×2
java ×2
javascript ×2
rxjs ×2
spring ×2
es6-modules ×1
event-bus ×1
kotlin ×1
reactjs ×1
rx-java2 ×1
typescript ×1
umd ×1