reactor.util.context.Context.get()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(281)

本文整理了Java中reactor.util.context.Context.get()方法的一些代码示例,展示了Context.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.get()方法的具体详情如下:
包路径:reactor.util.context.Context
类名称:Context
方法名:get

Context.get介绍

[英]Resolve a value given a type key within the Context.
[中]

代码示例

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void get() {
  assertThat((String) c.get(1)).isEqualTo("A");
  assertThat((String) c.get(2)).isEqualTo("B");
  assertThat((String) c.get(3)).isEqualTo("C");
  assertThat((String) c.get(4)).isEqualTo("D");
  assertThat((String) c.get(5)).isEqualTo("E");
  assertThat((String) c.get(6)).isEqualTo("F");
}

代码示例来源:origin: spring-projects/spring-security

/**
 * Gets the {@code Mono<SecurityContext>} from Reactor {@link Context}
 * @return the {@code Mono<SecurityContext>}
 */
public static Mono<SecurityContext> getContext() {
  return Mono.subscriberContext()
    .filter( c -> c.hasKey(SECURITY_CONTEXT_KEY))
    .flatMap( c-> c.<Mono<SecurityContext>>get(SECURITY_CONTEXT_KEY));
}

代码示例来源:origin: spring-projects/spring-security

private Mono<ServerWebExchange> currentServerWebExchange() {
  return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class));
}

代码示例来源:origin: lettuce-io/lettuce-core

/**
 * Gets the {@link TraceContextProvider} from Reactor {@link Context}.
 *
 * @return the {@link TraceContextProvider}.
 */
static Mono<TraceContextProvider> getContext() {
  return Mono.subscriberContext().filter(c -> c.hasKey(TraceContextProvider.class))
      .map(c -> c.get(TraceContextProvider.class));
}

代码示例来源:origin: lettuce-io/lettuce-core

@Override
  public Mono<TraceContext> getTraceContextLater() {
    return Mono.subscriberContext()
        .filter(it -> it.hasKey(Span.class) || it.hasKey(brave.propagation.TraceContext.class)).map(it -> {
          if (it.hasKey(Span.class)) {
            return new BraveTraceContext(it.get(Span.class).context());
          }
          return new BraveTraceContext(it.get(brave.propagation.TraceContext.class));
        });
  }
}

代码示例来源:origin: reactor/reactor-core

private Object[] sourcesContext() {
  return new Object[] {
      new Object[] { Mono.subscriberContext().map(it -> it.get(String.class)).hide() },
      new Object[] { Mono.subscriberContext().map(it -> it.get(String.class)) }
  };
}

代码示例来源:origin: reactor/reactor-core

@Test
public void currentContextWithEmpty() throws InterruptedException {
  StepVerifier.create(Mono.just("foo")
              .flatMap(d -> Mono.subscriberContext()
                       .map(c -> d + c.get(Integer.class))))
        .verifyErrorMatches(t -> t instanceof NoSuchElementException
            && "Context is empty".equals(t.getMessage()));
}

代码示例来源:origin: spring-projects/spring-security

@GetMapping("/**")
  Mono<String> pathWithinApplicationFromContext() {
    return Mono.subscriberContext()
      .filter(c -> c.hasKey(ServerWebExchange.class))
      .map(c -> c.get(ServerWebExchange.class))
      .map(e -> e.getRequest().getPath().pathWithinApplication().value());
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void currentContext() throws InterruptedException {
  StepVerifier.create(Mono.just("foo")
              .flatMap(d -> Mono.subscriberContext()
                       .map(c -> d + c.get(Integer.class)))
              .subscriberContext(ctx ->
                  ctx.put(Integer.class, ctx.get(Integer.class) + 1))
              .flatMapMany(Mono::just)
              .subscriberContext(ctx -> ctx.put(Integer.class, 0)))
        .expectNext("foo1")
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  UnicastProcessor<Integer> p = UnicastProcessor.create();
  p.subscriberContext(ctx -> ctx.put("foo", "bar")).subscribe();
  assertThat(p.sink().currentContext().get("foo").toString()).isEqualTo("bar");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSimple1() {
  String key = "message";
  Mono<String> r = Mono.just("Hello")
             .flatMap( s -> Mono.subscriberContext() //<2>
                      .map( ctx -> s + " " + ctx.get(key))) //<3>
             .subscriberContext(ctx -> ctx.put(key, "World")); //<1>
  StepVerifier.create(r)
        .expectNext("Hello World") //<4>
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.create(s -> IntStream.range(0, 10).forEach(i -> s.next(s
      .currentContext()
                              .get(AtomicInteger.class)
                              .incrementAndGet())))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTestPush() {
  StepVerifier.create(Flux.push(s -> IntStream.range(0, 10).forEach(i -> s.next(s
      .currentContext()
                              .get(AtomicInteger.class)
                              .incrementAndGet())))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void getThrows() throws Exception {
  assertThatExceptionOfType(NoSuchElementException.class)
      .isThrownBy(() -> c.get(1))
      .withMessage("Context is empty");
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextSimple4() {
  String key = "message";
  Mono<String> r = Mono.just("Hello")
          .flatMap( s -> Mono.subscriberContext()
                    .map( ctx -> s + " " + ctx.get(key)))
          .subscriberContext(ctx -> ctx.put(key, "Reactor")) //<1>
          .subscriberContext(ctx -> ctx.put(key, "World")); //<2>
  StepVerifier.create(r)
        .expectNext("Hello Reactor") //<3>
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Mono.create(s -> s.success(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.generate(s -> s.next(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .take(10)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextTest() {
  StepVerifier.create(Flux.just("foo")
              .handle((d, s) -> s.next(s.currentContext()
                          .get(AtomicInteger.class)
                          .incrementAndGet()))
              .repeat(9)
              .subscriberContext(ctx -> ctx.put(AtomicInteger.class,
                  new AtomicInteger())))
        .expectNext(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void contextGetMono() throws InterruptedException {
  StepVerifier.create(Mono.just(1)
              .log()
              .handle((d, c) -> c.next(c.currentContext().get("test") + "" + d))
              .handle((d, c) -> c.next(c.currentContext().get("test2") + "" + d))
              .subscriberContext(ctx -> ctx.put("test2", "bar"))
              .subscriberContext(ctx -> ctx.put("test", "foo"))
              .log())
        .expectNext("barfoo1")
        .verifyComplete();
}

相关文章