我已经定义了这个流:
public int sumNumbers(int[] numbers) {
return IntStream.of(numbers)
.filter(n -> n <= 1000)
.sum();
}
Run Code Online (Sandbox Code Playgroud)
我总结了不高于1000的所有整数.但是现在我想做的是,如果数组的任何元素是负数,则抛出异常.
我知道如何做到这一点的老式的方式,但我想知道是否有任何机制Stream,并.filter()在那里我可以定义该过滤器的过滤器和异常情况
只是为了澄清我想抛出异常,而不像其他问题那样控制运行时异常.
这里的想法是,如果我的过滤器是真的:
filter(n -> n < 0 throw Exception)
Run Code Online (Sandbox Code Playgroud) 我在玩 Spring reactor,我看不出concat和mergeoperator之间有什么区别
这是我的例子
@Test
public void merge() {
Flux<String> flux1 = Flux.just("hello").doOnNext(value -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Flux<String> flux2 = Flux.just("reactive").doOnNext(value -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Flux<String> flux3 = Flux.just("world");
Flux.merge(flux1, flux2, flux3)
.map(String::toUpperCase)
.subscribe(System.out::println);
}
@Test
public void concat() {
Flux<String> flux1 = Flux.just("hello").doOnNext(value -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Flux<String> …Run Code Online (Sandbox Code Playgroud) 为什么使用数据类型时我不能为这些数据属性提供相同的内部属性名称?
val这里我不能在多个Data中重用变量名
不编译
data Product = Product {val::String}deriving (Show, Eq)
data Price = Price {val::Double}deriving (Show, Eq)
data Discount = Discount { val::Double }deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
编译
data Product = Product {productVal::String}deriving (Show, Eq)
data Price = Price {priceVal::Double}deriving (Show, Eq)
data Discount = Discount { discountVal::Double }deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud) 我deferredResult在Spring MVC上使用,但使用此代码,超时仍然将HTTP代码503发送回客户端.
future.onCompletion(new Runnable() {
@Override
public void run() {
if(future.isSetOrExpired()){
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
}
});
Run Code Online (Sandbox Code Playgroud)
知道还有什么可以尝试吗?
我有一个函数希望接收另一个要执行的函数
a(){
function=$1
echo "common log! $function"
$function --> run the function
}
Run Code Online (Sandbox Code Playgroud)
我想要的是在我的函数中将该函数参数作为嵌套函数传递
b(){
a f(){ echo "nested function b" }
echo "since I´m doing more things here"
}
c(){
a f(){ echo "nested function c" }
echo "since I´m doing more things here"
}
Run Code Online (Sandbox Code Playgroud)
但似乎嵌套函数 f 不能在 bash 上完成
关于如何实现这一点的任何建议?
我继续玩Reactor,现在我看到compose操作符的行为完全一样flatMap,我想知道是否存在我不理解的差异。
@Test
public void compose() throws InterruptedException {
Scheduler mainThread = Schedulers.single();
Flux.just(("old element"))
.compose(element ->
Flux.just("new element in new thread")
.subscribeOn(mainThread)
.doOnNext(value -> System.out.println("Thread:" + Thread.currentThread().getName())))
.doOnNext(value -> System.out.println("Thread:" + Thread.currentThread().getName()))
.subscribe(System.out::println);
Thread.sleep(1000);
}
@Test
public void flatMapVsCompose() throws InterruptedException {
Scheduler mainThread = Schedulers.single();
Flux.just(("old element"))
.flatMap(element ->
Flux.just("new element in new thread")
.subscribeOn(mainThread)
.doOnNext(value -> System.out.println("Thread:" + Thread.currentThread().getName())))
.doOnNext(value -> System.out.println("Thread:" + Thread.currentThread().getName()))
.subscribe(System.out::println);
Thread.sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
这两个示例的行为并返回相同的结果。
问候。
我\xc2\xb4m 寻找运算符 SkipUntil,但似乎没有按我的预期工作。\n这是我的代码
\n\n@Test\npublic void testSkiUitil() throws InterruptedException {\n List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);\n Observable observable2 = Observable.just(1);\n Subscription subscription = Observable.from(numbers)\n .skipUntil(observable2)\n .subscribe(System.out::println);\n Thread.sleep(3000);\n observable2.subscribe();\n new TestSubscriber((Observer) subscription).awaitTerminalEvent(5, TimeUnit.SECONDS);\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我试图证明,由于 observable2 没有任何订阅,因此不会发出任何项目,因此使用操作符skipUntil 的第一个 observable 应该跳过所有项目。但仍然发出所有 5 项。
\n\n知道为什么吗?
\n\n医生说。
\n\n Returns an Observable that skips items emitted by the source Observable until a second Observable emits\nRun Code Online (Sandbox Code Playgroud)\n 我正在使用 gatling-maven-plugin 运行一些性能测试。我只是按照官方文档http://gatling.io/docs/2.2/extensions/maven_plugin/
我有这个配置
<!--PERFORMANCE TEST-->
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>2.2.4</version>
<configuration>
<!--<disableCompiler>true</disableCompiler>-->
<configFolder>${project.basedir}/src/test/resources</configFolder>
<dataFolder>${project.basedir}/src/test/resources/data</dataFolder>
<resultsFolder>${project.basedir}/target/gatling/results</resultsFolder>
<bodiesFolder>${project.basedir}/src/test/resources/bodies</bodiesFolder>
<simulationsFolder>${project.basedir}/src/test/scala/performance/</simulationsFolder>
<runDescription>This-is-the-run-description</runDescription>
<simulationClass>${project.basedir}\src\test\scala\performance\TillScenario</simulationClass>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Run Code Online (Sandbox Code Playgroud)
它在抱怨,因为
error: java.lang.NoClassDefFoundError: scala/reflect/internal/AnnotationInfos$ErroneousAnnotation$
at scala.tools.nsc.typechecker.Typers$class.newTyper(Typers.scala:100)
at scala.tools.nsc.Global$$anon$1.newTyper(Global.scala:463)
at scala.tools.nsc.typechecker.Namers$Namer.<init>(Namers.scala:58)
at scala.tools.nsc.typechecker.Namers$NormalNamer.<init>(Namers.scala:50)
at scala.tools.nsc.typechecker.Namers$class.newNamer(Namers.scala:51)
at scala.tools.nsc.Global$$anon$1.newNamer(Global.scala:463)
at scala.tools.nsc.typechecker.Analyzer$namerFactory$$anon$1.apply(Analyzer.scala:43)
at scala.tools.nsc.Global$GlobalPhase$$anonfun$applyPhase$1.apply$mcV$sp(Global.scala:441)
at scala.tools.nsc.Global$GlobalPhase.withCurrentUnit(Global.scala:432)
at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:441)
at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:399)
at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:399)
at scala.collection.Iterator$class.foreach(Iterator.scala:750)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1202)
at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:399)
at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1500)
at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1487)
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1482)
at scala.tools.nsc.Global$Run.compile(Global.scala:1580)
at scala.tools.nsc.Driver.doCompile(Driver.scala:32)
at scala.tools.nsc.MainClass.doCompile(Main.scala:23)
at scala.tools.nsc.Driver.process(Driver.scala:51)
at scala.tools.nsc.Main.process(Main.scala) …Run Code Online (Sandbox Code Playgroud) 我看到使用 Spring Boot 创建过滤器非常简单。只需关注这样的帖子https://www.baeldung.com/spring-boot-add-filter
我找不到的是如何创建将控制器中的特定端点订阅到一个过滤器的注释。
类似于 Jax-RS 中的东西,它看起来像
@GET
@Path("jax-rs-single")
@Reactive(ttlRequest = 2000)
@Produces(MediaType.APPLICATION_JSON)
public Single getSingle() {
return Single.just("Hello world single");
}
Run Code Online (Sandbox Code Playgroud)
在哪里 @Reactive,将触发每个请求的ReactiveFilter实施。
我也看到了@WebFlow 注释,但这不是我想要的。我想创建一个库,让消费者决定使用哪个过滤器,只需在控制器中添加注释。
知道如何用 Spring boot/MVC 做类似的事情吗?
问候
有人可以解释我为什么 Twitter 未来的行为不是 Async?有这个代码
private val future: Future[String] = Future {
Thread.sleep(2000)
println(s"Running this effect in ${Thread.currentThread().getName}")
"Hello from Twitter"
}
println("Present")
Await.result(future)
Run Code Online (Sandbox Code Playgroud)
两秒后的输出是这个
Running this effect in main
Present
Run Code Online (Sandbox Code Playgroud)
所以未来是阻塞的,而不是在另一个线程中运行